thumbnailfield Package¶
compatibility Module¶
Compatibility module
conf Module¶
-
class
thumbnailfield.conf.ThumbnailFieldConf(**kwargs)[source]¶ Bases:
appconf.base.AppConf-
DEFAULT_PIL_SAVE_OPTIONS= {}¶
-
DEFAULT_PROCESS_METHOD= 'thumbnail'¶
-
DEFAULT_PROCESS_OPTIONS= {'resample': 1}¶
-
FILENAME_PATTERN= '%(root)s/%(filename)s.%(name)s.%(ext)s'¶
-
class
Meta¶ Bases:
object
-
ThumbnailFieldConf.PROCESS_METHOD_TABLE= {'sepia': <function get_sepia_image at 0x7f55cc6e17d0>, 'crop': <function get_cropped_image at 0x7f55cc6e18c0>, 'grayscale': <function get_grayscale_image at 0x7f55cc6e16e0>, 'thumbnail': <function get_thumbnail_image at 0x7f55cc6e1a28>, 'resize': <function get_resized_image at 0x7f55cc6e19b0>}¶
-
ThumbnailFieldConf.REMOVE_PREVIOUS= False¶
-
fields Module¶
Model fields of ThumbnailField
-
class
thumbnailfield.fields.ThumbnailField(verbose_name=None, name=None, width_field=None, height_field=None, patterns=None, pil_save_options=None, **kwargs)[source]¶ Bases:
django.db.models.fields.files.ImageFieldEnhanced ImageField
ThumbnailField has the follwoing features
- Automatically remove previous file
- Automatically generate thumbnail files
- Automatically remove generated previous thumbnail files
-
attr_class¶ alias of
ThumbnailFieldFile
-
description= <django.utils.functional.__proxy__ object>¶
-
descriptor_class¶ alias of
ThumbnailFileDescriptor
-
class
thumbnailfield.fields.ThumbnailFieldFile(*args, **kwargs)[source]¶ Bases:
django.db.models.fields.files.ImageFieldFileEnhanced ImageFieldFile
This FieldFile contains thumbnail ImageFieldFile instances and these thumbnails are automatically generate when accessed
-
_get_thumbnail_filename -- get thumbnail filename
-
_get_image -- get PIL image instance
-
_get_thumbnail -- get PIL image instance of thumbnail
-
_get_thumbnail_file -- get ImageFieldFile instance of thumbnail
-
_create_thumbnail -- create PIL image instance of thumbnail
-
_create_thumbnail_file -- create ImageFieldFile instance of thumbnail
-
_update_thumbnail_file -- update thumbnail file and return ImageFieldFile instance
-
_remove_thumbnail_file -- remove thumbanil file from storage
-
iter_pattern_name -- return iterator of pattern name
-
get_pattern_name -- return list of pattern name
-
iter_thumbnail_filenames -- return iterator of thumbnail filename
-
get_thumbnail_filenames -- return list of thumbnail filename
-
iter_thumbnail_files -- return iterator of thumbnail file
-
get_thumbnail_files -- return list of thumbnail file
-
update_thumbnail_files -- update thumbnail files in storage
-
remove_thumbnail_files -- remove thumbnail files from storage
-
-
class
thumbnailfield.fields.ThumbnailFileDescriptor(field)[source]¶ Bases:
django.db.models.fields.files.ImageFileDescriptorEnhanced ImageFileDescriptor
Just like the ImageFileDescriptor, but for ThumbnailField. The only difference is removing previous Image and Thumbnails from storage when the value has changed.
models Module¶
process_methods Module¶
Builtin ThumbnailField process methods
-
thumbnailfield.process_methods.get_cropped_image(img, width, height, left, upper, **options)[source]¶ get cropped image
- Attribute:
- img – PIL image instance width – width of thumbnail height – height of thumbnail left – left point of thumbnail upper – upper point of thumbnail kwargs – Options used in PIL thumbnail method
- Usage::
>>> from thumbnailfield.compatibility import Image >>> img = Image.new('RGBA', (1000, 800)) >>> thumb = get_cropped_image(img, 100, 100, 0, 0) >>> assert thumb.size[0] == 100 >>> assert thumb.size[1] == 100
-
thumbnailfield.process_methods.get_grayscale_image(img, width, height, **options)[source]¶ get grayscale image
- Attribute:
- img – PIL image instance width – width of thumbnail height – height of thumbnail kwargs – Options used in PIL thumbnail method
- Usage::
>>> from thumbnailfield.compatibility import Image >>> img = Image.new('RGBA', (1000, 800)) >>> thumb = get_grayscale_image(img, 100, 100)
-
thumbnailfield.process_methods.get_resized_image(img, width, height, force=False, **options)[source]¶ get resized image
- Attribute:
- img – PIL image instance width – width of thumbnail height – height of thumbnail kwargs – Options used in PIL thumbnail method
- Usage::
>>> from thumbnailfield.compatibility import Image >>> img = Image.new('RGBA', (1000, 800)) >>> thumb = get_resized_image(img, 100, 100) >>> assert thumb.size[0] == 100 >>> assert thumb.size[1] == 100
-
thumbnailfield.process_methods.get_sepia_image(img, width, height, **options)[source]¶ get sepia image
- Attribute:
- img – PIL image instance width – width of thumbnail height – height of thumbnail kwargs – Options used in PIL thumbnail method
- Usage::
>>> from thumbnailfield.compatibility import Image >>> img = Image.new('RGBA', (1000, 800)) >>> thumb = get_sepia_image(img, 100, 100)
-
thumbnailfield.process_methods.get_thumbnail_image(img, width, height, **options)[source]¶ get thumbnail image
- Attribute:
- img – PIL image instance width – width of thumbnail height – height of thumbnail kwargs – Options used in PIL thumbnail method
- Usage::
>>> from thumbnailfield.compatibility import Image >>> img = Image.new('RGBA', (1000, 800)) >>> thumb = get_thumbnail_image(img, 100, 100) >>> assert thumb.size[0] == 100 >>> assert thumb.size[1] == 80
utils Module¶
Utilities of ThumbnailField
-
thumbnailfield.utils.get_content_file(img, file_fmt, **kwargs)[source]¶ get ContentFile from PIL image instance with file_fmt
-
img -- PIL Image instance
-
file_fmt -- Saved image format PNG, JPEG, ...
-
kwargs -- Options used in PIL image save method
- Usage::
>>> from thumbnailfield.compatibility import Image >>> from django.core.files.base import ContentFile >>> img = Image.new('RGBA', (100, 100)) >>> cf = get_content_file(img, 'PNG') >>> assert isinstance(cf, ContentFile)
-
-
thumbnailfield.utils.get_fileformat_from_filename(filename)[source]¶ get fileformat from filename
-
filename -- filename used to guess fileformat
- Usage::
>>> assert get_fileformat_from_filename("test.png") == "PNG" >>> assert get_fileformat_from_filename("test.jpg") == "JPEG" >>> assert get_fileformat_from_filename("test.jpe") == "JPEG" >>> assert get_fileformat_from_filename("test.jpeg") == "JPEG" >>> assert get_fileformat_from_filename("test.gif") == "GIF" >>> assert get_fileformat_from_filename("test.tif") == "TIFF" >>> assert get_fileformat_from_filename("test.tiff") == "TIFF" >>> assert get_fileformat_from_filename("test.bmp") == "BMP" >>> assert get_fileformat_from_filename("test.dib") == "BMP" >>> assert get_fileformat_from_filename( ... "/some/where/test.png") == "PNG"
-
-
thumbnailfield.utils.get_processed_image(f, img, patterns)[source]¶ process PIL image with pattern attribute
-
f -- ThumbnailFieldFile instance
-
img -- PIL Image instance
-
patterns -- Process patterns
pattern format is shown below:
# Use default process_method with default process options and same # width, height pattern = [square_size] # Use default process_method with default process options and width, # height pattern = [width, height] # Use process_method with default process options and width, height pattern = [width, height, process_method] # Use process_method with process_options with widht, height pattern = [width, height, process_method, process_options]
default process_method and process_options are configured in settings.py as:
THUMBNAILFIELD_DEFAULT_PROCESS_METHOD = 'thumbnail' THUMBNAILFIELD_DEFAULT_PROCESS_OPTIONS = {'filter': Image.ANTIALIAS}
-
-
thumbnailfield.utils.get_thumbnail_filename(path, name, pattern=None)[source]¶ get thumbnail filename with name and pattern
-
path -- original path
-
name -- thumbnail name
-
pattern -- file name generation pattern (default = settings.THUMBNAILFIELD_FILENAME_PATTERN)
- Usage::
>>> path = "/some/where/test.png" >>> name = "small" >>> pattern = r"%(root)s/%(filename)s.%(name)s.%(ext)s" >>> thumb_filename = get_thumbnail_filename(path, name, pattern) >>> assert thumb_filename == "/some/where/test.small.png"
-
-
thumbnailfield.utils.save_to_storage(img, storage, filename, overwrite=False, **kwargs)[source]¶ save PIL image instance to Django storage with filename
-
img -- PIL Image instance
-
storage -- Django storage instance
-
filename -- filename
-
overwrite -- If true, delete existing file first
-
kwargs -- Options used in PIL image save method
- Usage::
>>> from thumbnailfield.compatibility import Image >>> from django.core.files.storage import FileSystemStorage >>> img = Image.new('RGBA', (100, 100)) >>> storage = FileSystemStorage() >>> filename = 'test.png' >>> # save image to storage >>> filename_ = save_to_storage(img, storage, filename) >>> # file exists >>> assert storage.exists(filename_) >>> # resave >>> filename_ = save_to_storage(img, storage, filename) >>> # used different filename >>> assert filename != filename_ >>> storage.delete(filename_) >>> # overwrite the file >>> filename_ = save_to_storage(img, storage, filename, overwrite=True) >>> assert filename == filename_ >>> storage.delete(filename)
-