Article 57XA4 [SOLVED] Python PIL.Image package won't read TIFF image

[SOLVED] Python PIL.Image package won't read TIFF image

by
rnturn
from LinuxQuestions.org on (#57XA4)
I've been working on a Python3 script to read images and then manipulate them with a variety of the tools available, especially matplotlib and company. Particularly `plot_surface()'. So far, I've been coming up with errors that read
Code:ValueError: shape mismatch: objects cannot be broadcast to a single shapewhen plot_surface() is invoked. The image that causing all the trouble is, according to file(1), height=712, width=711. When read in by my script, "img=Image.open('origimage.tiff')" is succeeding and "print(img)" is telling me that it's:
Code:<PIL.TiffImagePlugin.TiffImageFile image mode=LA size=711x712 at 0x7F7E533860B8>which seems correct. This object is then being converted to a numpy array with "imgarray = np.array(img)" with "print(imgarray.shape)" returning "(712, 711, 2)". So far so good. Maybe. The array/matrix seems transposed or rotated. Anyway, when I try plotting that it's nothing but "ValueError: shape mismatch:" errors all the way down.

Thinking that there's something amiss in the way I'm passing data off to plot_surface() -- say, assuming that the data 711x712 but somewhere along the plotting process it's being seen as 712x711 (i.e., the transposition problem I mentioned) and causing that "shape mismatch" error to occur -- I made a copy of the image and resized it using The GIMP to be 710x710 in the hope that I could get something to come out of plot_surface() besides error messages (I could correct X/Y axis problems later).

Running "file newimage.tiff" show identical results to "file origimage.tiff" except that the new image has been resized to 710x710. Looks OK. But, when running the script, what I now get is:
Code:OSError: cannot identify image file 'newimage.tiff'and I'm even farther away from getting plot_surface() working than before. (When it rains...)

Any ideas as to why PIL.Image would balk at reading what appears to be a perfectly good TIFF file? Neither Okular or gwenview have any trouble reading either image file.

Hints, tips, even wild theories... all are welcomed.

TIA...

UPDATE: After a nice dinner and some extra time to consider the problem, looking at the output of "od -c" for each TIFF file The beginning of the GIMP-resized file was different enough that it got me to thinking about alternatives to using The GIMP to resize. "convert" works well so long as you use "-resize NxN!" instead of merely using "-resize NxN".

The "square" TIFF file can now be read. So the problem is now back to the "shape mismatch" errors.

Marking this thread as solved.latest?d=yIl2AUoC8zA latest?i=Yv6Pq8eEczM:A43xDwhZmJs:F7zBnMy latest?i=Yv6Pq8eEczM:A43xDwhZmJs:V_sGLiP latest?d=qj6IDK7rITs latest?i=Yv6Pq8eEczM:A43xDwhZmJs:gIN9vFwYv6Pq8eEczM
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments