25
25
ThumbnailJob ,
26
26
ThumbnailSourceJob ,
27
27
)
28
+ from .pillow_resize_and_crop import transform_image
28
29
29
30
logger = logging .getLogger ("bma_client" )
30
31
@@ -248,7 +249,7 @@ def write_and_upload_result(self, job: Job, result: "JobResult", filename: str)
248
249
metadata : dict [str , int | str ] = {}
249
250
if isinstance (job , ImageConversionJob | ThumbnailJob ):
250
251
image , exif = result
251
- if not isinstance (image , Image .Image ) or not isinstance (exif , Image .Exif ):
252
+ if not isinstance (image [ 0 ] , Image .Image ) or not isinstance (exif , Image .Exif ):
252
253
raise TypeError ("Fuck" )
253
254
# apply format specific encoding options
254
255
kwargs = {}
@@ -258,18 +259,30 @@ def write_and_upload_result(self, job: Job, result: "JobResult", filename: str)
258
259
logger .debug (f"Format { job .mimetype } has custom encoding settings, kwargs is now: { kwargs } " )
259
260
else :
260
261
logger .debug (f"No custom settings for format { job .mimetype } " )
261
- image .save (buf , format = job .filetype , exif = exif , ** kwargs )
262
+ # sequence?
263
+ if len (image ) > 1 :
264
+ kwargs ["append_images" ] = image [1 :]
265
+ kwargs ["save_all" ] = True
266
+ image [0 ].save (buf , format = job .filetype , exif = exif , ** kwargs )
262
267
263
268
elif isinstance (job , ImageExifExtractionJob ):
264
269
logger .debug (f"Got exif data { result } " )
265
270
buf .write (json .dumps (result ).encode ())
266
271
267
272
elif isinstance (job , ThumbnailSourceJob ):
268
273
image , exif = result
269
- if not isinstance (image , Image .Image ) or not isinstance (exif , Image .Exif ):
274
+ if not isinstance (image [ 0 ] , Image .Image ) or not isinstance (exif , Image .Exif ):
270
275
raise TypeError ("Fuck" )
271
- image .save (buf , format = "WEBP" , lossless = True , quality = 1 )
272
- metadata = {"width" : 500 , "height" : image .height , "mimetype" : "image/webp" }
276
+ kwargs = {}
277
+ # thumbnailsources are always WEBP
278
+ if "image/webp" in self .settings ["encoding" ]["images" ]:
279
+ kwargs .update (self .settings ["encoding" ]["images" ]["image/webp" ])
280
+ # sequence?
281
+ if len (image ) > 1 :
282
+ kwargs ["append_images" ] = image [1 :]
283
+ kwargs ["save_all" ] = True
284
+ image [0 ].save (buf , format = "WEBP" , ** kwargs )
285
+ metadata = {"width" : 500 , "height" : image [0 ].height , "mimetype" : "image/webp" }
273
286
274
287
else :
275
288
logger .error ("Unsupported job type" )
@@ -315,15 +328,11 @@ def handle_image_conversion_job(
315
328
316
329
logger .debug (f"Desired image size is { size } , aspect ratio: { ratio } ({ orig_str } ), converting image..." )
317
330
start = time .time ()
318
- # custom AR or not?
319
- if job .custom_aspect_ratio :
320
- image = ImageOps .fit (image = image , size = size , method = Image .Resampling .LANCZOS , centering = crop_center ) # type: ignore[assignment]
321
- else :
322
- image .thumbnail (size = size , resample = Image .Resampling .LANCZOS )
331
+ images = transform_image (original_img = image , crop_w = size [0 ], crop_h = size [1 ])
323
332
logger .debug (f"Converting image size and AR took { time .time () - start } seconds" )
324
333
325
334
logger .debug ("Done, returning result..." )
326
- return image , exif
335
+ return images , exif
327
336
328
337
def upload_job_result (
329
338
self ,
0 commit comments