How to get the format of image with PIL? - python

After loading an image file with PIL.Image, how can I determine whether the image file is a PNG/JPG/BMP/GIF? I understand very little about these file formats, can PIL get the format metadata from the file header? Or does it need to 'analyze' the data within the file?
If PIL doesn't provide such an API, is there any python library that does?

Try:
from PIL import Image
img = Image.open(filename)
print(img.format) # 'JPEG'
More info
https://pillow.readthedocs.io/en/latest/reference/Image.html#PIL.Image.Image.format
https://pillow.readthedocs.io/en/latest/handbook/image-file-formats.html

Related

Python: how to convert an image in memory?

I have an image in memory (downloaded from an online source) and I want to convert it to a different format before sending it on to a different online location.
The conversion is .webp to .jpg but that's not really relevant.
With Pillow I can easily convert local images and save them back to disc, but I can't get it to work with an image in memory.
I don't necessarily need to use Pillow. Any way to convert the image without having to save anything to disc is fine.
I am new to BytesIO with PIL, so just check my code attempt, with my test image it works, let me know
from PIL import Image
from io import BytesIO
img = Image.open('test.webp')
print('image : ', img.format)
img.show()
# Write PIL Image to in-memory PNG
membuf = BytesIO()
img.save(membuf, format="png")
img = Image.open(membuf)
print('image : ', img.format)
img.show()

How to read a multispectral image?

I have multispectral images with the .tif extension. How can I read such multispectral images with Python?
Pillow module supports "Multi-frame TIFF Images" that might be your thing.
Could you provide an example file you would like to open and, what do you expect inside?
https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html#reading-multi-frame-tiff-images
This might do the job:
from PIL import Image
im = Image.open("image.tif")
print(im.num_frames)
im.seek(im.tell() + 1))

conveting bytes to image using tinytag and PIL

I am using tinytags module in python to get the cover art of a mp3 file and want to display or store it. The return type of the variable is showing to be bytes. I have tried fumbling around with PIL using frombytes but to no avail. Is there any method to convert the bytes to image?
from tinytag import TinyTag
tag = TinyTag.get("03. Me, Myself & I.mp3", image=True)
img = tag.get_image()
I actually got a PNG image when I called tag.get_image() but I guess you might get a JPEG. Either way, you can wrap it in a BytesIO and open it with PIL/Pillow or display it. Carrying on from your code:
from PIL import Image
import io
...
im = tag.get_image()
# Make a PIL Image
pi = Image.open(io.BytesIO(im))
# Save as PNG, or JPEG
pi.save('cover.png')
# Display
pi.show()
Note that you don't have to use PIL/Pillow. You could look at the first few bytes and if they are a PNG signature (\x89PNG) save data as binary with PNG extension. If the signature is JPEG (\xff \xd8) save data as binary with JPEG extension.

PDF to IMG to PDF all done in memory

In order to remove sensitive content from a PDF, I am converting it to image and back to PDF again.
I am able to do this while saving the jpeg image, however I would eventually like to adapt my code so that the file is in memory the whole time. PDF in memory -> JPEG in memory -> PDF in memory. I'm having trouble with the intermediary step.
from pdf2image import convert_from_path, convert_from_bytes
import img2pdf
images = convert_from_path('testing.pdf', fmt='jpeg')
image = images[0]
# opening from filename
with open("output/output.pdf","wb") as f:
f.write(img2pdf.convert(image.tobytes()))
On the last line, I am getting the error:
ImageOpenError: cannot read input image (not jpeg2000). PIL: error reading image: cannot identify image file <_io.BytesIO object at 0x1040cc8f0>
I'm not sure how to be converting this image to the string that img2pdf is looking for.
The pdf2image module will extract the images as Pillow images. And according the Pillow tobytes() documention: "This method returns the raw image data from the internal storage." Which is some bitmap representation.
To get your code working use BytesIO module like so:
# opening from filename
import io
with open("output/output.pdf","wb") as f, io.BytesIO() as output:
image.save(output, format='jpg')
f.write(img2pdf.convert(output.getvalue()))

How do I change my image format?

I'm trying to blur an image using PIL:
from PIL import Image
from PIL import ImageFilter
im = Image.open("plot.png")
im = im.filter(ImageFilter.BLUR)
When I do im.show() and save it to my hard drive, it saves as a BMP file, which is incompatible with the place where I'm trying to upload it. How do I change the file format from BMP to something else that is compatible?
Just use the save() function directly:
from PIL import Image
from PIL import ImageFilter
im = Image.open("plot.png")
im = im.filter(ImageFilter.BLUR)
im.save("saved.jpg")
This function supports many formats, as explained in the documentation.

Categories

Resources