66from resizeimage import resizeimage
77
88import tinify
9+ import logging
910import requests
1011
1112from .settings import (OPTIMIZED_IMAGE_METHOD , TINYPNG_KEY )
1213
14+
1315BACKGROUND_TRANSPARENT = (255 , 255 , 255 , 0 )
1416
1517
@@ -34,7 +36,13 @@ def get_image_extension(image):
3436
3537
3638def image_optimizer (image_data , output_size = None , resize_method = None ):
37- """Optimize an image that has not been saved to a file."""
39+ """
40+ Optimize an image that has not been saved to a file.
41+ :param `image_data` is image data, e.g from request.FILES['image']
42+ :param `output_size` is float pixel scale of image (width, height) or None, e.g: (400, 300)
43+ :param `resize_method` is string resize method, choices are: None, "thumbnail", or "cover".
44+ :return optimized image data.
45+ """
3846 if OPTIMIZED_IMAGE_METHOD == 'pillow' :
3947 image = Image .open (image_data )
4048 bytes_io = BytesIO ()
@@ -45,57 +53,31 @@ def image_optimizer(image_data, output_size=None, resize_method=None):
4553 # resize_method. 'thumbnail' is used by default
4654 if output_size is not None :
4755
48- if resize_method is None :
49- pass
56+ if resize_method not in ('thumbnail' , 'cover' , None ):
57+ message = 'optimized_image_resize_method misconfigured, it\' s value must be \' thumbnail\' , \' cover\' or None'
58+ raise Exception (message )
5059
5160 elif resize_method is 'thumbnail' :
52- image = resizeimage .resize_thumbnail (
53- image ,
54- output_size ,
55- resample = Image .LANCZOS
56- )
61+ image = resizeimage .resize_thumbnail (image , output_size , resample = Image .LANCZOS )
5762
5863 elif resize_method is 'cover' :
59- image = resizeimage .resize_cover (
60- image ,
61- output_size ,
62- validate = False
63- )
64-
65- else :
66- raise Exception (
67- 'optimized_image_resize_method misconfigured, it\' s value must be \' thumbnail\' , \' cover\' or None'
68- )
69-
70- output_image = Image .new (
71- 'RGBA' ,
72- output_size ,
73- BACKGROUND_TRANSPARENT
74- )
75-
76- output_image_center = (
77- int ((output_size [0 ] - image .size [0 ]) / 2 ),
78- int ((output_size [1 ] - image .size [1 ]) / 2 )
79- )
80-
81- output_image .paste (
82- image ,
83- output_image_center
84- )
85-
86- # If output_size is None the output_image would be the same as source
64+ image = resizeimage .resize_cover (image , output_size , validate = False )
65+
66+ output_image = Image .new ('RGBA' , output_size , BACKGROUND_TRANSPARENT )
67+ output_image_center = (int ((output_size [0 ] - image .size [0 ]) / 2 ),
68+ int ((output_size [1 ] - image .size [1 ]) / 2 ))
69+
70+ output_image .paste (image , output_image_center )
71+
8772 else :
73+ # If output_size is None the output_image would be the same as source
8874 output_image = image
8975
9076 # If the file extension is JPEG, convert the output_image to RGB
9177 if extension == 'JPEG' :
92- output_image = output_image .convert (" RGB" )
78+ output_image = output_image .convert (' RGB' )
9379
94- output_image .save (
95- bytes_io ,
96- format = extension ,
97- optimize = True
98- )
80+ output_image .save (bytes_io , format = extension , optimize = True )
9981
10082 image_data .seek (0 )
10183 image_data .file .write (bytes_io .getvalue ())
@@ -105,9 +87,15 @@ def image_optimizer(image_data, output_size=None, resize_method=None):
10587 # disable warning info
10688 requests .packages .urllib3 .disable_warnings ()
10789
90+ # just info for people
91+ if any ([output_size , resize_method ]):
92+ message = '[django-image-optimizer] "output_size" and "resize_method" only for OPTIMIZED_IMAGE_METHOD="pillow"'
93+ logging .info (message )
94+
10895 tinify .key = TINYPNG_KEY
10996 optimized_buffer = tinify .from_buffer (image_data .file .read ()).to_buffer ()
11097 image_data .seek (0 )
11198 image_data .file .write (optimized_buffer )
11299 image_data .file .truncate ()
100+
113101 return image_data
0 commit comments