@@ -62,63 +62,71 @@ def render_variation(cls, file_name, variation, replace=False,
62
62
if storage .exists (variation_name ):
63
63
if replace :
64
64
storage .delete (variation_name )
65
- logger .info ('File "{}" already exists and has been replaced.' )
65
+ logger .info ('File "{}" already exists and has been replaced.' ,
66
+ variation_name )
66
67
else :
67
- logger .info ('File "{}" already exists.' )
68
+ logger .info ('File "{}" already exists.' , variation_name )
68
69
return variation_name
69
70
70
- resample = variation ['resample' ]
71
-
72
71
ImageFile .LOAD_TRUNCATED_IMAGES = True
73
72
with storage .open (file_name ) as f :
74
73
with Image .open (f ) as img :
75
- save_kargs = {}
76
- file_format = img .format
77
-
78
- if cls .is_smaller (img , variation ):
79
- factor = 1
80
- while img .size [0 ] / factor \
81
- > 2 * variation ['width' ] \
82
- and img .size [1 ] * 2 / factor \
83
- > 2 * variation ['height' ]:
84
- factor *= 2
85
- if factor > 1 :
86
- img .thumbnail (
87
- (int (img .size [0 ] / factor ),
88
- int (img .size [1 ] / factor )),
89
- resample = resample
90
- )
91
-
92
- size = variation ['width' ], variation ['height' ]
93
- size = tuple (int (i ) if i != float ('inf' ) else i
94
- for i in size )
95
-
96
- if file_format == 'JPEG' :
97
- # http://stackoverflow.com/a/21669827
98
- img = img .convert ('RGB' )
99
- save_kargs ['optimize' ] = True
100
- save_kargs ['quality' ] = 'web_high'
101
- if size [0 ] * size [1 ] > 10000 : # roughly <10kb
102
- save_kargs ['progressive' ] = True
103
-
104
- if variation ['crop' ]:
105
- img = ImageOps .fit (
106
- img ,
107
- size ,
108
- method = resample
109
- )
110
- else :
111
- img .thumbnail (
112
- size ,
113
- resample = resample
114
- )
115
-
74
+ img , save_kargs = cls .process_variation (variation , image = img )
116
75
with BytesIO () as file_buffer :
117
- img .save (file_buffer , file_format , ** save_kargs )
76
+ img .save (file_buffer , ** save_kargs )
118
77
f = ContentFile (file_buffer .getvalue ())
119
78
storage .save (variation_name , f )
120
79
return variation_name
121
80
81
+ @classmethod
82
+ def process_variation (cls , variation , image ):
83
+ """Process variation before actual saving."""
84
+ save_kargs = {}
85
+ file_format = image .format
86
+ save_kargs ['format' ] = file_format
87
+
88
+ resample = variation ['resample' ]
89
+
90
+ if cls .is_smaller (image , variation ):
91
+ factor = 1
92
+ while image .size [0 ] / factor \
93
+ > 2 * variation ['width' ] \
94
+ and image .size [1 ] * 2 / factor \
95
+ > 2 * variation ['height' ]:
96
+ factor *= 2
97
+ if factor > 1 :
98
+ image .thumbnail (
99
+ (int (image .size [0 ] / factor ),
100
+ int (image .size [1 ] / factor )),
101
+ resample = resample
102
+ )
103
+
104
+ size = variation ['width' ], variation ['height' ]
105
+ size = tuple (int (i ) if i != float ('inf' ) else i
106
+ for i in size )
107
+
108
+ if file_format == 'JPEG' :
109
+ # http://stackoverflow.com/a/21669827
110
+ image = image .convert ('RGB' )
111
+ save_kargs ['optimize' ] = True
112
+ save_kargs ['quality' ] = 'web_high'
113
+ if size [0 ] * size [1 ] > 10000 : # roughly <10kb
114
+ save_kargs ['progressive' ] = True
115
+
116
+ if variation ['crop' ]:
117
+ image = ImageOps .fit (
118
+ image ,
119
+ size ,
120
+ method = resample
121
+ )
122
+ else :
123
+ image .thumbnail (
124
+ size ,
125
+ resample = resample
126
+ )
127
+
128
+ return image , save_kargs
129
+
122
130
@classmethod
123
131
def get_variation_name (cls , file_name , variation_name ):
124
132
"""Return the variation file name based on the variation."""
0 commit comments