I'm working on large satellite image files in the .tif format. To start, I am just trying to open the files and view them using PIL. Here is the code I have written so far:
from PIL import Image
import os.path
script_dir = os.path.dirname(os.path.abspath(__file__))
im = Image.open(os.path.join(script_dir, 'orthoQB02_11JUL040015472-M1BS-101001000DB70900_u16ns3413.tif'))
im.show()
Unfortunately, I am receiving the error message:
IOError Traceback (most recent call last)
/Applications/Canopy.app/appdata/canopy-1.3.0.1715.macosx-x86_64/Canopy.app/Contents/lib/python2.7/site-packages/IPython/utils/py3compat.pyc in execfile(fname, *where)
202 else:
203 filename = fname
----> 204 __builtin__.execfile(filename, *where)
/Users/zlazow/Desktop/Geo Research Files/documents-export-2014-02-13 (3)/showfiles.py in <module>()
3
4 script_dir = os.path.dirname(os.path.abspath(__file__))
----> 5 im = Image.open(os.path.join(script_dir, 'orthoQB02_11JUL040015472-M1BS-101001000DB70900_u16ns3413.tif'))
6 im.show()
/Users/zlazow/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/PIL/Image.pyc in open(fp, mode)
1978 pass
1979
----> 1980 raise IOError("cannot identify image file")
1981
1982 #
IOError: cannot identify image file
Are the image files simply too large for PIL? I can open one of the smaller (200MB) .tif files in the Preview Application, but when I try to open it using PIL it creates a BMP image that opens in Preview, but the image never loads.
All of the rest of the files (300MB++) will not open with Preview or PIL at all.
Thanks for any assistance.
The Image constructor looks through its internal list of formats (depends on how PIL was compiled) and asks each one if it can parse the file.
As an input to the detector function, the first few bytes of the image file is used. By looking inside the TIFF image reader, it looks for one of the following magic bytes:
["MM\000\052", "II\052\000", "II\xBC\000"]
As indicated by the error message, the detector fails while reading the first few bytes of the file, way before it has gotten to read the dimensions of the image. One of the following reasons appear more likely:
The file is corrupt
The file is not a TIFF image
The file is some exotic/new TIFF sub-format that PIL can't understand
And as for solution, I would suggest:
Use the file command to try to identify the file format, e.g.
file orthoQB02_11JUL040015472-M1BS-101001000DB70900_u16ns3413.tif
which should print something like
Untitled.tiff: TIFF image data, big-endian
Try to open the file in e.g. Photoshop and see if it can understand the file.
Inspect the header manually, see if the file starts with the magic bytes above.
EDIT: Since you identified the format (BigTIFF), you have two options: Convert it or find a Python library to load it. http://bigtiff.org has unofficial libtiff versions with BigTIFF built in. You could try to compile pylibtiff against this libtiff version, or use ImageMagick (compiled with BigTIFF support) to convert the images to regular TIFF files first.
Related
Can anyone help me? I'm trying to copy all the metadata from one 3D tiff image to another in python. This is very easy to do with a perl based program:
exiftool -tagsfromfile <source-file> <target-file>
But that is not an easy install to use as a dependency in my python pipeline. There are similar Py libraries that easily to read the metadata tags of a tif image:
import exifread
with open("img_w_metadata.tiff", 'rb') as f:
tags = exifread.process_file(f)
or:
import piexif
exif_data = piexif.load("img_w_metadata.tiff")
But altering that data/inserting new one is very difficult. Exifread has no documentation about it and piexif seems to only work with jpg files, giving error if you provide tif:
# Open TIFF file for writing # THIS ERASES THE IMAGE CONTENTS
with open("img_no_metadata", "wb") as f:
exif_bytes = piexif.dump(exif_data)
f.write(exif_bytes)
Error:
Traceback (most recent call last):
piexif.insert(exif_bytes, img_new) File "C:\Users\......\Python39\site-packages\piexif\_insert.py",line 39, in insert
raise InvalidImageDataError piexif._exceptions.InvalidImageDataError
The Pillow library works well with metadata but does not work with 3D images! opencv works well with 3D images but not with metadata, the Tifffile library is also erasing the second image content, hence I'm trying Exif parsers
Cheers,
Ricardo
I'm struggling to properly open a TIFF image from an instance of Python's io.BufferedReader class. I download the image from a GCS path using the below lib, but I can't open seem to open the image with traditional tools.
# returns the <_io.BufferedReader>
file = beam.io.gcp.gcsio.GcsIO().open("<GCS_PATH>", 'r')
from PIL import Image
img = Image.open(file.read()) <---- Fails with "TypeError: embedded NUL character"
img = Image.open(file.raw) <--- Fails when any operations are performed with "IOError(err)"
I am open to other libraries besides PIL.
UPDATE
The following also fails:
img = Image.open(file)
It fails with an IOError, stating tempfile.tif: Cannot read TIFF header.
Make sure you wrap both in a ContextManager so they both get closed properly.
with beam.io.gcp.gcsio.GcsIO().open(file_path, 'r') as file, Image.open(io.BytesIO(file.read())) as multi_page_tiff:
do_stuff()
I am trying to change the dpi of my PNG images and convert them to TIFF using Pillow/PIL like so,
from PIL import Image
import os
for fl in os.listdir(os.getcwd()):
name, ext = fl.split(".")
im = Image.open(fl)
im.save(name + ".tiff", dpi=(500,500), compression="tiff_jpeg")
print("Done '{}'".format(name))
which works fine if the compression kwarg is not set, but I end up with massive 100MB TIFF files from my 1MB PNGs. If I set the compression type to any of the available options, I end up with the following error:
Traceback (most recent call last):
File "<ipython-input-1-3631f05e05f4>", line 7, in <module>
im.save(name + ".tiff", dpi=(500,500), compression="tiff_jpeg")
File "C:\Users\Patrick\Anaconda3\lib\site-packages\PIL\Image.py", line 1687, in save
save_handler(self, fp, filename)
File "C:\Users\Patrick\Anaconda3\lib\site-packages\PIL\TiffImagePlugin.py", line 1457, in _save
raise IOError("encoder error %d when writing image file" % s)
OSError: encoder error -2 when writing image file
In the docs for the Image.save method it mentions that compression is only available if the libtiff library is installed which I do have.
Here is the versions for Python and Pillow that I'm working with:
Python 3.5.1 |Anaconda 4.0.0 (64-bit)| (default, Feb 16 2016, 09:49:46) [MSC v.1900 64 bit (AMD64)] on win32
libtiff: 4.0.6-vc14_2 [vc14]
pillow: 3.2.0-py35_1
What might be the cause of this error and what steps can I take to resolve? This is the first time I've used Pillow/PIL and am unsure of where to begin.
Just got bitten by this myself so I thought I'd share my finds despite the question being a few months old.
The tiff_jpeg compression refers to the "old-style" JPEG-encoded TIFF files and is now obsolete. Using compression='jpeg' instead of compression='tiff_jpeg' worked for me.
The available options for compression can currently be found in the COMPRESSION_INFO dict at https://github.com/python-pillow/Pillow/blob/master/PIL/TiffImagePlugin.py. The tiff_lzw compression also isn't mentioned in the docs but worked for creating an LZW encoded TIFF for me using Pillow 3.4 on Windows.
I had a very similar issue with 'group4' compression on B&W tiffs. I solved it by changing the image array values from 0, 255 to True, False.
src = <your B&W tiff file>
dst = <output filename>
# Open PIL.Image
img = Image.open(src)
# Change img values
a = np.array(img)
a = np.array([x == 255 for x in a])
img = Image.fromarray(a)
# Save Out Compressed Tiff
img.save(dst, compression='group4')
Not a direct answer to this question, but might help someone who is experiencing similar issues in the future.
I've also been getting OSError: encoder error -2 when writing image file when trying to save a TiffImageFile with compression. This error was happening when trying to to apply tiff_ccitt, group3, or group4 compression options.
I fixed it by simply converting the TIFF to monochrome with image.convert('1') before compressing it. Note that the compression algorithms I mentioned are only used for monochrome images, so we are not losing any colour by adding this step.
How I compressed and saved a multi-page TIFF:
# convert all images to monochrome. Without this step CCITT compression will fail.
images = [image.convert('1') for image in images]
images[0].save(out_path,
compression="tiff_ccitt",
save_all=True,
append_images=images[1:])
I use uvccapture to take pictures and want to process them with the help of python and the python imaging library (PIL).
The problem is that PIL can not open those images. It throws following error message.
Traceback (most recent call last):
File "process.py", line 6, in <module>
im = Image.open(infile)
File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 1980, in open
raise IOError("cannot identify image file")
IOError: cannot identify image file
My python code looks like this:
import Image
infile = "snap.jpg"
im = Image.open(infile)
I tried to save the images in different formats before processing them. But this does not help. Also changing file permissions and owners does not help.
The only thing that helps is to open the images, for example with jpegoptim, and overwriting the old image with the optimized one. After this process, PIL can deal with these images.
What is the problem here? Are the files generated by uvccapture corrupt?
//EDIT: I also found out, that it is not possible to open the images, generated with uvccapture, with scipy. Running the command
im = scipy.misc.imread("snap.jpg")
produces the same error.
IOError: cannot identify image file
I only found a workaround to this problem. I processed the captured pic with jpegoptim and afterwords PIL could deal with the optimized image.
I am writing a script that changes the resolution of a floating point 2K (2048x2048) tiff image to 1024x1024.
But I get the following error:
File "C:\Python26\lib\site-packages\PIL\Image.py", line 1916, in open
IOError: cannot identify image file
My Code:
import Image
im = Image.open( inPath )
im = im.resize( (1024, 1024) , Image.ANTIALIAS )
im.save( outPath )
Any Ideas?
Download My Image From This Link
Also I'm using pil 1.1.6. The pil install is x64 same as the python install (2.6.6)
Try one of these two:
open the file in binary mode,
give the full path to the file.
HTH!
EDIT after testing the OP's image:
It definitively seems like is the image having some problem. I'm on GNU/Linux and couldn't find a single program being able to handle it. Among the most informative about what the problem is have been GIMP:
and ImageMagik:
display: roadnew_disp27-dm_u0_v0_hr.tif: invalid TIFF directory; tags are not sorted in ascending order. `TIFFReadDirectory' # warning/tiff.c/TIFFWarnings/703.
display: roadnew_disp27-dm_u0_v0_hr.tif: unknown field with tag 18 (0x12) encountered. `TIFFReadDirectory' # warning/tiff.c/TIFFWarnings/703.
I did not try it myself, but googling for "python tiff" returned the pylibtiff library, which - being specifically designed for TIFF files, it might perhaps offer some more power in processing this particular ones...
HTH!