@@ -17,18 +17,23 @@ def resize_image(input_path, output_path=None, width=720, quality=80):
1717 then save it as a compressed JPEG for web use.
1818 """
1919 img = Image.open(input_path)
20- w_percent = width / float (img.width)
21- height = int (img.height * w_percent)
2220
23- resized = img.resize((width, height), Image.LANCZOS )
24- if resized.mode in (" RGBA" , " P" ):
25- resized = resized.convert(" RGB" )
21+ if img.width > width:
22+ w_percent = width / float (img.width)
23+ height = int (img.height * w_percent)
24+ img = img.resize((width, height), Image.LANCZOS )
25+ print (f " Resized down to { width} x { height} " )
26+ else :
27+ print (f " Skipping resize: image width ( { img.width} px) <= target width ( { width} px) " )
28+
29+ if img.mode in (" RGBA" , " P" ):
30+ img = img.convert(" RGB" )
2631
2732 if output_path is None :
2833 base, _ = os.path.splitext(input_path)
2934 output_path = f " { base} _resized.jpg "
3035
31- resized .save(output_path, " JPEG" , quality = quality, optimize = True )
36+ img .save(output_path, " JPEG" , quality = quality, optimize = True )
3237 print (f " Saved: { output_path} ( { width} × { height} , quality= { quality} %) " )
3338
3439if __name__ == " __main__" :
0 commit comments