简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM