-
Notifications
You must be signed in to change notification settings - Fork 12.5k
/
Copy pathimages.py
45 lines (32 loc) · 1.46 KB
/
images.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
import os
from PIL import Image
from fpdf import FPDF
# Author: @NavonilDas
# Example to Append all the images inside a folder to pdf
pdf = FPDF()
# Size of a A4 Page in mm Where P is for Potrait and L is for Landscape
A4_SIZE = {"P": {"w": 210, "h": 297}, "L": {"w": 297, "h": 210}}
# pdf may produce empty page so we need to set auto page break as false
pdf.set_auto_page_break(0)
for filename in os.listdir("images"):
try:
# Read Image file so that we can cover the complete image properly and if invalid image file skip those files
img = Image.open("images\\" + filename)
# Read Width and Height
width, height = img.size
# Close opened Image
img.close()
# Convert Width and Height into mm from px as 1px = 0.2645833333 mm
width, height = float(width * 0.264583), float(height * 0.264583)
# Check if Width is greater than height so to know the image is in landscape or else in potrait
orientation = "P" if width < height else "L"
# Read the minimum of A4 Size and the image size
width = min(A4_SIZE[orientation]["w"], width)
height = min(A4_SIZE[orientation]["h"], height)
# Add Page With an orientation
pdf.add_page(orientation=orientation)
# Add Image with their respective width and height in mm
pdf.image("images\\" + filename, 0, 0, width, height)
except OSError:
print("Skipped : " + filename)
pdf.output("output.pdf", "F")