2

I'd like to update the metadata in a series of jpeg2000 images. And I'd like to do it using python. I've looked at glymur and have been able to extract the xml etree:

import glymur
from lxml import etree

jp2 = glymur.Jp2k(file)
metaroot = jp2.box[3].xml  # 4th element in box contains the metadata I want
fitshdr = metaroot[0]  # the metadata originated as a fits header

Then I can get tags and tag values:

for kw in fitshdr:
    tag = kw.tag
    val = fitshdr.findtext(tag)
    # do something with tags and values

My question is: Is there an easier way? This seems unnecessarily complicated.

  • 1
    It was pointed out to me that the sunpy package has nice facilities for this -- see sunpy.io.jp2.get_header or just read in the file via sunpy.io.jp2.read(filepath), which returns a list of (data,header) tuples. – Jonathan Slavin May 15 '15 at 17:26

1 Answers1

0

As @Jonathan mentioned, you can use sunpy module, which is popular among solar physicists and astronomers:

from sunpy.io import jp2
path_to_jp2 = 'example.jp2'
header = jp2.read(path_to_jp2)  # type:<class 'list'>

This function is documented under sunpy.io. You can also take a look at its source code.

Azim
  • 1,596
  • 18
  • 34