Created
September 12, 2021 00:08
-
-
Save dmgig/24eb15703469b37d5cdbd7567cfa2966 to your computer and use it in GitHub Desktop.
Images to PDF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python3 | |
import os | |
import sys | |
import shutil | |
import glob | |
from PIL import Image, ImageEnhance | |
import re | |
from fpdf import FPDF | |
origPath = 'orig' | |
finalPath = 'final' | |
split = 454 | |
THRESH = 125 | |
CONTRAST = 1.4 | |
if(os.path.exists(finalPath)): | |
shutil.rmtree(finalPath) | |
os.mkdir(finalPath) | |
imgs = glob.glob(origPath+'/*.JPG') | |
for img in imgs: | |
basename = os.path.basename(img) | |
print(basename) | |
imgId = re.search('[0-9]{4}', basename) | |
imgId = int(imgId.group(0)) | |
print(imgId) | |
workingImg = Image.open(img) | |
workingImg = workingImg.rotate(271, expand=True) | |
workingImg = workingImg.crop((607, 855, 2726, 3646)) | |
width, height = workingImg.size | |
newsize = (1000, int(1000 * (height/width))) | |
workingImg = workingImg.resize(newsize) | |
width, height = workingImg.size | |
workingImg = ImageEnhance.Contrast(workingImg).enhance(CONTRAST) | |
# workingImg = workingImg.convert('L') | |
def fn(x): return 255 if x > THRESH else 0 | |
workingImg = workingImg.convert('L').point(fn, mode='1') | |
finalBasename = basename | |
if(imgId >= split): | |
newPage = str(imgId - 38) | |
finalBasename = 'IMG_'+newPage+'a.JPG' | |
workingImg.save(finalPath+'/'+finalBasename, dpi=(150, 150)) | |
pdf = FPDF(unit="pt", format=[width, height]) | |
imgs = glob.glob(finalPath+'/*.JPG') | |
imgs.sort() | |
for img in imgs: | |
pdf.add_page() | |
pdf.image(img, 0, 0, width, height) | |
pdf.output( | |
"October-Surprise-Decide-For-Yourself-Prevailing-Winds-Research.pdf", "F") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment