Convert a PNG image to a PDF file. Args: input_path: Path to the source .png file. output_path: Path where the resulting .pdf will be saved.
(input_path: str, output_path: str)
| 997 | return new_data |
| 998 | |
| 999 | def png_to_pdf(input_path: str, output_path: str) -> None: |
| 1000 | """ |
| 1001 | Convert a PNG image to a PDF file. |
| 1002 | |
| 1003 | Args: |
| 1004 | input_path: Path to the source .png file. |
| 1005 | output_path: Path where the resulting .pdf will be saved. |
| 1006 | """ |
| 1007 | with Image.open(input_path) as img: |
| 1008 | # Convert image to RGB if it has an alpha channel |
| 1009 | if img.mode in ("RGBA", "LA") or (img.mode == "P" and "transparency" in img.info): |
| 1010 | background = Image.new("RGB", img.size, (255, 255, 255)) |
| 1011 | if img.mode != "RGBA": |
| 1012 | img = img.convert("RGBA") |
| 1013 | background.paste(img, mask=img.split()[-1]) # use alpha channel as mask |
| 1014 | img = background |
| 1015 | else: |
| 1016 | img = img.convert("RGB") |
| 1017 | |
| 1018 | img.save(output_path, "PDF", resolution=200.0) |
| 1019 | |
| 1020 | def extract_images_and_sections(md): |
| 1021 | parts = re.split(r'(## [^\n]+)', md) |
no test coverage detected