473,395 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Copying TIFF tags with PIL

dshimer
136 Expert 100+
I am doing some basic modification to existing TIFF files using PIL (1.6 in python 2.5). When I open the image there is quite a bit of info in the tags dict that I would like to save with the new image. Apparently (based on other forums I've seen) once you process the image all the TIFF specific data gets lost. I can copy the info but can't find an example of how to copy or re-assign it to the new image. For example

Expand|Select|Wrap|Line Numbers
  1. import Image,TiffImagePlugin
  2. im=Image.open('oldname.tif','r')
  3. tags=im.tag.tags
  4. im.rotate(90,0).save('newname.tif')
  5.  
would open an image named oldname.tif, save the tags, then save a new image that has been rotated by 90. How can I assign those tags back to the new image?
Jan 11 '07 #1
8 9169
bartonc
6,596 Expert 4TB
I am doing some basic modification to existing TIFF files using PIL (1.6 in python 2.5). When I open the image there is quite a bit of info in the tags dict that I would like to save with the new image. Apparently (based on other forums I've seen) once you process the image all the TIFF specific data gets lost. I can copy the info but can't find an example of how to copy or re-assign it to the new image. For example

Expand|Select|Wrap|Line Numbers
  1. import Image,TiffImagePlugin
  2. im=Image.open('oldname.tif','r')
  3. tags=im.tag.tags
  4. im.rotate(90,0).save('newname.tif')
  5.  
would open an image named oldname.tif, save the tags, then save a new image that has been rotated by 90. How can I assign those tags back to the new image?
I don't see any reference to setting the tag attr in the docs, but have you tried
im2 = im.rotate(90,0)
im2.tag.tags = tags
to store the saved tags into the new image?
Jan 11 '07 #2
dshimer
136 Expert 100+
That give me...

Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "C:\Python25\Lib\site-packages\PIL\Image.py", line 493, in __getattr__
raise AttributeError(name)
AttributeError: tag

I don't totally understand what is going on under the hood, but it seems (and I stress seems) that the tag attribute belongs to the TIFF image object, once you modify the image data in this case with the rotate command, it just becomes raw image data. When it is saved, the proper encoder is called based on the format provided, or the extension. This being the case im2 doesn't have a tag attribute that can be set. I have played with several ways of trying to have the save function do it as part of the re-encoding, but haven't had any luck with that either.

I don't see any reference to setting the tag attr in the docs, but have you tried
im2 = im.rotate(90,0)
im2.tag.tags = tags
to store the saved tags into the new image?
Jan 11 '07 #3
bartonc
6,596 Expert 4TB
That give me...

Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "C:\Python25\Lib\site-packages\PIL\Image.py", line 493, in __getattr__
raise AttributeError(name)
AttributeError: tag

I don't totally understand what is going on under the hood, but it seems (and I stress seems) that the tag attribute belongs to the TIFF image object, once you modify the image data in this case with the rotate command, it just becomes raw image data. When it is saved, the proper encoder is called based on the format provided, or the extension. This being the case im2 doesn't have a tag attribute that can be set. I have played with several ways of trying to have the save function do it as part of the re-encoding, but haven't had any luck with that either.
It looks like there is a get() and set() for tag data.
I hope this link proves useful. Google may have optimized itself for the searches that I do often; that link was at the top of the list for "python tiff tag attributes" search. I'll keep trying to get more documentation.
Jan 11 '07 #4
dshimer
136 Expert 100+
I appreciate it, I had looked at that page, the underlying code has already been incorporated into PIL 1.1.6 which is probably where the core dpi support comes from. It would probably be a great place to start though for writing a complete tag copy function.

I really have two issues, image processing is a huge part of my work and if I open a file in 4 different programs (including PIL based) there is a lot of inconsistancy in whether or not the TIFF dpi parameters get transferred correctly. Couple this with the amount of stuff that different disciplines try to cram into a TIFF (for example GeoTiff) and it really gets unwieldy. Hence the thought that if I could just completely copy the complete set, most of the problems would decrease. I think your steering me in the right direction, I'll just need some time since programming is a side issue.


It looks like there is a get() and set() for tag data.
I hope this link proves useful. Google may have optimized itself for the searches that I do often; that link was at the top of the list for "python tiff tag attributes" search. I'll keep trying to get more documentation.
Jan 12 '07 #5
bartonc
6,596 Expert 4TB
I appreciate it, I had looked at that page, the underlying code has already been incorporated into PIL 1.1.6 which is probably where the core dpi support comes from. It would probably be a great place to start though for writing a complete tag copy function.

I really have two issues, image processing is a huge part of my work and if I open a file in 4 different programs (including PIL based) there is a lot of inconsistancy in whether or not the TIFF dpi parameters get transferred correctly. Couple this with the amount of stuff that different disciplines try to cram into a TIFF (for example GeoTiff) and it really gets unwieldy. Hence the thought that if I could just completely copy the complete set, most of the problems would decrease. I think your steering me in the right direction, I'll just need some time since programming is a side issue.
Some Gui Tool Kits have good graphics support. Here is a snippet from wxPython help:
Expand|Select|Wrap|Line Numbers
  1. wxImage
  2. This class encapsulates a platform-independent image. An image can be created from data, or using wxBitmap::ConvertToImage. An image can be loaded from a file in a variety of formats, and is extensible to new formats via image format handlers. Functions are available to set and get image bits, so it can be used for basic image manipulation.
  3. A wxImage cannot (currently) be drawn directly to a wxDC. Instead, a platform-specific wxBitmap object must be created from it using the wxBitmap::wxBitmap(wxImage,int depth) constructor. This bitmap can then be drawn in a device context, using wxDC::DrawBitmap.
  4. One colour value of the image may be used as a mask colour which will lead to the automatic creation of a wxMask object associated to the bitmap object.
  5. Alpha channel support
  6. Starting from wxWidgets 2.5.0 wxImage supports alpha channel data, that is in addition to a byte for the red, green and blue colour components for each pixel it also stores a byte representing the pixel opacity. An alpha value of 0 corresponds to a transparent pixel (null opacity) while a value of 255 means that the pixel is 100% opaque.
  7. Unlike RGB data, not all images have an alpha channel and before using GetAlpha you should check if this image contains an alpha channel with HasAlpha. Note that currently only images loaded from PNG files with transparency information will have an alpha channel but alpha support will be added to the other formats as well (as well as support for saving images with alpha channel which also isn't implemented).
  8. Available image handlers
  9. The following image handlers are available. wxBMPHandler is always installed by default. To use other image formats, install the appropriate handler with wxImage::AddHandler or wxInitAllImageHandlers.
  10. wxBMPHandler 
  11. For loading and saving, always installed. 
  12. wxPNGHandler 
  13. For loading (including alpha support) and saving. 
  14. wxJPEGHandler 
  15. For loading and saving. 
  16. wxGIFHandler 
  17. Only for loading, due to legal issues. 
  18. wxPCXHandler 
  19. For loading and saving (see below). 
  20. wxPNMHandler 
  21. For loading and saving (see below). 
  22. !!!!!!!!!!!!!!!!!
  23. wxTIFFHandler 
  24. For loading and saving. 
  25. !!!!!!!!!!!!!!!!
  26. wxIFFHandler 
  27. For loading only. 
  28. wxXPMHandler 
  29. For loading and saving. 
  30. wxICOHandler 
  31. For loading and saving. 
  32. wxCURHandler 
  33. For loading and saving. 
  34. wxANIHandler 
  35. For loading only. 
  36. When saving in PCX format, wxPCXHandler will count the number of different colours in the image; if there are 256 or less colours, it will save as 8 bit, else it will save as 24 bit.
  37. Loading PNMs only works for ASCII or raw RGB images. When saving in PNM format, wxPNMHandler will always save as raw RGB.
so maybe this is a good option.
Jan 12 '07 #6
bartonc
6,596 Expert 4TB
Here is a very interisting page.
I actually linked there from here.
Jan 13 '07 #7
dshimer
136 Expert 100+
Here is a very interisting page.
I actually linked there from here.
Wow that looks like some neat stuff. Now all I need is for production to slow down a little so I can dive in.

This whole problem started with PIL 1.1.6 not setting dpi correctly. I really need to copy the entire tag of the original TIFF file but the real emergency came from not being able to properly manage the dpi. I did get a reply from the image-sig that points out a patch which at least corrects that problem.

The Tiff image plugin handles the RESOLUTION_UNIT field incorrectly,
see http://mail.python.org/pipermail/ima...ly/004011.html.

Markus Kemmerling
Medical University Vienna
I have applied and tested the indicated patch in 1.1.6 and all is now well as regards dpi! Hooray!
Jan 16 '07 #8
bartonc
6,596 Expert 4TB
Wow that looks like some neat stuff. Now all I need is for production to slow down a little so I can dive in.

This whole problem started with PIL 1.1.6 not setting dpi correctly. I really need to copy the entire tag of the original TIFF file but the real emergency came from not being able to properly manage the dpi. I did get a reply from the image-sig that points out a patch which at least corrects that problem.


I have applied and tested the indicated patch in 1.1.6 and all is now well as regards dpi! Hooray!
Glad to hear that you got it working. Thanks for the update, D!
Jan 16 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Skwerl | last post by:
Hi guys. I've written code to embed an ICC profile in a TIFF image, and I think my IO operations are slowing things down. It is taking about a second to embed each tag in 7-meg TIFF files. ...
0
by: Robert Seidl | last post by:
I saw that VB .Net 2003 has some tools for easily reading metadata information present in some TIFF and JPG files, eg. ---------------------------------------------------------------- .... Dim b...
5
by: Eddie Leung | last post by:
Hi All, As I know that the tiff contain header information, I can use one software called tifftags.exe to get tiff tag information. However, I can't get any reference from vb.net to do such...
5
by: mesh2005 | last post by:
I'm new to C# , I use MS visual studio.NET 2003. I have some TIFF images, I want to write a program using c# that reads an image (.tiff) and prints all its tags. can anyone guid me to the right...
2
by: Asfar | last post by:
I am using GDI+ to load a tiff image and write a string to it and save the tiff image to another file. I can save the image with only LZW Compression and not abel to save with CCITT4...
0
by: O Evans | last post by:
Hi there, I'm manipualating tiff images captured by a program that insists on using annoying private tags. I want to be able to import an image that I have created into the program but I cannot...
5
by: Sameer Gupta | last post by:
what are options for opening / handling tiff files in .Net framework ? which is the best library, namespace ? Please help Regards Sameer Gupta C# Designer & Developer Siemens UK
2
by: Jack | last post by:
Hello, I need to check TIFF or JPG file to get the DPI they were scanned in at 200, 300, etc. Any help would be great. Thanks in advance for any help, Jack
1
by: BuncyTheFrog | last post by:
Working in C# in Visual Studio 2005 So I know how to edit TIFF tags (get a propertyItem object, edit its details, and stick it into a bitmap), but saving them is presenting itself as a problem. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.