def open(filename, image=None, open_image=imtools.open_image_exif, size=SIZE, save_cache=True): """Retrieves a thumbnail from a file. It will only use the cache if ``size`` is smaller than the cache thumbnail sizes. On Linux it will try to load it from the freedesktop thumbnail cache, which makes it much faster. Otherwise it will generate the thumbnail. :param filename: filename of the image :type filename: string :param image: generate thumb from pil image directly (optional) :type image: pil.Image :param open_image: alternative for Image.open :type open_image: function :param size: size of the thumbnail :type size: tuple of int :param save_cache: save thumbnail in cache (linux only) :type save_cache: bool :returns: thumbnail :rtype: pil.Image """ thumb = _open(filename=filename, image=image, open_image=open_image, size=size, save_cache=save_cache) if thumb.size[0] > size[0] or thumb.size[1] > size[1]: #we need a smaller thumb return thumbnail(thumb, size, checkboard=True) #add checkerboard for transparant images return imtools.add_checkboard(thumb)
def open(filename, image=None, open_image=imtools.open_image_exif, size=SIZE, save_cache=True, force_size=None): """Retrieves a thumbnail from a file. It will only use the cache if ``size`` is smaller than the cache thumbnail sizes. On Linux it will try to load it from the freedesktop thumbnail cache, which makes it much faster. Otherwise it will generate the thumbnail. :param filename: filename of the image :type filename: string :param image: generate thumb from pil image directly (optional) :type image: pil.Image :param open_image: alternative for Image.open :type open_image: function :param size: size of the thumbnail :type size: tuple of int :param save_cache: save thumbnail in cache (linux only) :type save_cache: bool :param force_size: color if size needs to be forced :type force_size: None or tuple of 3 ints :returns: thumbnail :rtype: pil.Image """ thumb = _open(filename=filename, image=image, open_image=open_image, size=size, save_cache=save_cache) if thumb.size[0] > size[0] or thumb.size[1] > size[1]: #we need a smaller thumb thumb = thumbnail(thumb, size, checkboard=True) else: #add checkerboard for transparant images thumb = imtools.add_checkboard(thumb) x0, y0 = thumb.size x1, y1 = size if force_size and (x0 != x1 or y0 != y1): back = Image.new('RGB', size, force_size) imtools.paste(back, thumb, ((x1 - x0) / 2, (y1 - y0) / 2)) thumb = back return thumb
def thumbnail(image, size=SIZE, checkboard=False, copy=True): """Makes a not in place thumbnail :param image: image :type image: pil.Image :param size: thumbnail size :type size: tuple of int :returns: thumbnail :rtype: Image >>> im = Image.new('L', (1024, 1024)) >>> thumbnail(im, (128, 128)).size (128, 128) """ if copy: thumb = image.copy() #skip if thumb is smaller than requested size if thumb.size[0] > size[0] or thumb.size[1] > size[1]: thumb.thumbnail(size, Image.ANTIALIAS) if checkboard: return imtools.add_checkboard(thumb) return thumb