@@ -45,9 +45,9 @@ def save(self, **kwargs):
4545
4646 def get_thumbnail_url (self ):
4747 thumbnail_path = self .get_thumbnail_path (self .thumbnail_size , self .thumbnail_size )
48- if str (self .id ) == 'a118a297-0931-4ee9-95c7-925624d3b7a3 ' or not default_storage .exists (thumbnail_path ):
48+ if str (self .id ) == '4d4d9ae9-3120-4f24-8d57-9d15fa6edfc4 ' or not default_storage .exists (thumbnail_path ):
4949 try :
50- self .crop (thumbnail_path , self .thumbnail_size , self .thumbnail_size * 2 .5 )
50+ self .crop (thumbnail_path , self .thumbnail_size , self .thumbnail_size * .5 )
5151 except Exception :
5252 # thumbnail image could not be created
5353 return self .fallback_thumbnail_url
@@ -94,7 +94,6 @@ def crop(self, thumbnail_path, width, height):
9494 crop_x = 0
9595 crop_y = (image .height - image .width ) / 2
9696 crop_resize = crop_size = image .width
97- offset_x = offset_y = 0
9897 # elif width > height:
9998 # if width > crop_size * aspect_ratio:
10099 # # extend the crop size to prevent blurry images
@@ -106,89 +105,104 @@ def crop(self, thumbnail_path, width, height):
106105 crop_resize = min (image .height , height )
107106 else :
108107 crop_resize = crop_size
109- offset_y = crop_resize - crop_size
110- offset_x = offset_y * aspect_ratio
111108 else :
112109 if width > crop_size :
113110 crop_resize = min (image .width , width )
114111 else :
115112 crop_resize = crop_size
116- offset_x = crop_resize - crop_size
117- offset_y = offset_x / aspect_ratio
113+
114+ # calculate the cropped area in image coordinates
115+ if aspect_ratio > 1 :
116+ if aspect_ratio > orig_aspect_ratio :
117+ min_width = max (min (crop_size * aspect_ratio , image .width ), width )
118+ min_height = max (min_width / aspect_ratio , height )
119+ else :
120+ min_width = max (min (crop_size , image .height ) * aspect_ratio , width )
121+ min_height = max (min_width / aspect_ratio , height )
122+ else :
123+ if aspect_ratio < orig_aspect_ratio :
124+ min_height = max (min (crop_size / aspect_ratio , image .height ), height )
125+ min_width = max (min_height * aspect_ratio , width )
126+ else :
127+ min_height = max (min (crop_size , image .width ) / aspect_ratio , height )
128+ min_width = max (min_height * aspect_ratio , width )
118129
119130 # extend the horizontal crop size to prevent blurry images
120131 if gravity in ('e' , 'ne' , 'se' ):
121- if crop_resize >= crop_size :
122- crop_x # += (width - crop_resize) / 2
123- elif aspect_ratio > 1 :
124- if aspect_ratio > orig_aspect_ratio :
125- crop_x = max (crop_x - crop_resize * (aspect_ratio - 1 ), 0 )
126- else :
127- crop_x = max (crop_x + crop_size - image .height * aspect_ratio , 0 )
132+ # if crop_resize > crop_size:
133+ # crop_x # += (width - crop_resize) / 2
134+ # elif aspect_ratio > 1:
135+ # if aspect_ratio > orig_aspect_ratio:
136+ # crop_x = max(crop_x - crop_resize * (aspect_ratio - 1), 0)
137+ # if aspect_ratio < 1:
138+ # if aspect_ratio < orig_aspect_ratio:
139+ # crop_x = max(crop_x + crop_size - image.height * aspect_ratio, 0)
140+ # else:
141+ # crop_x = max(crop_x + crop_resize - min_width, 0)
142+ # minax = max if aspect_ratio > 1 else min
143+ crop_x = max (crop_x + max (crop_resize - min_width , 0 ), 0 )
128144 elif gravity in ('w' , 'nw' , 'sw' ):
129- if crop_resize >= crop_size :
130- crop_x = max (crop_x + crop_size - width , 0 )
131- elif aspect_ratio > 1 :
132- crop_x = max (crop_x - crop_resize * ( aspect_ratio - 1 ), 0 )
145+ # if crop_resize > crop_size:
146+ # crop_x = max(crop_x + crop_size - width, 0)
147+ # elif aspect_ratio > 1:
148+ crop_x = max (crop_x - max ( min ( min_width - crop_size , min_width ), 0 ), 0 )
133149 else : # centered crop
134- if aspect_ratio < orig_aspect_ratio and crop_resize / aspect_ratio > image .height :
135- crop_x = max (crop_x + (crop_size - image .height * aspect_ratio ) / 2 , 0 )
136- elif False and aspect_ratio > orig_aspect_ratio and crop_resize * aspect_ratio < image .width :
137- crop_x = max (crop_x - crop_resize * (aspect_ratio - 1 ) / 2 , 0 )
138- elif crop_resize > crop_size :
139- crop_x = max (crop_x - offset_x / 2 , 0 )
150+ # if crop_resize > crop_size:
151+ # crop_x = max(crop_x - max(aspect_ratio, 1) * (crop_resize - crop_size) / 2, 0)
152+ # else:
153+ crop_x = max (crop_x - (min_width - crop_size ) / 2 , 0 )
154+
155+ # if aspect_ratio < orig_aspect_ratio and crop_resize / aspect_ratio > image.height:
156+ # crop_x = max(crop_x + (crop_size - image.height * aspect_ratio) / 2, 0)
157+ # elif False and aspect_ratio > orig_aspect_ratio and crop_resize * aspect_ratio < image.width:
158+ # crop_x = max(crop_x - crop_resize * (aspect_ratio - 1) / 2, 0)
159+ # elif crop_resize > crop_size:
160+ # crop_x = max(crop_x - offset_x / 2, 0)
140161
141162 # extend the vertical crop size to prevent blurry images
142- if gravity in ('n' , 'ne' , 'nw' ):
143- if crop_resize >= crop_size :
144- crop_y = max (crop_y + crop_size - height , 0 )
145- elif aspect_ratio < 1 :
146- crop_y = max (crop_y + crop_size * (1 - 1 / aspect_ratio ), 0 )
147- elif gravity in ('s' , 'se' , 'sw' ):
148- if crop_resize >= crop_size :
149- crop_y # += (height - crop_resize) / 2
150- elif aspect_ratio < 1 :
151- if aspect_ratio < orig_aspect_ratio :
152- crop_y = max (crop_y + crop_resize * (1 - 1 / aspect_ratio ), 0 )
153- else :
154- crop_y
155- else :
156- crop_y = max (crop_y + (crop_size - image .width / aspect_ratio ), 0 )
163+ if gravity in ('s' , 'se' , 'sw' ):
164+ # if aspect_ratio > 1:
165+ # if aspect_ratio > orig_aspect_ratio:
166+ # crop_y = max(crop_y + crop_size - image.width / aspect_ratio, 0)
167+ # else:
168+ # crop_y = max(crop_y + min_height - crop_resize, 0)
169+
170+ # if crop_resize > crop_size:
171+ # crop_y = max(crop_y + crop_size - height, 0)
172+ # elif aspect_ratio < 1:
173+ # crop_y = max(crop_y + crop_resize - min_height, 0)
174+ crop_y = max (crop_y + max (crop_resize - min_height , 0 ), 0 )
175+ elif gravity in ('n' , 'ne' , 'nw' ):
176+ # if crop_resize >= crop_size:
177+ # crop_y # += (height - crop_resize) / 2
178+ # elif aspect_ratio < 1:
179+ # if aspect_ratio < orig_aspect_ratio:
180+ # crop_y = max(crop_y + crop_resize * (1 - 1 / aspect_ratio), 0)
181+ # else:
182+ # crop_y
183+ # else:
184+ # crop_y = max(crop_y + (crop_size - image.width / aspect_ratio), 0)
185+ crop_y = max (crop_y - max (min (min_height - crop_size , min_height ), 0 ), 0 )
157186 else : # centered crop
187+ # if crop_resize > crop_size:
188+ # crop_y = max(crop_y - (crop_resize - crop_size) / min(aspect_ratio, 1) / 2, 0)
189+ # else:
190+ crop_y = max (crop_y - (min_height - crop_size ) / 2 , 0 )
191+
158192 # if aspect_ratio < orig_aspect_ratio and crop_resize / aspect_ratio > image.height:
159193 # crop_x = max(crop_x + (crop_size - image.height * aspect_ratio) / 2, 0)
160194 # elif aspect_ratio > orig_aspect_ratio and crop_resize * aspect_ratio < image.width:
161195 # crop_x = max(crop_x - crop_resize * (aspect_ratio - 1) / 2, 0)
162- if aspect_ratio > orig_aspect_ratio and crop_resize * aspect_ratio > image .width :
163- crop_y = max (crop_y + crop_resize * (1 - 1 / aspect_ratio ) / 2 , 0 )
164- elif False and aspect_ratio < orig_aspect_ratio and crop_resize / aspect_ratio < image .height :
165- crop_y = max (crop_y + (crop_size - image .width / aspect_ratio ) / 2 , 0 )
166- elif crop_resize > crop_size :
167- crop_y = max (crop_y - offset_y / 2 , 0 )
168-
169-
196+ # if aspect_ratio > orig_aspect_ratio and crop_resize * aspect_ratio > image.width:
197+ # crop_y = max(crop_y + crop_resize * (1 - 1 / aspect_ratio) / 2, 0)
198+ # elif False and aspect_ratio < orig_aspect_ratio and crop_resize / aspect_ratio < image.height:
199+ # crop_y = max(crop_y + (crop_size - image.width / aspect_ratio) / 2, 0)
200+ # elif crop_resize > crop_size:
201+ # crop_y = max(crop_y - offset_y / 2, 0)
170202 crop_size = crop_resize
171203
172204 print (f"Crop parameters: crop_x={ crop_x } , crop_y={ crop_y } , crop_size={ crop_size } , gravity={ gravity } " )
173205
174- if aspect_ratio > 1 :
175- if aspect_ratio > orig_aspect_ratio :
176- min_width = max (min (crop_size * aspect_ratio , image .width ), width )
177- min_height = max (min_width / aspect_ratio , height )
178- else :
179- min_width = max (min (crop_size , image .height * aspect_ratio ), width )
180- min_height = max (min_width / aspect_ratio , height )
181- else :
182- if aspect_ratio < orig_aspect_ratio :
183- min_height = max (min (crop_size / aspect_ratio , image .height ), height )
184- min_width = max (min_height * aspect_ratio , width )
185- # min_height = max(image.height, height)
186- # min_width = max(min_height * aspect_ratio, height)
187- # min_height = max(min(crop_size, image.width / aspect_ratio), height)
188- # min_width = max(min_height * aspect_ratio, width)
189- else :
190- min_width = max (min (crop_size , image .height * aspect_ratio ), width )
191- min_height = max (min_width / aspect_ratio , height )
192206 if False :
193207 min_x = max (crop_x + (crop_size - min_width ) / 2 , 0 )
194208 else :
@@ -281,6 +295,7 @@ def crop(self, thumbnail_path, width, height):
281295 image .thumbnail ((width , height ))
282296 (default_storage .base_location / thumbnail_path .parent ).mkdir (parents = True , exist_ok = True )
283297 image .save (default_storage .open (thumbnail_path , 'wb' ), image .format )
298+ return image
284299
285300 def crop_centered (self , image ):
286301 width , height = image .size
0 commit comments