import os import datetime from pdf2image import convert_from_path from PIL import Image startYear = 2012 folder_path = '/var/www/irdept/public_html/ir01/media_admin/00372/' current_year = datetime.datetime.now().year for year in range(startYear, current_year): pdf_path = folder_path + str(year) + "/" img_path = pdf_path + "img" if not os.path.exists(img_path): os.makedirs(img_path) for filename in os.listdir(pdf_path): pdfFile = pdf_path + filename fileNameOnly, extension = os.path.splitext(os.path.basename(pdfFile)) imageFile = pdf_path + "img/" + fileNameOnly + ".png" if (extension != ".pdf" and extension != ".PDF"): continue else: # Open the PDF file print(pdfFile) images = convert_from_path(pdfFile, dpi=300) image = images[0] image = image.convert('RGB') # convert to RGB format if required image.save(imageFile, 'PNG') # save the image as JPEG file