Skip to content
Permalink
main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
from pathlib import Path
import sys
from PIL import Image
from PyPDF4 import PdfFileMerger, PdfFileReader, PdfFileWriter
from general_functions import try_remove
def png_2_pdf(path: Path):
"""
Converts all png files in path to PDFs
"""
for file in path.iterdir():
file = path / file
if file.suffix == ".png":
pdf_name = file.with_suffix(".pdf")
img_rgba = Image.open(file)
img_rgb = Image.new("RGB", img_rgba.size, (255, 255, 255))
img_rgb.paste(img_rgba, mask=img_rgba.split()[3])
img_rgb.save(pdf_name, "PDF", resolution=100)
def merge_pdfs(path: Path):
"""
Merge all PDF files in path into one results.pdf.
Rotates all files that start with 'x'. These are output of dotter and
need to be rotated for better readability.
Deletes all used PDF files afterwards.
"""
out_file = path / "results.pdf"
if out_file.is_file():
try_remove(out_file)
merger = PdfFileMerger()
delete_afterwards = []
for file_name in sorted(path.iterdir()):
file = path / file_name
if file.suffix == ".pdf" and file.stat().st_size > 0:
delete_afterwards.append(file)
if (fn:=file.name).startswith("x"):
#rotate (as dotter renders weird pdfs)
pdf_in = PdfFileReader(open(file, 'rb'))
pdf_writer = PdfFileWriter()
pdf_writer.addPage(pdf_in.getPage(0).rotateClockwise(90))
new_file_name = file.parent / fn.replace("x", "dotplot")
pdf_writer.write(open(new_file_name, "wb"))
# delete_afterwards.append(new_file_name)
file = new_file_name
merger.append(str(file))
merger.write(str(out_file))
merger.close()
for file in delete_afterwards:
try_remove(file)
if __name__ == "__main__":
main_dir = Path(sys.argv[1])
for conflict_dir in main_dir.iterdir():
conflict_dir = main_dir / conflict_dir
png_2_pdf(conflict_dir)
merge_pdfs(conflict_dir)