Skip to content

Commit

Permalink
Add auto-rotate page
Browse files Browse the repository at this point in the history
If the image is in landscape mode, the page that contains the image will now automatically rotate to landscape
  • Loading branch information
Jangsoodlor committed Mar 30, 2022
1 parent 5db15a5 commit a143e0a
Showing 1 changed file with 33 additions and 25 deletions.
58 changes: 33 additions & 25 deletions JPG2PDF.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,34 @@
from fpdf import FPDF
from PIL import Image
import os
import natsort


while True:
# --------------- USER INPUT -------------------- #

folder = str(input("insert your picture's folder directory: ")) # Folder containing all the images.
name = str(input("insert your desired document name: "))+".pdf" # Name of the output PDF file.
moonmai = input("Do you want to auto-rotate page that contains landscape picture to a landscape page(Y/N)?: ") or 'y'
# moon = str(input("Do you want to auto-rotate landscape pictures to portrait?(Y/N)?: ")) or 'n'
size = input("insert the paper size: ") or 'a4'


moon = str(moonmai)

# ---------------FPDF--------------------------------#
pdf = FPDF(format = f'{size}')
imagelist = [] # Contains the list of all images to be converted to PDF.

# --------------- DEFINE OUTPUT PAPER SIZE --------------------#

if size == "A4" or size == "a4":
if size == "A3" or size == "a3":
x = int(297)
y = int (420)
elif size == "A4" or size == "a4":
x = int(210)
y = int(297)
elif size == "A5" or size == "a5":
x = int(148)
y = int (210)
elif size == "A3" or size == "a3":
x = int(297)
y = int (420)
y = int (210)


# ------------- ADD ALL THE IMAGES IN A LIST ------------- #
Expand All @@ -44,27 +44,35 @@
print(imagelist[i])

# --------------- ROTATE ANY LANDSCAPE MODE IMAGE IF PRESENT ----------------- #
if moon == 'y' or moon == 'Y':
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:
print("rotating picture")
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


# if moon == 'y' or moon == 'Y':
# for i in range(0, len(imagelist)):
# im1 = Image.open(imagelist[i]) # Open the image.
# print(im1)
# width, height = im1.size # Get the width and height of that image.
# if width > height:
# print("rotating picture")
# 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.

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, x, y)

for i in range(0, len(imagelist)):
im1 = Image.open(imagelist[i]) # Open the image.
print(im1)
width, height = im1.size # Get the width and height of that image.
if width > height:
pdf.add_page('L')
pdf.image(imagelist[i], 0, 0, y, x)
if width < height:
pdf.add_page('P')
pdf.image(imagelist[i], 0, 0, x, y)

# for image in imagelist:
# pdf.add_page()
# pdf.image(image, 0, 0, 297, 210)

pdf.output(folder + name, "F") # Save the PDF.

print("PDF generated successfully!")
Expand Down

0 comments on commit a143e0a

Please sign in to comment.