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))
Related
I've been trying to find the best way to convert a given GIF image to a sequence of BMP files using python.
I've found some libraries like Wand and ImageMagic but still haven't found a good example to accomplish this.
Reading an animated GIF file using Python Image Processing Library - Pillow
from PIL import Image
from PIL import GifImagePlugin
imageObject = Image.open("./xmas.gif")
print(imageObject.is_animated)
print(imageObject.n_frames)
Display individual frames from the loaded animated GIF file
for frame in range(0,imageObject.n_frames):
imageObject.seek(frame)
imageObject.show()
from wand.image import Image
with Image(filename="animation.gif") as img:
img.coalesce()
img.save(filename="frame%02d.bmp)
Use Image.coalesce() to rebuild optimized frames, and ImageMagick's "Percent Escapes" format (%02d) to write image frame as a separate BMP file.
In Imagemagick, which comes with Linux and can be installed for Windows or Mac OSX,
convert image.gif -coalesce image.bmp
the results will be image-0.bmp, image-1.bmp ...
Use convert for Imagemagick 6 or replace convert with magick for Imagemagick 7.
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.
Given the short, 5 page PDF file (attached at the bottom), and the following python code to convert to a multi-page TIFF:
from wand.image import Image
with Image(filename='5-page-pdf.pdf', resolution=200) as img:
img.type = "grayscale"
img.format = "tiff"
img.compression = "lzw"
img.save(filename="test.tiff")
results in a TIFF file that has pages 2-4 as what appears to be black text on a dark-grey (or maybe transparent) background. Other image processing libraries cannot open the file or render it.
Converting the same PDF with ImageMagick, which Wand uses, works just fine
convert -density 200 5-page-pdf.pdf -type grayscale -compress lzw 5-page-pdf.tiff
this produces a file that does work with other imaging libraries and looks correct in a TIFF viewer.
I've tried removing the alpha channel, I've tried setting the background color to 'White', and a few other things, to no avail. The TIFF that comes out of Wand is always garbled. If it's doable in ImageMagick it should be doable in Wand, right? What parameter or setting am I missing?
Original PDF
Wand Produced TIFF
Looks like setting the img.alpha_channel property is not propagating across the pages.
Try this workaround
from wand.api import library
from wand.image import Image
with Image(filename="5-page-pdf.pdf", resolution=200) as img:
img.type = 'grayscale'
img.compression = "lzw"
# Manually iterate over all page, and turn off alpha channel.
library.MagickResetIterator(img.wand)
for idx in range(library.MagickGetNumberImages(img.wand)):
library.MagickSetIteratorIndex(img.wand, idx)
img.alpha_channel = 'off'
img.save(filename="test.tiff")
I have a problem converting a tiff image from a microscope to a jpeg, which should be shown within a web application.
I tried the following:
image = Image.open(file_name)
image.convert(mode="RGB")
image.save('my.jpeg')
>>IOError: cannot write mode I;16 as JPEG
Anybody has some experience in converting 16-bit TIFF files to jpegs...
I have linked such a file below.
Thanks for your help!
https://drive.google.com/open?id=0B04N02JqhWJOWjBPY1RRZkIwbTg
It's a bug. Here is a workaround:
import Image
image = Image.open("Fredy1_002.tif")
image.mode = 'I'
image.point(lambda i:i*(1./256)).convert('L').save('my.jpeg')
See https://stackoverflow.com/a/7248480/839338
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