-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathJPG to PDF.py
48 lines (32 loc) · 1.9 KB
/
JPG to PDF.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from fpdf import FPDF
from PIL import Image
import os
pdf = FPDF()
imagelist = [] # Contains the list of all images to be converted to PDF.
# --------------- USER INPUT -------------------- #
folder = "" # Folder containing all the images.
name = "" # Name of the output PDF file.
# ------------- ADD ALL THE IMAGES IN A LIST ------------- #
for dirpath, dirnames, filenames in os.walk(folder):
for filename in [f for f in filenames if f.endswith(".jpg")]:
full_path = os.path.join(dirpath, filename)
imagelist.append(full_path)
imagelist.sort() # Sort the images by name.
for i in range(0, len(imagelist)):
print(imagelist[i])
# --------------- ROTATE ANY LANDSCAPE MODE IMAGE IF PRESENT ----------------- #
for i in range(0, len(imagelist)):
im1 = Image.open(imagelist[i]) # Open the image.
width, height = im1.size # Get the width and height of that image.
if width > height:
im2 = im1.transpose(Image.ROTATE_270) # If width > height, rotate the image.
os.remove(imagelist[i]) # Delete the previous image.
im2.save(imagelist[i]) # Save the rotated image.
# im.save
print("\nFound " + str(len(imagelist)) + " image files. Converting to PDF....\n")
# -------------- CONVERT TO PDF ------------ #
for image in imagelist:
pdf.add_page()
pdf.image(image, 0, 0, 210, 297) # 210 and 297 are the dimensions of an A4 size sheet.
pdf.output(folder + name, "F") # Save the PDF.
print("PDF generated successfully!")