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
Related
Is there a python module that will allow me to read an HDR image's dimensions?
Standard python PIL library can't read HDR images.
HDR references high dynamic range images. Photoshop can read this file format, but I have not been able to find any python module that can read this file format.
Update:
PIL Image.open returns "IOError: cannot identify image file"
imghdr returns None when reading an HDR image file format.
There is a python module called smc.freeimage that can read HDR images. Works well, but the trick is getting the module installed successfully. Make sure you install the cython module first... then try easy_install instead of pip install.
import smc.freeimage as fi
filename = "myimage.hdr"
img = fi.Image(filename)
print "width =",img.width
print "height =",img.height
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.
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 run into several problems when I try to open EPS- or SVG-Images with PIL.
Opening EPS
from PIL import Image
test = Image.open('test.eps')
ends in:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\Lib\site-packages\PIL\Image.py", line 1965, in open
return factory(fp, filename)
File "C:\Python27\Lib\site-packages\PIL\ImageFile.py", line 91, in __init__
self._open()
File "C:\Python27\Lib\site-packages\PIL\EpsImagePlugin.py", line 206, in _open
raise IOError, "bad EPS header"
IOError: bad EPS header
Also opening SVG ends in IOError: cannot identify image file.
The problem is I have to support both formats in my application. Converting to other formats is no alternative. I'm on Windows 7, Python 2.7.2 and PIL 1.1.7.
I uploaded both images: EPS and SVG.
As of today, that is July 2017, reading and converting SVG files can be easily accomplished by importing cairosvg that provides the svg2png function.
Furthermore the svglib development is on again, thus by importing svglib and reportlab, the conversion from svg to png should be easy as well. a matter of 2 calls.
There are alternatives to PIL, but alternatives to PIL are not what you want - There is no
library I know of that would transparently open a vector based drawing and treat it just as any other image, short of opening a web browser and grabbing its render.
For dealing with SVG, there is a recipe using Cairo - which also can handle a lot of other formats, if a bit more difficult to deal with than the PIL API - I think Cairo can also handle EPS - so, you can probably develop your app with pycairo - or pycairo + PIL in the worst case.
The recipe for rendering SVG's is in the answer to: Convert SVG to PNG in Python
(note that you don't have to "convert the file to PNG" - the recipe shows how you render to a cairo surface, which can be displayed, saved to a file, and so on)
Yet, Pillow supports EMF (Enhanced Windows Metafile) which is still vector graphics. I stumbled on this thread as I was searching for convenient mean to convert SVG to EMF anyway.
Your best bet is to use Inkscape to create EMF from SVG:
inkscape --file image.svg --export-emf image.emf
PIL doesn't support svg, but you could try svglib which, unfortunately, does not seem to be under active development:
https://pypi.python.org/pypi/svglib/
https://github.com/deeplook/svglib
While I believe the top answer is better, one is able to leverage Inkscape to render SVG to PNG files.
render_image_command = "inkscape --export-background-opacity=0 --export-width={} --export-type=png --export-filename {} {}"
render_image_command = render_image_command.format(
image_width_in_pixels,
path_to_png_image_to_write,
path_to_svg_image_to_read
)
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!