Created
December 2, 2019 22:54
-
-
Save RouNNdeL/e927450858c16a99cc0f0321988fe7fc to your computer and use it in GitHub Desktop.
Ugly script to generate B&W PNGs from a 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
import glob, os | |
import pdf2image | |
from PIL import Image, ImageEnhance | |
import PIL.ImageOps | |
file = input("File:") | |
dpi = int(input("DPI:")) | |
i = 0; | |
dir = "d_"+file | |
if not os.path.exists(dir): | |
os.makedirs(dir) | |
maxPages = 100 | |
print("Extracting PNGs") | |
for _page in range(1,maxPages,2) : | |
pages = pdf2image.convert_from_path(file, dpi=dpi, first_page=_page, last_page = min(_page+2-1,maxPages)) | |
for page in pages: | |
i+=1 | |
page.save(os.path.join(dir, str(i)+".png"), 'PNG') | |
if not os.path.exists(dir): | |
os.makedirs(dir) | |
print("Increasing contrast and converting") | |
for file in os.listdir(dir): | |
if file.endswith(".png"): | |
img = Image.open(os.path.join(dir, file)).convert("RGB", colors=16) | |
enhancer = ImageEnhance.Contrast(img) | |
img = enhancer.enhance(10) | |
enhancer = ImageEnhance.Brightness(img) | |
img = enhancer.enhance(1.2) | |
img = img.convert("L") | |
dark_count = 0; | |
light_count = 0; | |
for pixel in img.getdata(): | |
if(pixel < 128): | |
dark_count += 1 | |
else: | |
light_count += 1 | |
if(dark_count > light_count): | |
img = PIL.ImageOps.invert(img) | |
print("Inverted: "+file); | |
img.save(os.path.join(dir, file),'png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment