Extract a page from a pdf as a jpeg - python

In python code, how can I efficiently save a certain page of a PDF as a JPEG file?
Use case: I have a Python flask web server where PDFs will be uploaded and JPEGs corresponding to each page are stored.
This solution is close, but the problem is that it does not convert the entire page to JPEG.

The pdf2image library can be used.
You can install it simply using,
pip install pdf2image
Once installed you can use following code to get images.
from pdf2image import convert_from_path
pages = convert_from_path('pdf_file', 500)
Saving pages in jpeg format
for page in pages:
page.save('out.jpg', 'JPEG')
Edit: the Github repo pdf2image also mentions that it uses pdftoppm and that it requires other installations:
pdftoppm is the piece of software that does the actual magic. It is distributed as part of a greater package called poppler.
Windows users will have to install poppler for Windows.
Mac users will have to install poppler for Mac.
Linux users will have pdftoppm pre-installed with the distro (Tested on Ubuntu and Archlinux) if it's not, run sudo apt install poppler-utils.
You can install the latest version under Windows using anaconda by doing:
conda install -c conda-forge poppler
note: Windows versions upto 0.67 are available at http://blog.alivate.com.au/poppler-windows/ but note that 0.68 was released in Aug 2018 so you'll not be getting the latest features or bug fixes.

I found this simple solution, PyMuPDF, output to png file. Note the library is imported as "fitz", a historical name for the rendering engine it uses.
import fitz
pdffile = "infile.pdf"
doc = fitz.open(pdffile)
page = doc.load_page(0) # number of page
pix = page.get_pixmap()
output = "outfile.png"
pix.save(output)
doc.close()
Note: The library changed from using "camelCase" to "snake_cased". If you run into an error that a function does not exist, have a look under deprecated names. The functions in the example above have been updated accordingly.
The fitz.Document class supports a context manager initialization:
with fitz.open(pdffile) as doc:
...

The Python library pdf2image (used in the other answer) in fact doesn't do much more than just launching pdttoppm with subprocess.Popen, so here is a short version doing it directly:
PDFTOPPMPATH = r"D:\Documents\software\____PORTABLE\poppler-0.51\bin\pdftoppm.exe"
PDFFILE = "SKM_28718052212190.pdf"
import subprocess
subprocess.Popen('"%s" -png "%s" out' % (PDFTOPPMPATH, PDFFILE))
Here is the Windows installation link for pdftoppm (contained in a package named poppler): http://blog.alivate.com.au/poppler-windows/.

Using pypdfium2 (v3):
python3 -m pip install "pypdfium2>=3,<4"
import pypdfium2 as pdfium
# Load a document
filepath = "tests/resources/multipage.pdf"
pdf = pdfium.PdfDocument(filepath)
# render a single page (in this case: the first one)
page = pdf.get_page(0)
pil_image = page.render_to(
pdfium.BitmapConv.pil_image,
)
pil_image.save("output.jpg")
# render multiple pages concurrently (in this case: all)
page_indices = [i for i in range(len(pdf))]
renderer = pdf.render_to(
pdfium.BitmapConv.pil_image,
page_indices = page_indices,
)
for image, index in zip(renderer, page_indices):
image.save("output_%02d.jpg" % index)
Advantages:
PDFium is liberal-licensed (BSD 3-Clause or Apache 2.0, at your choice)
It is fast, outperforming Poppler. In terms of speed, pypdfium2 can almost reach PyMuPDF
Returns PIL.Image.Image, numpy.ndarray, bytes, or a ctypes array, depending on your needs
Is capable of processing encrypted (password-protected) PDFs
No mandatory runtime dependencies
Supports Python >= 3.6
Setup infrastructure complies with PEP 517/518, while legacy setup still works as well
Wheels are currently available for
Windows amd64, win32, arm64
macOS x86_64, arm64
Linux (glibc) x86_64, i686, aarch64, armv7l
Linux (musl) x86_64, i686
There is a script to build from source, too.
(Disclaimer: I'm the author)

There is no need to install Poppler on your OS. This will work:
pip install Wand
from wand.image import Image
f = "somefile.pdf"
with(Image(filename=f, resolution=120)) as source:
for i, image in enumerate(source.sequence):
newfilename = f.removesuffix(".pdf") + str(i + 1) + '.jpeg'
Image(image).save(filename=newfilename)

#gaurwraith, install poppler for Windows and use pdftoppm.exe as follows:
Download zip file with Poppler's latest binaries/dlls from http://blog.alivate.com.au/poppler-windows/ and unzip to a new folder in your program files folder. For example: "C:\Program Files (x86)\Poppler".
Add "C:\Program Files (x86)\Poppler\poppler-0.68.0\bin" to your SYSTEM PATH environment variable.
From cmd line install pdf2image module -> "pip install pdf2image".
Or alternatively, directly execute pdftoppm.exe from your code using Python's subprocess module as explained by user Basj.
#vishvAs vAsuki, this code should generate the jpgs you want through the subprocess module for all pages of one or more pdfs in a given folder:
import os, subprocess
pdf_dir = r"C:\yourPDFfolder"
os.chdir(pdf_dir)
pdftoppm_path = r"C:\Program Files (x86)\Poppler\poppler-0.68.0\bin\pdftoppm.exe"
for pdf_file in os.listdir(pdf_dir):
if pdf_file.endswith(".pdf"):
subprocess.Popen('"%s" -jpeg %s out' % (pdftoppm_path, pdf_file))
Or using the pdf2image module:
import os
from pdf2image import convert_from_path
pdf_dir = r"C:\yourPDFfolder"
os.chdir(pdf_dir)
for pdf_file in os.listdir(pdf_dir):
if pdf_file.endswith(".pdf"):
pages = convert_from_path(pdf_file, 300)
pdf_file = pdf_file[:-4]
for page in pages:
page.save("%s-page%d.jpg" % (pdf_file,pages.index(page)), "JPEG")

GhostScript performs much faster than Poppler for a Linux based system.
Following is the code for pdf to image conversion.
def get_image_page(pdf_file, out_file, page_num):
page = str(page_num + 1)
command = ["gs", "-q", "-dNOPAUSE", "-dBATCH", "-sDEVICE=png16m", "-r" + str(RESOLUTION), "-dPDFFitPage",
"-sOutputFile=" + out_file, "-dFirstPage=" + page, "-dLastPage=" + page,
pdf_file]
f_null = open(os.devnull, 'w')
subprocess.call(command, stdout=f_null, stderr=subprocess.STDOUT)
GhostScript can be installed on macOS using brew install ghostscript
Installation information for other platforms can be found here. If it is not already installed on your system.

Their is a utility called pdftojpg which can be used to convert the pdf to img
You can found the code here https://github.com/pankajr141/pdf2jpg
from pdf2jpg import pdf2jpg
inputpath = r"D:\inputdir\pdf1.pdf"
outputpath = r"D:\outputdir"
# To convert single page
result = pdf2jpg.convert_pdf2jpg(inputpath, outputpath, pages="1")
print(result)
# To convert multiple pages
result = pdf2jpg.convert_pdf2jpg(inputpath, outputpath, pages="1,0,3")
print(result)
# to convert all pages
result = pdf2jpg.convert_pdf2jpg(inputpath, outputpath, pages="ALL")
print(result)

One problem everyone will face that is to Install Poppler. My way is a tricky way,but will work efficiently.
1st download Poppler here.
Then extract it and in the code section just add poppler_path=r'C:\Program Files\poppler-0.68.0\bin' (for eg.) like below
from pdf2image import convert_from_path
images = convert_from_path("mypdf.pdf", 500,poppler_path=r'C:\Program Files\poppler-0.68.0\bin')
for i, image in enumerate(images):
fname = 'image'+str(i)+'.png'
image.save(fname, "PNG")

Here is a function that does the conversion of a PDF file with one or multiple pages to a single merged JPEG image.
import os
import tempfile
from pdf2image import convert_from_path
from PIL import Image
def convert_pdf_to_image(file_path, output_path):
# save temp image files in temp dir, delete them after we are finished
with tempfile.TemporaryDirectory() as temp_dir:
# convert pdf to multiple image
images = convert_from_path(file_path, output_folder=temp_dir)
# save images to temporary directory
temp_images = []
for i in range(len(images)):
image_path = f'{temp_dir}/{i}.jpg'
images[i].save(image_path, 'JPEG')
temp_images.append(image_path)
# read images into pillow.Image
imgs = list(map(Image.open, temp_images))
# find minimum width of images
min_img_width = min(i.width for i in imgs)
# find total height of all images
total_height = 0
for i, img in enumerate(imgs):
total_height += imgs[i].height
# create new image object with width and total height
merged_image = Image.new(imgs[0].mode, (min_img_width, total_height))
# paste images together one by one
y = 0
for img in imgs:
merged_image.paste(img, (0, y))
y += img.height
# save merged image
merged_image.save(output_path)
return output_path
Example usage: -
convert_pdf_to_image("path_to_Pdf/1.pdf", "output_path/output.jpeg")

I wrote this script to easily convert a folder directory that contains PDFs (single page) to PNGs really nicely.
import os
from pathlib import PurePath
import glob
# from PIL import Image
from pdf2image import convert_from_path
import pdb
# In[file list]
wd = os.getcwd()
# filter images
fileListpdf = glob.glob(f'{wd}//*.pdf')
# In[Convert pdf to images]
for i in fileListpdf:
images = convert_from_path(i, dpi=300)
path_split = PurePath(i).parts
fileName, ext = os.path.splitext(path_split[-1])
images[0].save(f'{fileName}.png', 'PNG')
Hopefully, this helps if you need to convert PDFs to PNGs!

I use a (maybe) much simpler option of pdf2image:
cd $dir
for f in *.pdf
do
if [ -f "${f}" ]; then
n=$(echo "$f" | cut -f1 -d'.')
pdftoppm -scale-to 1440 -png $f $conv/$n
rm $f
mv $conv/*.png $dir
fi
done
This is a small part of a bash script in a loop for the use of a narrow casting device.
Checks every 5 seconds on added pdf files (all) and processes them.
This is for a demo device, at the end converting will be done at a remote server. Converting to .PNG now, but .JPG is possible too.
This converting, together with transitions on A4 format, displaying a video, two smooth scrolling texts and a logo (with transition in three versions) sets the Pi3 to allmost 4x 100% cpu-load ;-)

from pdf2image import convert_from_path
import glob
pdf_dir = glob.glob(r'G:\personal\pdf\*') #your pdf folder path
img_dir = "G:\\personal\\img\\" #your dest img path
for pdf_ in pdf_dir:
pages = convert_from_path(pdf_, 500)
for page in pages:
page.save(img_dir+pdf_.split("\\")[-1][:-3]+"jpg", 'JPEG')

Here is a solution which requires no additional libraries and is very fast. This was found from: https://nedbatchelder.com/blog/200712/extracting_jpgs_from_pdfs.html#
I have added the code in a function to make it more convenient.
def convert(filepath):
with open(filepath, "rb") as file:
pdf = file.read()
startmark = b"\xff\xd8"
startfix = 0
endmark = b"\xff\xd9"
endfix = 2
i = 0
njpg = 0
while True:
istream = pdf.find(b"stream", i)
if istream < 0:
break
istart = pdf.find(startmark, istream, istream + 20)
if istart < 0:
i = istream + 20
continue
iend = pdf.find(b"endstream", istart)
if iend < 0:
raise Exception("Didn't find end of stream!")
iend = pdf.find(endmark, iend - 20)
if iend < 0:
raise Exception("Didn't find end of JPG!")
istart += startfix
iend += endfix
jpg = pdf[istart:iend]
newfile = "{}jpg".format(filepath[:-3])
with open(newfile, "wb") as jpgfile:
jpgfile.write(jpg)
njpg += 1
i = iend
return newfile
Call convert with the pdf path as the argument and the function will create a .jpg file in the same directory

For a pdf file with multiple pages, the following is the best & simplest (I used pdf2image-1.14.0):
from pdf2image import convert_from_path
from pdf2image.exceptions import (
PDFInfoNotInstalledError,
PDFPageCountError,
PDFSyntaxError
)
images = convert_from_path(r"path/to/input/pdf/file", output_folder=r"path/to/output/folder", fmt="jpg",) #dpi=200, grayscale=True, size=(300,400), first_page=0, last_page=3)
images.clear()
Note:
"images" is a list of PIL images.
The saved images in the output folder will have system generated names; one can later change them, if required.

This easy script can convert a folder directory that contains PDFs (single/multiple pages) to jpeg.
from PIL import Image
import pytesseract
import sys
from pdf2image import convert_from_path
import os
from os import listdir
from os import system
from os.path import isfile, join, basename, dirname
import shutil
def move_processed_file(file, doc_path, download_processed):
try:
shutil.move(doc_path + '/' + file, download_processed + '/' + file)
pass
except Exception as e:
print(e.errno)
raise
else:
pass
finally:
pass
pass
def run_conversion():
root_dir = os.path.abspath(os.curdir)
doc_path = root_dir + r"\data\download"
pdf_processed = root_dir + r"\data\download\pdf_processed"
results_folder = doc_path
files = [f for f in listdir(doc_path) if isfile(join(doc_path, f))]
pdf_files = [f for f in listdir(doc_path) if isfile(join(doc_path, f)) and f.lower().endswith('.pdf')]
# check OS type
if os.name == 'nt':
# if is windows or a graphical OS, change this poppler path with your own path
poppler_path = r"C:\poppler-0.68.0\bin"
else:
poppler_path = root_dir + r"\usr\bin"
for file in pdf_files:
'''
# Converting PDF to images
'''
# Store all the pages of the PDF in a variable
pages = convert_from_path(doc_path + '/' + file, 500, poppler_path=poppler_path)
# Counter to store images of each page of PDF to image
image_counter = 1
filename, file_extension = os.path.splitext(file)
# Iterate through all the pages stored above
for page in pages:
# Declaring filename for each page of PDF as JPG
# PDF page n -> page_n.jpg
filename = filename + '_' + str(image_counter) + ".jpg"
# Save the image of the page in system
page.save(results_folder + '/' + filename, 'JPEG')
# Increment the counter to update filename
image_counter += 1
move_processed_file(file, doc_path, pdf_processed)

from pdf2image import convert_from_path
PDF_file = 'Statement.pdf'
pages = convert_from_path(PDF_file, 500,userpw='XXX')
image_counter = 1
for page in pages:
filename = "foldername/page_" + str(image_counter) + ".jpg"
page.save(filename, 'JPEG')
image_counter = image_counter + 1

Related

Python: pdf2image doesn't write .jpg - no error message

I'm working on a python script that checks the .pdf files in a directory, creates a new directory for each file, converts the .pdf into images, and writes the images as jpg into the new directory. I'm using pdf2image and have the following code:
import os
#import main
import glob
#import cv2
import matplotlib.pyplot as plt
from pdf2image import convert_from_path
from PIL import Image
path = "C:/Users/d/Desktop/Reis/"
for file in glob.iglob(path + "*.pdf"):
print(file)
name = os.path.basename(file)
filename = name.split(".")[0]
print(filename)
images = os.mkdir(path + filename)
pages = convert_from_path("C:/Users/d/Desktop/Reis/Reis_Wasser_Verhaeltnis.pdf",
350,
poppler_path=r'C:/Program Files/poppler-22.04.0/Library/bin',
output_folder=images)
for i in range(len(pages)):
pages[i].save('page' + str(i) + '.jpg', 'JPEG')
When I run my code I don't get an error message but no images either. Does anyone have an idea what I'm overseeing?
os.mkdir creates the Folder but it is of type boolean. Thus:
images = os.mkdir(path + filename)
returns only True and cannot be used as the output folder. My script writes the images into the default project directory.

I want to select wav files from one directory and save the features as png file with the same filename

I want to plot MFCC features of .wav files(more than one file) from a folder and save them with the same file name.png to another folder.
you can loop through them like this:
import os
import imageio
audio_folder = "./audio/"
image_folder = "./images/"
for filename in os.listdir(audio_folder):
path = os.path.join(audio_folder, filename)
# -=-=-=- read audio file using `path`
# -=-=-=- convert to image here -=-=-=-
image = ...
new_name = filename.split(".wav")[0] + "png"
path = os.path.join(image_folder, new_name)
imageio.imwrite(path, image)
this would require the package imageio which can be simply installed using:
pip install imageio

Is there any faster way to find every image from the entire computer?

I'm using that function to look for images but that's actually a very slow. I wonder if there is a faster way to do that.
import os
import cv2
images = []
def load_images_from_folder(folder):
global images
os.chdir(folder)
for filename in os.listdir(folder):
if os.path.isdir(os.path.join(folder,filename)):
try:
load_images_from_folder(os.path.join(folder, filename))
except:
pass
img = cv2.imread(os.path.join(folder,filename))
if img is not None:
images.append(img)
load_images_from_folder("C:\\")
As recommended in this answer you can use imghdr built-in module to test if a file is an image:
import imghdr
import os
def get_images(root_directory):
images = []
for root, dirs, filenames in os.walk(root_directory):
for filename in filenames:
path = os.path.join(root, filename)
if imghdr.what(path) is not None:
images.append(path)
return images
But imghdr module only detects several image types, according to documentation it cannot detect swg.
As #NiklasMertsch suggested you can just check for image file extensions like this:
import os
extensions = [
'.png',
'.jpg',
'.jpeg',
'.bmp',
'.gif',
'.tiff',
'.swg',
]
def get_images(root_directory):
images = []
for root, dirs, filenames in os.walk(root_directory):
for filename in filenames:
for extension in extensions:
if filename.endswith(extension):
images.append(os.path.join(root, filename))
break
return images
You can use the library treeHandler to achieve this. You can install it using pip install treeHandler
from treeHandler import treeHandler
import os
import cv2
inputFolder='SampleData'
### initialising treeHandler object
th=treeHandler()
### calling getFiles function to get all jpg files, you can add more extensions like ['jpg','png','bmp'] if required
fileTuple=th.getFiles(inputFolder,['jpg'])
Here is what fileTuple sample looks like:
[('image01.jpg', 'sampleData'), ('image02.jpg', 'sampleData'), ('image03.jpg', 'sampleData'), ('image04.jpg', 'sampleData'), ('image11.jpg', 'sampleData/folder1'), ('image12.jpg', 'sampleData/folder1'), ('image111.jpg', 'sampleData/folder1/folder11'), ('image112.jpg', 'sampleData/folder1/folder11'), ('image1111.jpg', 'sampleData/folder1/folder11/folder111'), ('image1112.jpg', 'sampleData/folder1/folder11/folder111'), ('image11111.jpg', 'sampleData/folder1/folder11/folder111/folder1111')....]
I have written a blog on processing large number of files and processing them
https://medium.com/#sreekiranar/directory-and-file-handling-in-python-for-real-world-projects-9bc8baf6ba89
You can refer that to get a fair idea on how to deal with getting all files from folders and subfolders.

python download images are not saved to the correct directory

When i use python 2.7 to download images from a website, the code as follows:
pic = requests.get(src[0])
f = open("pic\\"+str(i) + '.jpg', "wb")
f.write(pic.content)
f.close()
i += 1
I want to save the picture into pic directory, but I find that images is saved in the same directory with the name like pic\1.jpg. Is this a bug?
In Windows, it's right, but on Ubuntu, it's an error!
Windows uses backslashes for file paths, but Ubuntu uses forward slashes. This is why your save path with a backslash doesn't work on Ubuntu.
You probably want to use os.path.join to make your path OS agnostic:
import os
path = os.path.join('pic', '{}.jpg'.format(str(i)))
f = open(path, 'wb)
...
import os
f = open(os.sep.join(['pic', str(i), '.jpg']), 'wb')
Now the line should be os agnostic

Python GUI: How to browse a directory and read ALL files in it?

This'd be my first post here, and I'm having serious issues about file reading with Python GUI.
So, I'm totally lost in trying to figure a smarter way to write a code in Python 2.7 using GUI to browse a directory and read ALL files in it for further operations. In my case, the files I'm reading are images and I'm trying to save it in an array.
I'm using those numpy, scikits etc, for easing my image-processing work. I am trying to process 3 images named "pulse1.jpg" to "pulse3.jpg".
Here's my work below. My apologies if it's kinda bit messy/unclear, especially since I can't post images yet:
import os
import numpy as np
import cv2
from matplotlib import pyplot as plt
from skimage import color, data, restoration
# first we specify the file directory
directory = 'I:\\My Pictures\\toby arm pics'
filename = "pulse"
# initiate file reading
isfile = os.path.isfile
join = os.path.join
# first, we get the number of files (images)
# in the directory
number_of_files = sum(1 for item in os.listdir(directory)\
if isfile(join(directory, item)))
X = [] # we'll store the read images here
i = 1 # initiate iteration for file reading
string = "" # initiation
# read the images from directory
# and save it to array X
while i <= number_of_files:
string = directory + "\\" + filename + str(i) + ".jpg"
temp = cv2.imread(string, -1)
X.append(temp)
i += 1
Thank you so much for your help, guys!
>>> import Tkinter, tkFileDialog
>>> root = Tkinter.Tk()
>>> root.withdraw()
''
>>> dirname = tkFileDialog.askdirectory(parent=root,initialdir="/",title='Pick a directory')
>>> for filename in os.listdir(dirname):
>>> temp = cv2.imread(filename,-1)
maybe?
(I snagged most of this code from http://code.activestate.com/recipes/438123-file-tkinter-dialogs/)

Categories