def _thumbnail_exists(self, filepath): ''' Checks if the thumbnail for <filepath> already exists. This function will return False if the thumbnail exists and it's mTime doesn't match the mTime of <filepath>, it's size is different from the one specified in the thumbnailer, or if <force_recreation> is True. ''' if not self.force_recreation: thumbpath = self._path_to_thumbpath(filepath) if os.path.isfile(thumbpath): # Check the thumbnail's stored mTime try: with reader.LockedFileIO(thumbpath) as fio: with Image.open(fio) as img: info = img.info stored_mtime = float(info['Thumb::MTime']) # The source file might no longer exist file_mtime = os.path.isfile(filepath) and os.stat( filepath).st_mtime or stored_mtime return stored_mtime == file_mtime and \ max(*img.size) == max(self.width, self.height) except IOError: return False else: return False else: return False
def load_pixbuf_size(path, width, height): ''' Loads a pixbuf from a given image file and scale it to fit inside (width, height). ''' try: with reader.LockedFileIO(path) as fio: with Image.open(fio) as im: im.thumbnail((width, height), resample=Image.BOX) return pil_to_pixbuf(im, keep_orientation=True) except: info, image_width, image_height = GdkPixbuf.Pixbuf.get_file_info(path) # If we could not get the image info, still try to load # the image to let GdkPixbuf raise the appropriate exception. if not info: pixbuf = GdkPixbuf.Pixbuf.new_from_file(path) # Don't upscale if smaller than target dimensions! if image_width <= width and image_height <= height: width, height = image_width, image_height # Work around GdkPixbuf bug # https://gitlab.gnome.org/GNOME/gdk-pixbuf/issues/45 # TODO: GIF should be always supported by Pillow. # Is this workaround really needed? if info.get_name() == 'gif': pixbuf = GdkPixbuf.Pixbuf.new_from_file(path) else: # directly return pixbuf return GdkPixbuf.Pixbuf.new_from_file_at_size(path, width, height) return fit_in_rectangle(pixbuf, width, height, scaling_quality=GdkPixbuf.InterpType.BILINEAR)
def get_image_info(path): '''Return image informations: (format, width, height) ''' info = None try: with Image.open(reader.LockedFileIO(path)) as im: return (im.format, ) + im.size except: info = GdkPixbuf.Pixbuf.get_file_info(path) if info[0] is None: info = None else: info = info[0].get_name().upper(), info[1], info[2] if info is None: info = (_('Unknown filetype'), 0, 0) return info
def load_pixbuf_size(path, width, height): ''' Loads a pixbuf from a given image file and scale it to fit inside (width, height). ''' try: with reader.LockedFileIO(path) as fio: with Image.open(fio) as im: im.thumbnail((width, height), resample=Image.BOX) return pil_to_pixbuf(im, keep_orientation=True) except: info, image_width, image_height = GdkPixbuf.Pixbuf.get_file_info(path) # If we could not get the image info, still try to load # the image to let GdkPixbuf raise the appropriate exception. if not info: pixbuf = GdkPixbuf.Pixbuf.new_from_file(path) # Don't upscale if smaller than target dimensions! if image_width <= width and image_height <= height: width, height = image_width, image_height return GdkPixbuf.Pixbuf.new_from_file_at_size(path, width, height)
def load_pixbuf(path): ''' Loads a pixbuf from a given image file. ''' enable_anime = prefs['animation mode'] != constants.ANIMATION_DISABLED try: with Image.open(reader.LockedFileIO(path)) as im: # make sure n_frames loaded im.load() if enable_anime and getattr(im, 'is_animated', False): return load_animation(im) return pil_to_pixbuf(im, keep_orientation=True) except: pass if enable_anime: pixbuf = GdkPixbuf.PixbufAnimation.new_from_file(path) if pixbuf.is_static_image(): return pixbuf.get_static_image() return pixbuf return GdkPixbuf.Pixbuf.new_from_file(path)