Skip to content
Merged
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
6 changes: 6 additions & 0 deletions pdf2image/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# jpg
*.jpg
*.jpeg
*.png
*.pdf
test.py
48 changes: 48 additions & 0 deletions pdf2image/pdf2img.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import os,sys,fitz

if len(sys.argv) != 2:
pdf_path = input("Please enter pdf name(with extention) : ")
else:
pdf_path = sys.argv[1]
# for command line usage : python pdf2img.py main.pdf

# pdf_path = 'main.pdf'

# CHECK IF Path exist
if not os.path.exists(pdf_path):
print("path does not exist, pdf must be in the same folder")
sys.exit(1)

# create folder recursively XD
os.makedirs('Converted_img',exist_ok=True)
# os.mkdir('Converted_img',0o666)

base_name = os.path.basename(pdf_path).split('.')[0]
images = []
#
pdf_file = fitz.open(pdf_path)

for i in range(len(pdf_file)):
page = pdf_file.loadPage(i)
page_pixel = page.getPixmap()
images.append(page_pixel.tobytes('pgm'))
page_pixel.writePNG(f"Converted_img/{base_name}{i}.png")




# to compress png and convert into JPEG
'''
from PIL import Image
def png2jpg():
directory = r'Converted_img'
for f in os.listdir(directory):
if f.endswith(".png"):
im = Image.open(os.path.join(directory, f))
name= os.path.splitext(os.path.join(directory, f))[0] +'.jpg'
rgb_im = im.convert('RGB')
rgb_im.save(name)
# print(os.path.join(directory, f))
png2jpg()

'''
36 changes: 36 additions & 0 deletions pdf2image/readme.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# PDF 2 Image converter

## Requirements

Python 3+
pyMuPDF
```pip install pyMuPDF```

## usage :

- command line argument :
```cmd
python pdf2img.py main.pdf
```
- general python program :
```cmd
python pdf2img.py
```
It will ask for user input :
you can give the file name with extention (i.e, mydoc.pdf ) [pdf must be in the same directory as of the script]
or you can also give absolute path of the pdf.

### Output :

```Each page will be converted into PNG image in a subfolder Converted_img```

#### There is a commented code also available to compress image and convert into jpg format.

## Contriburation are always welcomed :)

1. fork the repository
2. clone the repository
3. start hacking XD

#### Hacktoberfest2021
#### Happy coding !!