skimage/imageio fails to read grayscale images - python

I have been reading images using (Python) skimage.imread (or imageio.imread) for months successfully, but now, without changing the code, I get failures when reading grayscale images. My collaborators can read the files. The image properties are:
identify test/resources/biosynth1_cropped/text_removed.png
test/resources/biosynth1_cropped/text_removed.png PNG 1512x315 1512x315+0+0 8-bit sRGB 48c 10094B 0.000u 0:00.005
and some properties with --verbose
Type: Grayscale
Base type: Undefined
Endianness: Undefined
Depth: 8-bit
Channel depth:
Red: 8-bit
Green: 8-bit
Blue: 8-bit
Channel statistics:
Pixels: 476280
The code (run in repl) is:
from skimage import io
import imageio
file="/Users/pm286/workspace/pyamiimage/test/resources/biosynth1_cropped/arrows_removed.png"
img = imageio.imread(file)
and gives errors:
(base) pm286macbook:pyamiimage pm286$ python test_load.py
Traceback (most recent call last):
File "test_load.py", line 9, in <module>
img = imageio.imread(file)
File "/opt/anaconda3/lib/python3.8/site-packages/imageio/core/functions.py", line 265, in imread
reader = read(uri, format, "i", **kwargs)
File "/opt/anaconda3/lib/python3.8/site-packages/imageio/core/functions.py", line 186, in get_reader
return format.get_reader(request)
File "/opt/anaconda3/lib/python3.8/site-packages/imageio/core/format.py", line 170, in get_reader
return self.Reader(self, request)
File "/opt/anaconda3/lib/python3.8/site-packages/imageio/core/format.py", line 221, in __init__
self._open(**self.request.kwargs.copy())
File "/opt/anaconda3/lib/python3.8/site-packages/imageio/plugins/pillow.py", line 298, in _open
return PillowFormat.Reader._open(self, pilmode=pilmode, as_gray=as_gray)
File "/opt/anaconda3/lib/python3.8/site-packages/imageio/plugins/pillow.py", line 138, in _open
as_gray=as_gray, is_gray=_palette_is_grayscale(self._im)
File "/opt/anaconda3/lib/python3.8/site-packages/imageio/plugins/pillow.py", line 689, in _palette_is_grayscale
palette = np.asarray(pil_image.getpalette()).reshape((256, 3))
ValueError: cannot reshape array of size 144 into shape (256,3)
I have (un/re)installed skimage, imageio, and pillow.
(The code seems to read coloured images satisfactorily.)
I'd be grateful for pointers to solutions.
EDIT.The comment from #Nick ODell seems to have identified the problem.
Has the dimensionality of the output changed from (x, y)to (x, y, 3)?

This is a bug which was fixed in imageio version 2.16.2. The issue is caused by Pillow returning image palettes with less than 256 colors.
To fix it, upgrade to imageio version 2.16.2 or later.
More information:
GH issue
Changelog

Related

How to work around skimage.io.imread() error and re-save image correctly?

The below error is thrown when trying to read the image URL referenced below. (Note, I can't even upload the image to SO because it throws an error when I try to upload it.)
https://s3.amazonaws.com/comicgeeks/comics/covers/large-7441962.jpg
image = imread('https://s3.amazonaws.com/comicgeeks/comics/covers/large-7441962.jpg', as_gray=True)
This is the stack trace.
Traceback (most recent call last):
....
return imread(image_or_path, as_gray=True)
File "skimage/io/_io.py", line 48, in imread
img = call_plugin('imread', fname, plugin=plugin, **plugin_args)
File "skimage/io/manage_plugins.py", line 209, in call_plugin
return func(*args, **kwargs)
File "skimage/io/_plugins/imageio_plugin.py", line 10, in imread
return np.asarray(imageio_imread(*args, **kwargs))
File "imageio/__init__.py", line 86, in imread
return imread_v2(uri, format=format, **kwargs)
File "imageio/v2.py", line 159, in imread
with imopen(uri, "ri", plugin=format) as file:
File "imageio/core/imopen.py", line 333, in imopen
raise err_type(err_msg)
ValueError: Could not find a backend to open `/var/folders/82/rky4yjcx75n1zskhy5570v0m0000gn/T/tmpy9xg7dvb.jpg`` with iomode `ri`.
After looking into this further I believe the image was originally a TIFF file that was just renamed to a .jpg file manually, but I'm not sure. If I download the file and try to open it with Photoshop I get the following message.
Could not open “large-7441962.jpeg” because an unknown or invalid JPEG marker type is found.
If I simply change the extension to a .tiff file it will not open as it states it is an invalid tiff file.
The only way I can open it with photoshop is if I open it with the preview.app and then save a copy of the image as a .tiff file. Then I can open it in photoshop.
This is an issue with a potentially large number of images so re-saving them one-by-one is not an option.
Are there any possible ways to re-save this file when this error is thrown? Or somehow figure out how to handle it even though imread() is failing?
I was able to work around this by using the following.
from PIL import Image
from skimage.io import imread
try:
image = imread(url, as_gray=True)
return image
except:
image = Image.open(requests.get(url, stream=True).raw)
return image
However it is worth noting that when having to make this request using PIL it is significantly slower.

Errors importing 16-bit TIFF in Pillow 7.1.2

I recently updated Pillow from version 6.1.2 to version 7.1.2 and tried to load a 16-bit tiff image, which I mainly work with on a daily basis. Prior to the update, I could load these images fine, but now it raises an error:
q = Image.open(file)
q
Traceback (most recent call last):
File "C:\Users\martinkenny\AppData\Local\Continuum\anaconda2\envs\tftwoenv\lib\site-packages\IPython\core\formatters.py", line 345, in __call__
return method()
File "C:\Users\martinkenny\AppData\Local\Continuum\anaconda2\envs\tftwoenv\lib\site-packages\PIL\Image.py", line 671, in _repr_png_
self.save(b, "PNG")
File "C:\Users\martinkenny\AppData\Local\Continuum\anaconda2\envs\tftwoenv\lib\site-packages\PIL\Image.py", line 2100, in save
self._ensure_mutable()
File "C:\Users\martinkenny\AppData\Local\Continuum\anaconda2\envs\tftwoenv\lib\site-packages\PIL\Image.py", line 617, in _ensure_mutable
self._copy()
File "C:\Users\martinkenny\AppData\Local\Continuum\anaconda2\envs\tftwoenv\lib\site-packages\PIL\Image.py", line 610, in _copy
self.load()
File "C:\Users\martinkenny\AppData\Local\Continuum\anaconda2\envs\tftwoenv\lib\site-packages\PIL\TiffImagePlugin.py", line 1070, in load
return self._load_libtiff()
File "C:\Users\martinkenny\AppData\Local\Continuum\anaconda2\envs\tftwoenv\lib\site-packages\PIL\TiffImagePlugin.py", line 1182, in _load_libtiff
raise OSError(err)
OSError: -2
Out[21]: <PIL.TiffImagePlugin.TiffImageFile image mode=I;16 size=1024x1024 at 0x180FD99E438>
From looking at other answers, I see that Pillow has had issues with 16-bit images before but I thought they had been resolved. Is there a way to work around this error and access the image data? Or do I need to downgrade to my previous version?

How to resolve TiffFileError: Not a Tiff File and Byte Problem with KeyError: b'\x89P'

I am setting up a computer vision project to detect and process GFP proteins. I keep getting errors about my file not being a Tiff Image and a Byte Error. I don't quite understand what they mean and haven't found anything about it online.
I have already made sure that the file path is correct and have tried changing the file into Tiff Format. Now on Finder, it says that it is a TIFF Image but still gives an error.
import tifffile
from colicoords import Data, Cell, CellPlot
import matplotlib.pyplot as plt
binary_img = tifffile.imread('organoid_images/gfp/cells1.tif')
data = Data()
data.add_data(binary_img, 'binary')
cell = Cell(data)
cell.optimize()
cp = CellPlot(cell)
plt.figure()
cp.imshow('flu_514', cmap='viridis', interpolation='nearest')
cp.plot_outline()
cp.plot_midline()
plt.show()
Error Message:
Traceback (most recent call last):
File "/Users/CosmoCrash/opencvblobs/lib/python3.7/site-packages/tifffile/tifffile.py", line 2236, in __init__
byteorder = {b'II': '<', b'MM': '>'}[header[:2]]
KeyError: b'\x89P'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "gfp.py", line 6, in <module>
binary_img = tifffile.imread('organoid_images/gfp/cells1.tif')
File "/Users/CosmoCrash/opencvblobs/lib/python3.7/site-packages/tifffile/tifffile.py", line 715, in imread
with TiffFile(files, **kwargs_file) as tif:
File "/Users/CosmoCrash/opencvblobs/lib/python3.7/site-packages/tifffile/tifffile.py", line 2238, in __init__
raise TiffFileError('not a TIFF file')
tifffile.tifffile.TiffFileError: not a TIFF file
Your file starting \x89P is a PNG file, not TIFF, as that is a PNG signature, whereas TIFF files start II if in Intel order, or MM if in Motorola order.
If on Linux/macOS, try running:
file cells1.tif
See Wikipedia for description of PNG signature, as suggested by Warren.
See Wikipedia for description of TIFF header.

JPEG-Decompression on RAW image in python

When I try to JPEG-Decompress (old-style JPEG compression, not JPEG-LS and not JPEG2000) the RAW data, I get following error:
Traceback (most recent call last):
File "raw-reader.py", line 766, in <module>
raw_image_data = imageio.imread(io.BytesIO(raw_packed_image_data))
File "/home/ian/.local/lib/python3.6/site-packages/imageio/core/functions.py", line 206, in imread
reader = read(uri, format, 'i', **kwargs)
File "/home/ian/.local/lib/python3.6/site-packages/imageio/core/functions.py", line 129, in get_reader
return format.get_reader(request)
File "/home/ian/.local/lib/python3.6/site-packages/imageio/core/format.py", line 168, in get_reader
return self.Reader(self, request)
File "/home/ian/.local/lib/python3.6/site-packages/imageio/core/format.py", line 217, in __init__
self._open(**self.request.kwargs.copy())
File "/home/ian/.local/lib/python3.6/site-packages/imageio/plugins/pillow.py", line 398, in _open
pilmode=pilmode, as_gray=as_gray)
File "/home/ian/.local/lib/python3.6/site-packages/imageio/plugins/pillow.py", line 122, in _open
self._im = factory(self._fp, '')
File "/home/ian/.local/lib/python3.6/site-packages/PIL/JpegImagePlugin.py", line 780, in jpeg_factory
im = JpegImageFile(fp, filename)
File "/home/ian/.local/lib/python3.6/site-packages/PIL/ImageFile.py", line 102, in __init__
self._open()
File "/home/ian/.local/lib/python3.6/site-packages/PIL/JpegImagePlugin.py", line 339, in _open
handler(self, i)
File "/home/ian/.local/lib/python3.6/site-packages/PIL/JpegImagePlugin.py", line 166, in SOF
raise SyntaxError("cannot handle %d-bit layers" % self.bits)
SyntaxError: cannot handle 14-bit layers
The RAW data in the image is 14-bit JPEG data, and imageio isn't able to read it. When I tried using pillow, it didn't even recognize the data as JPEG. My question now is: How can I decompress the data without writing my own JPEG decompressor, while keeping in mind that the data is 14 bits?
My code:
import io
import imageio
allbytes = open("raw_data.dat", "rb").read()
raw_packed_image_data = allbytes
raw_image_data = imageio.imread(io.BytesIO(raw_packed_image_data))
The file raw_data.dat is a file containing purely the RAW-Image data compressed with JPEG. Link to raw_data.dat
raw_data.dat is a JPEG Lossless, Nonhierarchical file with 2 frames and a precision > 8-bit, a very rare format.
The imagecodecs package can read the file (assuming that the _imagecodecs Cython extension is present):
>>> from imagecodecs import jpegsof3_decode
>>> data = open('raw_data.dat', 'rb').read()
>>> image = jpegsof3_decode(data)
>>> image.shape
(3528, 2640, 2)
>>> image.dtype
dtype('uint16')
The LEADTOOLS SDK should also be able to read the file (not tested).
I believe the issue can be solved using a different library to load the image. Similar underlying issue has been posted here How to combine 3 high range JPEG2000 images into single RGB one?. You can use something along these lines:
import matplotlib.image as mpimg
img_red = mpimg.imread('raw_data.dat')
Then you can use the read bitmap for further manipulation as if it was loaded via PIL/Pillow/imageio.

Using PIL modifying and then saving a TIFF returns error

TLDR; I'm trying to take a TIFF, resize it, then save it. However it returns an error. This works fine if I change the saved filetype to png or jpg.
System: Windows 7
Tried using both Python 3.4 and 2.7.
Code:
from PIL import Image
try: #test file exists
im = Image.open(r"c:\temp\file.tif")
except:
print("Error opening image")
multiply = 5 #how much bigger
processing = tuple([multiply*x for x in im.size]) #maths
saved = (r"c:\temp\biggerfile.tif") #save location
imB = im.resize((processing)) #resizing
imB.save(saved) #saving
I need to resize a TIFF because I'm using tesseract-ocr, and resizing the image to get a better output. The program seems to work best with a TIFF.
The error I receive is:
_TIFFVSetField: c:\temp\biggerfile.tif: Bad value 2 for "ExtraSamples" tag.
Traceback (most recent call last):
File "step1.py", line 15, in <module>
imB.save(saved)
File "C:\Python34\lib\site-packages\PIL\Image.py", line 1684, in save
save_handler(self, fp, filename)
File "C:\Python34\lib\site-packages\PIL\TiffImagePlugin.py", line 1185, in _save
e = Image._getencoder(im.mode, 'libtiff', a, im.encoderconfig)
File "C:\Python34\lib\site-packages\PIL\Image.py", line 430, in _getencoder
return encoder(mode, *args + extra)
RuntimeError: Error setting from dictionary
Thanks!
Try to install libtiff
http://gnuwin32.sourceforge.net/packages/tiff.htm
File "C:\Python34\lib\site-packages\PIL\TiffImagePlugin.py", line 1185, in _save
e = Image._getencoder(im.mode, 'libtiff', a, im.encoderconfig)
Looks like that's the error that is holding you up. It's trying to access libtiff and you don't have it installed so it's failing.
Had the same issue, when using PIL to combine multiple images to one and adding a label.
I could fix this easily by converting the .tif file to a .png file in MS Paint (pls don't hate me for using MS :D). Quality of the final merged image was not reduced.

Categories

Resources