We're Hiring!

How to read OME-tiff file and extract metadata in python

General and open developer discussion about using OMERO APIs from C++, Java, Python, Matlab and more! Please new questions at https://forum.image.sc/tags/omero
Please note:
Historical discussions about OMERO. Please look for and ask new questions at https://forum.image.sc/tags/omero

If you are having trouble with custom code, please provide a link to a public repository, ideally GitHub.

How to read OME-tiff file and extract metadata in python

Postby pz3977 » Mon Aug 25, 2014 4:51 pm

Hi,

I am looking for a piece of python code which reads a OME-tiff, or simply a tiff file and give me a list of metadata. I found a very complicated one in : https://svn.broadinstitute.org/CellProf ... ifffile.py , but I need a simple one!

Could you please help me with that!

Best regards,
pz3977
pz3977
 
Posts: 31
Joined: Thu Jul 10, 2014 11:39 am

Re: How to read OME-tiff file and extract metadata in python

Postby wmoore » Tue Aug 26, 2014 9:24 am

You can get the tiff image description using PIL (which you should already have installed).
I'm using the Pillow version of PIL, looking at https://github.com/python-pillow/Pillow ... ePlugin.py for reference.

But this may also work with other versions of PIL.

The code above tells me that 270 is the tag ID for image description, so I can get the OME.XML from an ome.tiff then parse the XML like this...

Code: Select all
>>> from PIL import Image
>>> im = Image.open('7990-Lamin-B_envelope.ome.tiff')
>>> imageDesc = im.tag[270]

# there are lots of ways of parsing XML in Pyhton, for example....
>>> import xml.etree.ElementTree as ET
>>> root = ET.fromstring(imageDesc)
>>> root.tag
'{http://www.openmicroscopy.org/Schemas/OME/2008-09}OME'
>>> for child in root:
...     print child.tag, child.attrib


Hope that helps,

Will.
User avatar
wmoore
Team Member
 
Posts: 674
Joined: Mon May 18, 2009 12:46 pm

Re: How to read OME-tiff file and extract metadata in python

Postby pz3977 » Tue Aug 26, 2014 7:36 pm

Hi Will,

Thank you so much for the reply. For few days I have to focus on some thing else. After that I will try your solution and let you know in case of any question.

Regards,
pz3977
pz3977
 
Posts: 31
Joined: Thu Jul 10, 2014 11:39 am

Re: How to read OME-tiff file and extract metadata in python

Postby pz3977 » Thu Sep 11, 2014 4:35 pm

Hi Will,

Today I got a chance to try the code. I have already PIL installed. I tried the code:

Code: Select all
from PIL import Image
im = Image.open('ICORR-F-20130315-150830.tiff')
imageDesc = im.tag[270]


but it gives me the following error:

Code: Select all
Traceback (most recent call last):
  File "readtiff.py", line 5, in <module>
    imageDesc = im.tag[270]
  File "/usr/lib/python2.7/dist-packages/PIL/TiffImagePlugin.py", line 248, in __getitem__
    type, data = self.tagdata[tag] # unpack on the fly
KeyError: 270


I also tried it with :

Code: Select all
for k in im.tag:
   print k


the error was the same, just with
Code: Select all
KeyError=0


Could you please help me to solve it.

Regards,
pz3977
pz3977
 
Posts: 31
Joined: Thu Jul 10, 2014 11:39 am

Re: How to read OME-tiff file and extract metadata in python

Postby wmoore » Fri Sep 12, 2014 11:06 am

I think that simply means that the tiff doesn't have any content in the image description.
If I open a regular tiff, I get the same results as you.

Seems only some of the keys listed at https://github.com/python-pillow/Pillow ... ePlugin.py are supported, and you can get them via

Code: Select all
>>> im.tag.keys()

[256, 257, 258, 259, 262, 273, 278, 284, 305, 274, 277, 279, 280, 281]


See that 270 for image description is not there.
You can get a list of what's available by:

Code: Select all
>>> for t in im.tag.keys():
...     print t, im.tag[t]
...
256 (128,)
257 (128,)
258 (16,)
259 (1,)
262 (1,)
305 softWoRx 2.50, Applied Precision, Inc.
273 (8,)
278 (128,)
284 (1,)
274 (1,)
277 (1,)
279 (32768,)
280 (1,)
281 (7864,)


That's all I see at the moment,

Do you have an idea that there is some other metadata in the tiff that you're not seeing?

Hope that helps,

Will.
User avatar
wmoore
Team Member
 
Posts: 674
Joined: Mon May 18, 2009 12:46 pm

Re: How to read OME-tiff file and extract metadata in python

Postby pz3977 » Fri Sep 19, 2014 7:28 am

Thanks will, It works fine!
pz3977
 
Posts: 31
Joined: Thu Jul 10, 2014 11:39 am


Return to Developer Discussion

Who is online

Users browsing this forum: No registered users and 1 guest