I want to convert a pdf (one page) into a png file.
I installed pdf2image and get this error:
popler is not installed in windows.
According to this question:
Poppler in path for pdf2image, poppler should be installed and PATH modified.
I cannot do any of those (I don't have the necessary permissions in the system I am working with).
I had a look at opencv and PIL and none seems to offer the possibility to make this transformation:
PIL (see here https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html?highlight=pdf#pdf) does not offer the possibility to read pdfs, only to save images as pdfs.
The same goes for openCV.
Any suggestion how to make the pdf to png transformation ? I can install any python library but I can not touch the windows installation.
thanks
Here is a snippet that generates PNG images of arbitrary resolution (dpi):
import fitz
file_path = "my_file.pdf"
dpi = 300 # choose desired dpi here
zoom = dpi / 72 # zoom factor, standard: 72 dpi
magnify = fitz.Matrix(zoom, zoom) # magnifies in x, resp. y direction
doc = fitz.open(fname) # open document
for page in doc:
pix = page.get_pixmap(matrix=magnify) # render page to an image
pix.save(f"page-{page.number}.png")
Generates PNG files name page-0.png, page-1.png, ...
By choosing dpi < 72 thumbnail page images would be created.
PyMuPDF supports pdf to image rasterization without requiring any external dependencies.
Sample code to do a basic pdf to png transformation:
import fitz # PyMuPDF, imported as fitz for backward compatibility reasons
file_path = "my_file.pdf"
doc = fitz.open(file_path) # open document
for page in doc:
pix = page.get_pixmap() # render page to an image
pix.save(f"page_{i}.png")
Related
I want to remove exif from an image before uploading to s3. I found a similar question (here), but it saves as a new file (I don't want it). Then I found an another way (here), then I tried to implemented it, everything was ok when I tested it. But after I deployed to prod, some users reported they occasionally got a problem while uploading images with a size of 1 MB and above, so they must try it several times.
So, I just want to make sure is my code correct?, or maybe there is something I can improve.
from PIL import Image
# I got body from http Request
img = Image.open(body)
img_format = img.format
# Save it in-memory to remove EXIF
temp = io.BytesIO()
img.save(temp, format=img_format)
body = io.BytesIO(temp.getvalue())
# Upload to s3
s3_client.upload_fileobj(body, BUCKET_NAME, file_key)
*I'm still finding out if this issue is caused by other things.
You should be able to copy the pixel data and palette (if any) from an existing image to a new stripped image like this:
from PIL import Image
# Load existing image
existing = Image.open(...)
# Create new empty image, same size and mode
stripped = Image.new(existing.mode, existing.size)
# Copy pixels, but not metadata, across
stripped.putdata(existing.getdata())
# Copy palette across, if any
if 'P' in existing.mode: stripped.putpalette(existing.getpalette())
Note that this will strip ALL metadata from your image... EXIF, comments, IPTC, 8BIM, ICC colour profiles, dpi, copyright, whether it is progressive, whether it is animated.
Note also that it will write JPEG images with PIL's default quality of 75 when you save it, which may or may not be the same as your original image had - i.e. the size may change.
If the above stripping is excessive, you could just strip the EXIF like this:
from PIL import Image
im = Image.open(...)
# Strip just EXIF data
if 'exif' in im.info: del im.info['exif']
When saving, you could test if JPEG, and propagate the existing quality forward with:
im.save(..., quality='keep')
Note: If you want to verify what metadata is in any given image, before and after stripping, you can use exiftool or ImageMagick on macOS, Linux and Windows, as follows:
exiftool SOMEIMAGE.JPG
magick identify -verbose SOMEIMAGE.JPG
I am looking for a solution to convert Image bytes into PDF bytes in memory only.
For my web application, it takes in pdf/tiff documents (can be multi-paged) for information extraction.
I am adding in an image preprocessing step at the start of the pipeline. However, this step is only applicable for images as I am using OpenCV2. Thus, the pdf/tiff file is converted into image(s) for preprocessing. However, to send the file for information extraction I will need to join them back together, as there is a different logic flow for the first vs the subsequent pages.
I was previously using a workaround (referencing local path of merged pdf) but now I would like to remove the dependency and do everything in-memory. This is so that I will be able to deploy the application on the cloud.
image = Image.open(io.BytesIO(file_str))
num_frames = image.n_frames
# Loop through each page of a tif file
for i in range(num_frames):
image.seek(i)
file_array = np.array(image)
file_array = file_array.astype(np.uint8) * 255
# Preprocessing (removed for simplicity)
# TODO: Merge back into PDF file
Edit:
Simple answer: I can't do this in memory. Instead, I have used the tempfile library to help me to save the files there and delete the temporary directory after I am done. That, in some way has helped to achieve the "in memory" aspect.
Writing (not reading) multi-page PDF files is possible using Pillow. For the below solution, I used pdf2image for converting some multi-page PDF file to a list of Pillow Image objects. So, please adapt that according to your existing code.
from PIL import Image
import pdf2image
import numpy as np
# Read pages from PDF to Pillow Image objects
frames_in = pdf2image.convert_from_path('path/to/your/file.pdf')
# Enumerate frames, and preprocess
frames_out = []
for i, frame in enumerate(frames_in):
# Convert to NumPy array
frame = np.array(frame)
# Preprocessing for the first page
if i == 0:
frame[:100, ...] = [255, 0, 0]
# Preprocessing for the other pages
else:
frame[:100, ...] = [0, 0, 255]
# Convert back to Pillow Image object, and append to output list
frames_out.append(Image.fromarray(frame))
frames_out[0].save('output.pdf', save_all=True, append_images=frames_out[1:])
When using some sample PDF, the output looks the same, but with a red rectangle on the first page, and a blue rectangle on the second page.
----------------------------------------
System information
----------------------------------------
Platform: Windows-10-10.0.16299-SP0
Python: 3.9.1
PyCharm: 2021.1.1
NumPy: 1.20.2
pdf2image 1.14.0
Pillow: 8.2.0
----------------------------------------
The pillow package has a method called Image.putalpha() which is used to add or change the alpha channel of an image.
I tried to play with this method and found that I can not change the background color of an image. The original image is
This is my code to add alpha to it
from PIL import Image
im_owl = Image.open("owl.jpg")
alpha = Image.new("L", im_owl.size, 50)
im_owl.putalpha(alpha)
im_owl.show()
The produced image is nothing different from the original image. I have tried with different value of alpha and see no difference.
What could have been wrong?
try to save the image and see it.
I am also not able to see the image directly from
im_owl.show()
but when I saved it
im_owl.save()
I am able to see the image changed.
Try using
im_owl.save("alphadOwl.png")
And then view the saved image. It would seem that the alpha channel isn't applied to bmp or jpg files. It is a bmp file that gets displayed with im.show()
(For the record, I'm on a mac, I don't know if im.show() uses different applications on other devices).
As #sanyam and #Pam have pointed out, we can save the converted image and it shows correctly. This is because on Windows, images are saved as temporary BMP file before they are shown using the system default image viewer, as per the PIL documentation:
Image.show(title=None, command=None)
Displays this image. This method is mainly intended for debugging purposes.
On Unix platforms, this method saves the image to a temporary PPM file, and calls
either the xv utility or the display utility, depending on which one can be found.
On macOS, this method saves the image to a temporary BMP file, and opens it with
the native Preview application.
On Windows, it saves the image to a temporary BMP file, and uses the standard BMP
display utility to show it (usually Paint).
To fix this issue, we can patch the Pillow code to use PNG format as default. First, we need to find the root of Pillow package:
import PIL
print(PIL.__path__)
On my system, the output is:
[’D:\Anaconda\lib\site-packages\PIL’]
Go to this directory and open the file ImageShow.py. I add the following code after the line register(WindowsViewer):
class WindowsPNGViewer(Viewer):
format = "PNG"
def get_command(self, file, **options):
return ('start "Pillow" /WAIT "%s" '
'&& ping -n 2 127.0.0.1 >NUL '
'&& del /f "%s"' % (file, file))
register(WindowsPNGViewer, -1)
After that, I can show the image with alpha channel correctly.
References
https://github.com/python-pillow/Pillow/issues/3695
First post here, although i already spent days of searching for various queries here. Python 3.6, Pillow and tiff processing.
I would like to automate one of our manual tasks, by resizing some of the images from very big to match A4 format. We're operating on tiff format, that sometimes ( often ) contains more than one page. So I wrote:
from PIL import Image,
...
def image_resize(path, dcinput, file):
dcfake = read_config(configlocation)["resize"]["dcfake"]
try:
imagehandler = Image.open(path+file)
imagehandler = imagehandler.resize((2496, 3495), Image.ANTIALIAS)
imagehandler.save(dcinput+file, optimize=True, quality=95)
except Exception:
But the very (not) obvious is that only first page of tiff is being converted. This is not exactly what I expect from this lib, however tried to dig, and found a way to enumerate each page from tiff, and save it as a separate file.
imagehandler = Image.open(path+file)
for i, page in enumerate(ImageSequence.Iterator(imagehandler)):
page = page.resize((2496, 3495), Image.ANTIALIAS)
page.save(dcinput + "proces%i.tif" %i, optimize=True, quality=95, save_all=True)
Now I could use imagemagick, or some internal commands to convert multiple pages into one, but this is not what I want to do, as it drives to code complication.
My question, is there a unicorn that can help me with either :
1) resizing all pages of given multi-page tiff in the fly
2) build a tiff from few tiffs
I'd like to focus only on python modules.
Thx.
Take a look at this example. It will make every page of a TIF file four times smaller (by halving width and height of every page):
from PIL import Image
from PIL import ImageSequence
from PIL import TiffImagePlugin
INFILE = 'multipage_tif_example.tif'
OUTFILE = 'multipage_tif_resized.tif'
print ('Resizing TIF pages')
pages = []
imagehandler = Image.open(INFILE)
for page in ImageSequence.Iterator(imagehandler):
new_size = (page.size[0]/2, page.size[1]/2)
page = page.resize(new_size, Image.ANTIALIAS)
pages.append(page)
print ('Writing multipage TIF')
with TiffImagePlugin.AppendingTiffWriter(OUTFILE) as tf:
for page in pages:
page.save(tf)
tf.newFrame()
It's supposed to work since late Pillow 3.4.x versions (works with version 5.1.0 on my machine).
Resources:
AppendingTiffWriter discussed here.
Sample TIF files can be downloaded here.
Most people recommend "wand" when it comes to imagemagick for python but how can I append images using it in python ? I want to add lables to bottom of images using imagemagick in python : http://www.imagemagick.org/Usage/annotating/ but wand api seems to be very basic and doesn't have lots of imgemagick commands including labels and appending.
Is there any other way to use imagemagick in python ? my images are png type and are BytesIO streams inside the python code not files so I can not pass them to imagemagick using command line and can't save them on any temporary file either.
I'm not really sure what your asking for, but I'm guessing you want to write a label "below" an image. Here's an example with the wand library.
from wand.image import Image
from wand.compat import nested
from wand.color import Color
from wand.font import Font
with nested(Image(filename='logo:'),
Image(filename='null:')) as (source, text):
text.font = Font('Impact', 64)
text.read(filename='label:Hello world!')
largest_width = max(source.width, text.width)
offset = (largest_width - min(source.width, text.width)) / 2
with Image(width=largest_width,
height=source.height + text.height,
background=Color('WHITE')) as dst:
dst.composite(source, 0, 0)
dst.composite(text, int(offset), source.height)
dst.save(filename="output.png")
Overview
with nested(Image(filename='logo:'),
Image(filename='null:')) as (source, text):
Create two images. You would be responsible for replacing logo: image with your ByteIO buffer. The null: image is a placeholder for allocating a wand instance.
text.font = Font('Impact', 64)
text.read(filename='label:Hello world!')
This defines the typeface & text to draw. The label: protocol can be replaced with caption: for additional behavior(s?).
with Image(width=largest_width,
height=source.height + text.height,
background=Color('WHITE')) as dst:
Create a third "blank" image that is large enough to include both images.
dst.composite(source, 0, 0)
dst.composite(text, int(offset), source.height)
Copy the image data from the source & text to the new image.
Imagemagick is great but has steep learning curve. You need to have 3rd party ImageMagick installed (Would have to be 32 or 64 bit depending on your version of python). Installation alone is a pain considering you need to add it to path and the executable is necessary to run your script.
Consider something more portable and contained like Pillow which has number of features for achieving your goal.
pip install pillow
In pillow you can read images from BytesIO. And also draw or print on them. There is number of options available.
from PIL import Image, ImageDraw
import io
# Reading bytes from file but you can skip that and simply open your bytes like below.
with open('picture.jpg', 'rb') as f:
im = Image.open(io.BytesIO(f.read()))
draw = ImageDraw.Draw(im)
draw.text((10,10), "Hello World", fill=(255))
im.show() # do whatever you like with the image
Check the docs for more examples. https://pillow.readthedocs.io/en/latest/