Created
April 19, 2021 12:06
-
-
Save rasjani/e8d10b3d2e8f71724b19db3f56ef8ca5 to your computer and use it in GitHub Desktop.
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 re | |
from PIL import Image | |
from natsort import natsorted | |
from pathlib import Path | |
import subprocess | |
# convert 1-2.png -crop 2x1@ out-%d.png | |
# convert etukansi.png -background white -page a5 etu.pdf | |
matcher = "Pohjolan Leijona (?P<season>\d+) vuosikerta nro.(?P<issue>\d+) (?P<year>\d+)" | |
cwd = Path.cwd() | |
for d in Path(".").glob("Pohjolan*"): | |
matches = re.match(matcher, str(d)) | |
season = matches.group("season") | |
issue = matches.group("issue") | |
year = matches.group("year") | |
new_dir = cwd / f"{year}.{season}" / f"{issue}" | |
if not new_dir.exists(): | |
new_dir.mkdir(parents=True) | |
tmp_pages = d.glob("*.png") | |
# tmp_pages = filter(lambda x: "kansi" not in str(x), tmp_pages) | |
pages = natsorted(tmp_pages, key=lambda i: str(i)) | |
print(new_dir) | |
page = 1 | |
for x in pages: | |
with Image.open(x) as im: | |
width, height = im.size | |
if width > 1200: | |
cmd = ["convert", str(x), "-crop", "2x1@", str(new_dir / "out-%d.png")] | |
subprocess.check_call(cmd) | |
for c in [0,1]: | |
tmp_file = new_dir / f"out-{c}.png" | |
to_pdf = tmp_file.replace( new_dir / f"page_{page + c}.png") | |
pdf_cmd = ["convert", str(to_pdf), "-background", "white", "-page", "a5", f"{new_dir}/{to_pdf.stem}.pdf"] | |
subprocess.check_call(pdf_cmd) | |
to_pdf.unlink() | |
page += 2 | |
else: | |
to_pdf = None | |
if "etu" in str(x): | |
to_pdf = new_dir / "page_0_fc.pdf" | |
elif "taka" in str(x): | |
to_pdf = new_dir / f"page_{page}_bc.pdf" | |
else: | |
to_pdf = new_dir / f"page_{page}_xxx.odf" | |
page += 1 | |
pdf_cmd = ["convert", str(x), "-background", "white", "-page", "a5", f"{to_pdf}"] | |
subprocess.check_call(pdf_cmd) | |
print(f"\t {x} {width}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment