thumbnail

The most important functions of this module are open and save_to_cache.

For the freedesktop specifications, it follows: http://jens.triq.net/thumbnail-spec/index.html

lib.thumbnail.delete(filename)
lib.thumbnail.ensure_path(*paths)
lib.thumbnail.get_filesize(filename, file_stat=None)
lib.thumbnail.get_format_data(image, format, size=(128, 128), checkboard=False)

Convert the image in the file bytes of the image at a certain size. By consequence this byte data is different for the chosen format (JPEG, TIFF, ...).

Parameters:
  • image – source image
  • format (string) – image file type format
  • size (tuple of int) – target thumbnail size
Returns:

byte data of the thumbnail

lib.thumbnail.get_freedesktop_filename(filename, size_label='normal')

Get filename of freedekstop thumbnail.

Parameters:
  • filename (string) – image filename
  • size_label (string) – 'normal' or 'large'
Returns:

thumbnail filename

Return type:

string

lib.thumbnail.get_freedesktop_pnginfo(filename, image=None, thumb_info=None)

Gets png metadata for the thumbnail.

Parameter:filename (string) – image filename
Returns:png info
Return type:PngImagePlugin.PngInfo
lib.thumbnail.get_freedesktop_size_label(size)

Returns the freedesktop size label.

Parameter:size (tuple of int) – requested size of the thumbnail
Returns:size label
Return type:string
>>> get_freedesktop_size_label((128, 128))
'normal'
>>> get_freedesktop_size_label((128, 129))
'large'
lib.thumbnail.get_hash(filename)

Get md5 hash of uri of filename.

Parameter:filename (string) – filename
Returns:hash
Return type:string
>>> get_hash('file:///home/user/test.png')
'03223f4f10458a8b5d14327f3ae23136'
lib.thumbnail.get_mtime(filename, file_stat=None)
lib.thumbnail.get_uri(filename)

Get uri of filename.

Parameter:filename (string) – filename
Returns:uri
Return type:string
>>> get_uri('/home/user/test.png')
'file:///home/user/test.png'
lib.thumbnail.is_needed(image, format='JPEG')

Small images don’t need thumbnails

Parameters:
  • image (pil.Image) – image
  • format (string) – image format
Returns:

True if large enough, False otherwise

Return type:

bool

>>> im = Image.new('L', (128, 128))
>>> is_needed(im)
False
>>> im = Image.new('L', (1024, 1024))
>>> is_needed(im)
True
lib.thumbnail.needs_update(filename, size_label='normal', thumb_filename=None)
lib.thumbnail.open(filename, image=None, open_image=<function open_image_exif at 0x2706398>, size=(128, 128), 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.

Parameters:
  • filename (string) – filename of the image
  • image (pil.Image) – generate thumb from pil image directly (optional)
  • open_image (function) – alternative for Image.open
  • size (tuple of int) – size of the thumbnail
  • save_cache (bool) – save thumbnail in cache (linux only)
Returns:

thumbnail

Return type:

pil.Image

lib.thumbnail.save_to_cache(filename, image=None, open_image=<function open_image_exif at 0x2706398>, thumb_info=None, **options)

Save the thumb of image as a thumbnail for specified file.

This is called by the _open function, which requires that it returns the thumb.

Parameters:
  • filename (string) – filename of the image
  • image (pil.Image) – generate thumb from pil image directly (optional)
  • open_image (function) – alternative for Image.open
lib.thumbnail.thumbnail(image, size=(128, 128), checkboard=False, copy=True)

Makes a not in place thumbnail

Parameters:
  • image (pil.Image) – image
  • size (tuple of int) – thumbnail size
Returns:

thumbnail

Return type:

Image

>>> im = Image.new('L', (1024, 1024))
>>> thumbnail(im, (128, 128)).size
(128, 128)

Previous topic

system

Next topic

unicoding