Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
# Python PDF to PNG
Converting a PDF to a PNG using wand, --pythonMagic-- and --PythonMagicWand-- as a comparison

I found that wand seems to be the only currently supported imagemagick library for python.

It was also found that jpeg results in better image output that PNG files. This could be dependant on the PDF, but it's worth a try if your output needs improvement.
# Python PDF to PNG/JPG
Converting a PDF to a PNG using wand, --pythonMagic-- and --PythonMagicWand-- as a comparison

I found that wand seems to be the only currently supported imagemagick library for python.

It was also found that jpeg results in better image output that PNG files. This could be dependant on the PDF, but it's worth a try if your output needs improvement.

## Setup
Clone the repository into your machine using `git clone https://github.com/nycynik/PythonPDFtoPNG.git` and extract the folder. Open the folder
in the terminal and follow these steps:

- Copy over the PDF file you want to convert to PNG/JPG inside the main folder as such so the PDF file is along side `pdfToPNGWand.py`
- In the terminal run `pip install -r requirements.txt` to install the required packages. If installing through pip doesn't work install wand
through [this link](https://docs.wand-py.org/en/latest/guide/install.html#install-imagemagick-on-windows)
- In the terminal run `python3 pdfToPNGWand.py` to start the program
- Follow the steps the program takes you through and the output files will be in the `/output` folder
Binary file added cmdout/s-0.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed cmdout/s-0.png
Binary file not shown.
Binary file added cmdout/s-1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed cmdout/s-1.png
Binary file not shown.
Binary file added cmdout/s-10.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed cmdout/s-10.png
Binary file not shown.
Binary file added cmdout/s-11.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed cmdout/s-11.png
Binary file not shown.
Binary file added cmdout/s-12.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed cmdout/s-12.png
Binary file not shown.
Binary file added cmdout/s-13.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed cmdout/s-13.png
Binary file not shown.
Binary file added cmdout/s-14.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed cmdout/s-14.png
Binary file not shown.
Binary file added cmdout/s-15.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed cmdout/s-15.png
Binary file not shown.
Binary file added cmdout/s-2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed cmdout/s-2.png
Binary file not shown.
Binary file added cmdout/s-3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed cmdout/s-3.png
Binary file not shown.
Binary file added cmdout/s-4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed cmdout/s-4.png
Binary file not shown.
Binary file added cmdout/s-5.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed cmdout/s-5.png
Binary file not shown.
Binary file added cmdout/s-6.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed cmdout/s-6.png
Diff not rendered.
Binary file added cmdout/s-7.jpg
Binary file removed cmdout/s-7.png
Diff not rendered.
Binary file added cmdout/s-8.jpg
Binary file removed cmdout/s-8.png
Diff not rendered.
Binary file added cmdout/s-9.jpg
Binary file removed cmdout/s-9.png
Diff not rendered.
Binary file added document.pdf
Binary file not shown.
Binary file added output/page-0.jpg
Binary file added output/page-1.jpg
Binary file added output/page-10.jpg
Binary file added output/page-11.jpg
Binary file added output/page-12.jpg
Binary file added output/page-13.jpg
Binary file added output/page-14.jpg
Binary file added output/page-15.jpg
Binary file added output/page-2.jpg
Binary file added output/page-3.jpg
Binary file added output/page-4.jpg
Binary file added output/page-5.jpg
Binary file added output/page-6.jpg
Binary file added output/page-7.jpg
Binary file added output/page-8.jpg
Binary file added output/page-9.jpg
79 changes: 72 additions & 7 deletions pdfToPNGWand.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,76 @@
from __future__ import print_function
from wand.image import Image
import os

with Image(filename='source.pdf') as img:
print('width =', img.width)
print('height =', img.height)
print('pages = ', len(img.sequence))
print('resolution = ', img.resolution)
program_directory = os.path.dirname(os.path.realpath(__file__))

with img.convert('png') as converted:
converted.save(filename='pyout/page.png')
def pdf2png(path_to_file, png_or_jpg, resolution):
if not os.path.exists('output'):
os.makedirs('output')

with Image(filename=path_to_file, resolution=resolution) as img:
with img.convert(png_or_jpg) as converted:
converted.save(filename=f'output/page.{png_or_jpg}')

def start_program():
print("\33[1m\33[96m* PDF to PNG/JPG Converter *\33[0m")
print()

print("\33[93mWhat is the name of the PDF file?\33[0m")
print("The file should be in the same directory as this program.")
file_name = str(input("Enter the name of the file: \33[93m"))
print("\33[0m")

# Check if the file exists
try:
with open(file_name):
pass
except FileNotFoundError:
# Check if any part of the file name matches any pdf file in the directory
pdf_files = [f for f in os.listdir() if f.endswith('.pdf')]

if len(pdf_files) > 1:
print("\33[91mMultiple PDF files were found in this directory, can't autofill file name. Please enter the full name of the file.\33[0m")
return

for pdf_file in pdf_files:
if file_name in pdf_file:
print(f'The file \33[93m"{pdf_file}"\33[0m was found wich matches \33[93m"{file_name}"\33[0m. Use this file instead?')
use_file = str(input("Enter Y or N: \33[93m"))
print("\33[0m")

if use_file == 'Y' or use_file == 'y' or use_file == 'yes' or use_file == 'Yes' or use_file == 'YES':
file_name = pdf_file
break
else:
return
else:
print("\33[91mFile not found. Please make sure the file is in the same directory as this program.\33[0m")
return


print("\33[93mConvert the file to PNG or JPG?\33[0m")
print("PNG images are transparent while JPG images are not.")
print("Enter 'png' or 'jpg' to convert the file to PNG or JPG respectively.")
png_or_jpg = str(input("Enter your choice: \33[93m"))
print("\33[0m")

if png_or_jpg != 'png' and png_or_jpg != 'jpg':
print("\33[91mInvalid choice. Please enter 'png' or 'jpg'.\33[0m")
return

print("\33[93mWhat resolution should the output image be?\33[0m")
print("Enter a number between 10 and 1000. Reccomended resolution is 100.")
resolution = int(input("Enter the resolution: \33[93m"))
print("\33[0m")

if resolution < 10 or resolution > 1000:
print("\33[91mInvalid resolution. Please enter a number between 10 and 1000.\33[0m")
return

print(f"\33[92mConverting {file_name} to {png_or_jpg}...\33[0m")
pdf2png(file_name, png_or_jpg, resolution)
print(f"Conversion successful. The images are in \33[92m{program_directory}\output\33[0m")


start_program()
2 changes: 1 addition & 1 deletion pdftopng.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

cd cmdout
convert -density 300 ../source.pdf s.png
convert -density 300 ../source.pdf s.jpg
cd ..

Binary file removed pyout/page-0.png
Diff not rendered.
Binary file removed pyout/page-1.png
Diff not rendered.
Binary file removed pyout/page-10.png
Diff not rendered.
Binary file removed pyout/page-11.png
Diff not rendered.
Binary file removed pyout/page-12.png
Diff not rendered.
Binary file removed pyout/page-13.png
Diff not rendered.
Binary file removed pyout/page-14.png
Diff not rendered.
Binary file removed pyout/page-15.png
Diff not rendered.
Binary file removed pyout/page-2.png
Diff not rendered.
Binary file removed pyout/page-3.png
Diff not rendered.
Binary file removed pyout/page-4.png
Diff not rendered.
Binary file removed pyout/page-5.png
Diff not rendered.
Binary file removed pyout/page-6.png
Diff not rendered.
Binary file removed pyout/page-7.png
Diff not rendered.
Binary file removed pyout/page-8.png
Diff not rendered.
Binary file removed pyout/page-9.png
Diff not rendered.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
wand==0.6.7
Binary file removed source.pdf
Binary file not shown.
4 changes: 2 additions & 2 deletions source.pdf.readme.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# source of pdf
http://www.tclauset.org/c5/
# Source of PDF Document used as example
[PapaCambridge](https://papacambridge.com/)