'numpy.array() is not converting tiff image to 2D array
I have been trying to figure out why the numpy.array() method is converting my tiff image into an object (dtype=object). I tried this with other tiff images and I did not encounter this problem. I am pretty sure it has to do something with how my "actin2.tif" image is acquired. I just need to be able to convert this particular tiff image to a 2D array. This is what my code looks like:
>>> import numpy
>>> from PIL import Image
>>> a = Image.open('actin2.tif')
>>> a_array = numpy.array(a)
>>> a
<PIL.TiffImagePlugin.TiffImageFile image mode=I;16B size=37x58 at 0x14BBC68>
>>> a_array
array(<PIL.TiffImagePlugin.TiffImageFile image mode=I;16B size=37x58 at 0x14BBC68>, dtype=object)
I eventually need to be able to manipulate the value of the pixels in the image and I cannot do that without the image being converted to a 2D array. Currently, this is the error thrown when I try to manipulate the array:
structure_masked = numpy.multiply(structure_mask,image)
TypeError: unsupported operand type(s) for *: 'bool' and 'instance'
which is a result from this line of code:
structure_masked = numpy.multiply(structure_mask,image)
"structure_mask" and "image" were converted to numpy arrays in the same way as mentioned above.
I have tried changing mode and dtype but that does not seem to be working, any suggestions?
Solution 1:[1]
Ran across this same problem. Turned out we were running PIL instead of Pillow. This fixed it for us:
sudo pip uninstall PIL
sudo pip install pillow -U
Solution 2:[2]
>>> a = Image.open('actin2.tif')
>>> a_array = numpy.array(a)
>>> a # you are printing the image not array.
you are printing image instead of array. It should be >>>a_array
instead of >>>a
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Scott Shellington |
| Solution 2 | Naveen Kumar |
