@@ -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 ) == '4d4d9ae9-3120-4f24-8d57-9d15fa6edfc4 ' or not default_storage .exists (thumbnail_path ):
48+ if str (self .id ) == 'a118a297-0931-4ee9-95c7-925624d3b7a3 ' or not default_storage .exists (thumbnail_path ):
4949 try :
50- self .crop (thumbnail_path , self .thumbnail_size , self .thumbnail_size * 0.6 )
50+ self .crop (thumbnail_path , self .thumbnail_size , self .thumbnail_size * 2.5 )
5151 except Exception :
5252 # thumbnail image could not be created
5353 return self .fallback_thumbnail_url
@@ -94,6 +94,7 @@ 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
9798 # elif width > height:
9899 # if width > crop_size * aspect_ratio:
99100 # # extend the crop size to prevent blurry images
@@ -102,88 +103,97 @@ def crop(self, thumbnail_path, width, height):
102103 # crop_size = width
103104 elif aspect_ratio > 1 :
104105 if height > crop_size :
105- crop_resize = min (image .height , width )
106+ crop_resize = min (image .height , height )
106107 else :
107108 crop_resize = crop_size
109+ offset_y = crop_resize - crop_size
110+ offset_x = offset_y * aspect_ratio
108111 else :
109112 if width > crop_size :
110- crop_resize = min (image .width , height )
113+ crop_resize = min (image .width , width )
111114 else :
112115 crop_resize = crop_size
116+ offset_x = crop_resize - crop_size
117+ offset_y = offset_x / aspect_ratio
113118
114119 # extend the horizontal crop size to prevent blurry images
115120 if gravity in ('e' , 'ne' , 'se' ):
116- if crop_resize > crop_size :
117- crop_x += (width - crop_resize ) / 2
121+ if crop_resize >= crop_size :
122+ crop_x # += (width - crop_resize) / 2
118123 elif aspect_ratio > 1 :
119124 if aspect_ratio > orig_aspect_ratio :
120125 crop_x = max (crop_x - crop_resize * (aspect_ratio - 1 ), 0 )
121126 else :
122127 crop_x = max (crop_x + crop_size - image .height * aspect_ratio , 0 )
123128 elif gravity in ('w' , 'nw' , 'sw' ):
124- if crop_resize > crop_size :
125- crop_x = max (crop_x - width + crop_size , 0 )
129+ if crop_resize >= crop_size :
130+ crop_x = max (crop_x + crop_size - width , 0 )
126131 elif aspect_ratio > 1 :
127132 crop_x = max (crop_x - crop_resize * (aspect_ratio - 1 ), 0 )
128133 else : # centered crop
129- if crop_resize > crop_size :
130- crop_x = max (crop_x - (crop_resize - crop_size ) / 2 , 0 )
131- elif aspect_ratio > 1 :
132- if aspect_ratio > orig_aspect_ratio :
133- crop_x = max (crop_x - crop_resize * (aspect_ratio - 1 ) / 2 , 0 )
134- else :
134+ if aspect_ratio < orig_aspect_ratio and crop_resize / aspect_ratio > image .height :
135135 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 )
136140
137141 # extend the vertical crop size to prevent blurry images
138142 if gravity in ('n' , 'ne' , 'nw' ):
139- if crop_resize > crop_size :
140- crop_y = max (crop_y - height + crop_size , 0 )
143+ if crop_resize >= crop_size :
144+ crop_y = max (crop_y + crop_size - height , 0 )
141145 elif aspect_ratio < 1 :
142146 crop_y = max (crop_y + crop_size * (1 - 1 / aspect_ratio ), 0 )
143147 elif gravity in ('s' , 'se' , 'sw' ):
144- if crop_resize > crop_size :
145- crop_y += (height - crop_resize ) / 2
148+ if crop_resize >= crop_size :
149+ crop_y # += (height - crop_resize) / 2
146150 elif aspect_ratio < 1 :
147151 if aspect_ratio < orig_aspect_ratio :
148152 crop_y = max (crop_y + crop_resize * (1 - 1 / aspect_ratio ), 0 )
153+ else :
154+ crop_y
149155 else :
150156 crop_y = max (crop_y + (crop_size - image .width / aspect_ratio ), 0 )
151- else :
152- if crop_resize > crop_size :
153- # if aspect_ratio < 1:
154- crop_y = max ( crop_y - ( height - crop_size ) / 2 , 0 )
155- elif aspect_ratio < 1 :
156- if aspect_ratio < orig_aspect_ratio :
157- crop_y = max (crop_y + crop_resize * (1 - 1 / aspect_ratio ) / 2 , 0 )
158- else :
157+ else : # centered crop
158+ # if aspect_ratio < orig_aspect_ratio and crop_resize / aspect_ratio > image.height :
159+ # crop_x = max(crop_x + (crop_size - image.height * aspect_ratio) / 2, 0)
160+ # elif aspect_ratio > orig_aspect_ratio and crop_resize * aspect_ratio < image.width:
161+ # 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 :
159165 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+
160169
161170 crop_size = crop_resize
162171
163172 print (f"Crop parameters: crop_x={ crop_x } , crop_y={ crop_y } , crop_size={ crop_size } , gravity={ gravity } " )
164173
165- if orig_aspect_ratio > aspect_ratio :
166- # if image.width / crop_size > aspect_ratio:
167- # # we can't fill the thumbnailed image with the cropped part
168- # min_width = min(crop_size, image.height * aspect_ratio)
169- # else:
170- # min_width = image.height * aspect_ratio
171- # min_height = min(crop_size, image.height)
172- min_width = min (crop_size , image .height * aspect_ratio )
173- min_height = min_width / aspect_ratio
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 )
174181 else :
175- # min_width = max(crop_size, image.width)
176- # if image.height / crop_size < aspect_ratio:
177- # # we can't fill the thumbnailed image with the cropped part
178- # min_height = min(crop_size, image.width / aspect_ratio)
179- # else:
180- # min_height = image.width / aspect_ratio
181- min_height = min (crop_size , image .width / aspect_ratio )
182- min_width = min_height * aspect_ratio
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 )
183192 if False :
184193 min_x = max (crop_x + (crop_size - min_width ) / 2 , 0 )
185194 else :
186195 min_x = crop_x
196+
187197 if min_x + min_width > image .width :
188198 min_x = max (image .width - min_width , 0 )
189199 max_x = image .width
0 commit comments