My main aim is to produce 1D array out of each image in the 'dengue' folder. For which I used below code to read the file using both PIL and GLOB.
from PIL import Image
import glob
image_list = []
for filename in glob.glob('./dengue/*.tiff'):
im=Image.open(filename)
image_list.append(im)
OUTPUT IS -
UnidentifiedImageError: cannot identify image file './dengue/image_2016-09-18.tiff
How to resolve this? The same error showed up for numerous other images.
Or is there any other way I can read each of these tiff images to produce 1D array out of them? Thank you so much for your time.
Use one of the following tools to see the difference between a TIFF file you can read and one you cannot:
exiftool UNHAPPY.TIF
or, with tiffinfo from libtiff:
tiffinfo UNHAPPY.TIF
or, with ImageMagick:
magick identify -verbose UNHAPPY.TIF
My guess would be you have an unsupported compression or pixel type.
Related
I try to read a TIFF file with pillow/PIL (7.2.0) in Python (3.8.3), e.g. this image.
The resulting file seems to be corrupted:
from PIL import Image
import numpy as np
myimage = Image.open('moon.tif')
myimage.mode
# 'L'
myimage.format
# 'TIFF'
myimage.size
# (358, 537)
# so far all good, but:
np.array(myimage)
# shows only zeros in the array, likewise
np.array(myimage).sum()
# 0
It doesn't seem to be a problem of the conversion to numpy array only, since if I save it to a jpg (myimage.save('moon.jpg')) the resulting jpg image has the appropriate dimensions but is all black, too.
Where did I do wrong or is it a bug?
I am not an expert in coding but i had same problem and found the TIFF file has 4 layers. R, G ,B and Alpha. When you convert it using PIL it is black.
try to view the image as plt.imshow(myimage[:, :, 0])
you could also remove the Alpha layer by saving the read image ( i used plt.imread('image')) and then saving it as image=image[:,:,3]. Now its a RGB image.
I don't know if i answered your question, but i felt this info might be of help.
I want to open a .tif image but I always get error for every library I tried to use.
I tried with PIL:
from PIL import Image
img = Image.open('filepath/img_name.tif')
but I get the following error:
UnidentifiedImageError: cannot identify image file 'filepath/img_name.tif'
(This error does not mean that I can not find the file so the directory should be good)
I tried with tifffile:
import tifffile
img = tifffile.imread('filepath/img_name.tif')
I got the following error:
NotImplementedError: unpacking 14-bit integers to uint16 not supported.
I am pretty sure the problem is that the picture because I tried to open a tif image on the internet and it work just by doing this: this is the picture
from PIL import Image
im = Image.open('a_image.tif')
Is there a way to convert my 14-bit picture to a 16-bit picture?
(I know that I could multiply by 4 to get to 16-bit but I do not know how)
I installed imagedecodecs and tifffile has been able to open it
import tifffile
img = tifffile.imread(tif_name)
The problem was that my image was in 14bits.
Perhaps your TIF file has more than one frame. That could be a problem. Try:
from PIL import Image
image = Image.open("animation.tif")
image.seek(1) # skip to the second frame
try:
while 1:
image.seek(image.tell()+1)
# do something to im
except EOFError:
pass # end of sequence
From the documentation.
I currently have a .h5 file containing grayscale imagery. I need to convert it to a .jpg.
Does anybody have any experience with this?
Note: I could possible convert the h5 file to a numpy array and then use an external library like pypng to convert that to a png. But I am wondering if there is a more efficient way to convert to an image, and preferrably a .jpg.
Key ingredients:
h5py to read the h5 file.
Determine the format of your image and use PIL.
Let us suppose it's RGB format (https://support.hdfgroup.org/products/java/hdfview/UsersGuide/ug06imageview.html)
Suppose your image is located at Photos/Image 1 then you can do.
import h5py
import numpy as np
from PIL import Image
hdf = h5py.File("Sample.h5",'r')
array = hdf["Photos/Image 1"][:]
img = Image.fromarray(array.astype('uint8'), 'RGB')
img.save("yourimage.thumbnail", "JPEG")
img.show()
How can I achieve this with Wand library for python:
convert *.png stack_of_multiple_pngs.tiff
?
In particular, how can I read every png image, pack them into a sequence and then save the image as tiff stack:
with Image(filename='*.tiff') as img:
img.save(filename='stack_of_multiple_pngs.tiff')
I understand how to do it for gifs though, i.e. as described in docs. But what about building a sequence as a list and appending every new image I read as a SingleImage()?
Having trouble figuring it out right now.
See also
With wand you would use Image.sequence, not a wildcard filename *.
from wand.image import Image
from glob import glob
# Get list of all images filenames to include
image_names = glob('*.tiff')
# Create new Image, and extend sequence
with Image() as img:
img.sequence.extend( [ Image(filename=f) for f in image_names ] )
img.save(filename='stack_of_multiple_pngs.tiff')
The sequence_test.py file under the test directory will have better examples of working with the image sequence.
I am using the frombuffer command to save DICOM image data as TIFF images. But somehwere throughout this process, the image intensities are inverted (inverted LUT). Any idea on how to overcome this?
I have tried using the ImageOps.invert function from PIL, but if gives me "not supported for this image mode" error.
This is the code I'm using:
import dicom
import Image
import PIL.ImageOps
meta=dicom.read_file("DicomImage.dcm")
imHeight=meta.Rows
imWidth=meta.Columns
imSize=(imWidth,imHeight)
TT=Image.frombuffer("L",imSize,meta.PixelData,"raw","L",0,1)
TT.save("testOUTPUT.tiff","TIFF",compression="none")
Any guidance is appreciated ...
Python 2.7
PIL 1.1.7
Pydicom 0.9.6
Rather than "" for the raw mode, you should be using one of the mode strings from the documentation. Try "L" or "L;I", one or the other should be correct.