Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 09-08-2020, 04:09 PM   #1
Senior Member
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: Currently: openSUSE, Raspbian, Slackware. Formerly: CentOS, MacOS, Red Hat. Other: Solaris, Tru64
Posts: 2,059

Rep: Reputation: 338Reputation: 338Reputation: 338Reputation: 338
Python PIL.Image package won't read TIFF image

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
ValueError: shape mismatch: objects cannot be broadcast to a single shape
when 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, "'origimage.tiff')" is succeeding and "print(img)" is telling me that it's:
<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:
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.


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.

Last edited by rnturn; 09-08-2020 at 08:48 PM. Reason: Found a means of correctly resize the image to make it readable.
Old 09-09-2020, 09:24 PM   #2
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,434
Blog Entries: 11

Rep: Reputation: 3398Reputation: 3398Reputation: 3398Reputation: 3398Reputation: 3398Reputation: 3398Reputation: 3398Reputation: 3398Reputation: 3398Reputation: 3398Reputation: 3398
Thanks for returning to update your progress!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] No package 'x11' found No package 'xext' found No package 'xdamage' found No package 'xfixes' found No package 'x11-xcb' found Jigsaw Linux From Scratch 12 04-25-2019 07:33 AM
[SOLVED] Install PIL on different version of Python dc_eros Slackware 2 01-07-2013 09:16 PM
Use PIL in Python to simple composite images browny_amiga Programming 1 06-25-2010 08:57 AM
LXer: Python Python Python (aka Python 3) LXer Syndicated Linux News 0 08-05-2009 08:30 PM
Single Page tiff to Multi Page tiff James_dean Programming 3 11-10-2005 10:50 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:14 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration