1+ import logging
12from reportlab .pdfgen import canvas
23from reportlab .lib .pagesizes import A4
3- from PIL import Image
4+ from PIL import Image , UnidentifiedImageError
45import math
56import bisect
67from time import time
78import os
89
10+ logger = logging .getLogger (__name__ )
11+
12+
913
1014
1115class VirtualImage :
@@ -127,26 +131,29 @@ def cm_to_points(cm):
127131 return inches * 72
128132
129133def resize_image (image_path , max_width_points , max_height_points ):
130- with Image . open ( image_path ) as img :
131-
132- rotated = False
133- width = img .width
134- height = img .height
135-
136- if (width - height ) * (max_width_points - max_height_points ) < 0 :
137- rotated = True
138- width = img .height
139- height = img .width
140-
141- img_ratio = width / height
142- if img_ratio > max_width_points / max_height_points :
143- new_width = min (width , max_width_points )
144- new_height = int (new_width / img_ratio )
145- else :
146- new_height = min (height , max_height_points )
147- new_width = int (new_height * img_ratio )
134+ try :
135+ with Image . open ( image_path ) as img :
136+ rotated = False
137+ width = img .width
138+ height = img .height
139+
140+ if (width - height ) * (max_width_points - max_height_points ) < 0 :
141+ rotated = True
142+ width = img .height
143+ height = img .width
144+
145+ img_ratio = width / height
146+ if img_ratio > max_width_points / max_height_points :
147+ new_width = min (width , max_width_points )
148+ new_height = int (new_width / img_ratio )
149+ else :
150+ new_height = min (height , max_height_points )
151+ new_width = int (new_height * img_ratio )
148152
149- return VirtualImage (image_path , new_width , new_height , rotated )
153+ return VirtualImage (image_path , new_width , new_height , rotated )
154+ except UnidentifiedImageError :
155+ logger .warning (f'The file { image_path } could not be identified as an image.' )
156+ return None
150157
151158def collect_and_resize_images (directory , max_width_cm , max_height_cm ):
152159 max_width_points = cm_to_points (max_width_cm )
@@ -155,11 +162,11 @@ def collect_and_resize_images(directory, max_width_cm, max_height_cm):
155162 min_size = min (max_width_points , max_height_points )
156163 for root , _ , files in os .walk (directory ):
157164 for filename in files :
158- if filename .lower ().endswith (('png' , 'jpg' , 'jpeg' , 'gif' , 'bmp' )):
159165 path = os .path .join (root , filename )
160166 image = resize_image (path , max_width_points , max_height_points )
161- min_size = min (min_size , image .width , image .height )
162- images .append (image )
167+ if image is not None :
168+ min_size = min (min_size , image .width , image .height )
169+ images .append (image )
163170 images .sort (reverse = True )
164171 return images , min_size
165172
@@ -294,12 +301,10 @@ def place_images_on_pdf(images, output_pdf_path, margin, min_size, progress_call
294301
295302
296303if __name__ == "__main__" :
297- #directory = "/Users/betty/PythonProjects/image_resize/photos"
298- directory = "/Users/betty/PythonProjects/image_resize/bat-mizvush-all"
299- #directory = "/Users/betty/PythonProjects/image_resize/bat-mizvush-6"
304+ directory = "/Users/betty/projects/2025-03-25/vetochka/images"
300305 max_width_cm , max_height_cm = 10 , 15.5
301306 # output_pdf_path = "/Users/betty/PythonProjects/image_resize/output_images.pdf"
302- output_pdf_path = "/Users/betty/PythonProjects/image_resize/output_images .pdf"
307+ output_pdf_path = "/Users/betty/projects/2025-03-25/vetochka/images/output_imagess .pdf"
303308 start_time = time ()
304309 images , min_size = collect_and_resize_images (directory , max_width_cm , max_height_cm )
305310 place_images_on_pdf (images , output_pdf_path , cm_to_points (0.2 ), min_size )
0 commit comments