58
glymur Documentation Release 0.7.3 John Evans November 19, 2014

glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur DocumentationRelease 0.7.3

John Evans

November 19, 2014

Page 2: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-
Page 3: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

Contents

1 Glymur: a Python interface for JPEG 2000 31.1 Glymur Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

2 Advanced Installation Instructions 52.1 Glymur Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52.2 Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

3 How do I...? 73.1 ... read images? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73.2 ... write images? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73.3 ... display metadata? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73.4 ... add XML metadata? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113.5 ... add metadata in a more general fashion? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113.6 ... create an image with an alpha layer? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133.7 ... work with XMP UUIDs? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

4 “What’s new” documents 194.1 Changes in glymur 0.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194.2 Changes in glymur 0.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194.3 Changes in glymur 0.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

5 Known Issues 23

6 Roadmap 25

7 API 277.1 Jp2k . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277.2 Individual Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

8 Indices and tables 49

Bibliography 51

i

Page 4: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

ii

Page 5: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

Contents:

Contents 1

Page 6: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

2 Contents

Page 7: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

CHAPTER 1

Glymur: a Python interface for JPEG 2000

Glymur is an interface to the OpenJPEG library which allows one to read and write JPEG 2000 files from Python.Glymur supports both reading and writing of JPEG 2000 images, but writing JPEG 2000 images is currently limitedto images that can fit in memory. Glymur can read images using OpenJPEG library versions as far back as 1.3, but itis strongly recommended to use version 2.1.0, which is the most recently released version of OpenJPEG at this time.

In regards to metadata, most JP2 boxes are properly interpreted. Certain optional JP2 boxes can also be written,including XML boxes and XMP UUIDs. There is incomplete support for reading JPX metadata.

Glymur works on Python versions 2.7, 3.3 and 3.4. If you have Python 2.6, you should use the 0.5 series of Glymur.

For more information about OpenJPEG, please consult http://www.openjpeg.org.

1.1 Glymur Installation

You can retrieve the source for Glymur from either of

• https://pypi.python.org/pypi/Glymur/ (stable releases)

• http://github.com/quintusdias/glymur (bleeding edge, use the devel branch)

but you should also be able to install Glymur via pip

$ pip install glymur

In addition to the package, this also gives you a command line script jp2dump that can be used from the commandline line to print JPEG 2000 metadata.

3

Page 8: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

4 Chapter 1. Glymur: a Python interface for JPEG 2000

Page 9: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

CHAPTER 2

Advanced Installation Instructions

2.1 Glymur Configuration

The default glymur installation process expects OpenJPEG to be properly installed on your system as a shared library.If you have OpenJPEG installed through your system’s package manager on linux or if you use MacPorts on themac, you are probably already set to go. But if you have OpenJPEG installed into a non-standard place or if you usewindows, then read on.

Glymur uses ctypes to access the openjp2/openjpeg libraries, and because ctypes accesses libraries in a platform-dependent manner, it is recommended that if you compile and install OpenJPEG into a non-standard location, youshould then create a configuration file to help Glymur properly find the openjpeg or openjp2 libraries (linux users ormacports users don’t need to bother with this if you are using OpenJPEG as provided by your package manager). Theconfiguration format is the same as used by Python’s configparser module, i.e.

[library]openjp2: /somewhere/lib/libopenjp2.so

This assumes, of course, that you’ve installed OpenJPEG into /opt/openjpeg on a linux system. The location of theconfiguration file can vary as well. If you use either linux or mac, the path to the configuration file would normally be

$HOME/.config/glymur/glymurrc

but if you have the XDG_CONFIG_HOME environment variable defined, the path will be

$XDG_CONFIG_HOME/glymur/glymurrc

On windows, the path to the configuration file can be determined by starting up Python and typing

import osos.path.join(os.path.expanduser(’~’, ’glymur’, ’glymurrc’)

You may also include a line for the version 1.x openjpeg library if you have it installed in a non-standard place, i.e.

[library]openjpeg: /somewhere/lib/libopenjpeg.so

If your platform is windows, it is strongly recommended that you compile the 2.1.0 version of the openjpeg libraryfrom source.

5

Page 10: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

2.2 Testing

It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-ning OpenJPEG’s test suite. Check their instructions on how to do that. You can then set the OPJ_DATA_ROOTenvironment variable for the purpose of pointing Glymur to OpenJPEG’s test suite.

$ svn co http://openjpeg.googlecode.com/svn/data$ export OPJ_DATA_ROOT=‘pwd‘/data

In order to run the tests, you can either run them from within python as follows ...

>>> import glymur>>> glymur.runtests()

or from the command line.

$ cd /to/where/you/unpacked/glymur$ python -m unittest discover

6 Chapter 2. Advanced Installation Instructions

Page 11: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

CHAPTER 3

How do I...?

3.1 ... read images?

Jp2k implements slicing via the __getitem__() method, meaning that multiple resolution imagery in a JPEG2000 file can easily be accessed via array-style slicing. For example here’s how to retrieve a full resolution and firstlower-resolution image

>>> import glymur>>> jp2file = glymur.data.nemo() # just a path to a JPEG2000 file>>> jp2 = glymur.Jp2k(jp2file)>>> fullres = jp2[:]>>> print(fullres.shape)(1456, 2592, 3)>>> thumbnail = jp2[::2, ::2]>>> print(thumbnail.shape)(728, 1296, 3)

The read() method exposes many more options for other JPEG 2000 features such as quality layers.

3.2 ... write images?

So long as the image data can fit entirely into memory, array-style slicing may also be used to write JPEG 2000 files.

>>> import glymur, numpy as np>>> jp2 = glymur.Jp2k(’zeros.jp2’, mode=’wb’)>>> jp2[:] = np.zeros((640, 480), dtype=np.uint8)

The write()method exposes many more options for other JPEG 2000 features. You should have OpenJPEG version1.5 or more recent before writing JPEG 2000 images.

3.3 ... display metadata?

There are two ways. From the command line, the console script jp2dump is available.

$ jp2dump /path/to/glymur/installation/data/nemo.jp2

From within Python, the same result is obtained simply by printing the Jp2k object, i.e.

7

Page 12: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

>>> import glymur>>> jp2file = glymur.data.nemo() # just a path to a JP2 file>>> jp2 = glymur.Jp2k(jp2file)>>> print(jp2)File: nemo.jp2JPEG 2000 Signature Box (jP ) @ (0, 12)

Signature: 0d0a870aFile Type Box (ftyp) @ (12, 20)

Brand: jp2Compatibility: [’jp2 ’]

JP2 Header Box (jp2h) @ (32, 45)Image Header Box (ihdr) @ (40, 22)

Size: [1456 2592 3]Bitdepth: 8Signed: FalseCompression: waveletColorspace Unknown: False

Colour Specification Box (colr) @ (62, 15)Method: enumerated colorspacePrecedence: 0Colorspace: sRGB

UUID Box (uuid) @ (77, 3146)UUID: be7acfcb-97a9-42e8-9c71-999491e3afac (XMP)UUID Data:<ns0:xmpmeta xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:ns0="adobe:ns:meta/" xmlns:ns2="http://ns.adobe.com/xap/1.0/" xmlns:ns3="http://ns.adobe.com/tiff/1.0/" xmlns:ns4="http://ns.adobe.com/exif/1.0/" xmlns:ns5="http://ns.adobe.com/photoshop/1.0/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" ns0:xmptk="Exempi + XMP Core 5.1.2">

<rdf:RDF><rdf:Description rdf:about=""><ns2:CreatorTool>Google</ns2:CreatorTool><ns2:CreateDate>2013-02-09T14:47:53</ns2:CreateDate>

</rdf:Description><rdf:Description rdf:about=""><ns3:YCbCrPositioning>1</ns3:YCbCrPositioning><ns3:XResolution>72/1</ns3:XResolution><ns3:YResolution>72/1</ns3:YResolution><ns3:ResolutionUnit>2</ns3:ResolutionUnit><ns3:Make>HTC</ns3:Make><ns3:Model>HTC Glacier</ns3:Model><ns3:ImageWidth>2592</ns3:ImageWidth><ns3:ImageLength>1456</ns3:ImageLength><ns3:BitsPerSample><rdf:Seq><rdf:li>8</rdf:li><rdf:li>8</rdf:li><rdf:li>8</rdf:li>

</rdf:Seq></ns3:BitsPerSample><ns3:PhotometricInterpretation>2</ns3:PhotometricInterpretation><ns3:SamplesPerPixel>3</ns3:SamplesPerPixel><ns3:WhitePoint>

<rdf:Seq><rdf:li>1343036288/4294967295</rdf:li><rdf:li>1413044224/4294967295</rdf:li>

</rdf:Seq></ns3:WhitePoint><ns3:PrimaryChromaticities>

<rdf:Seq><rdf:li>2748779008/4294967295</rdf:li><rdf:li>1417339264/4294967295</rdf:li>

8 Chapter 3. How do I...?

Page 13: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

<rdf:li>1288490240/4294967295</rdf:li><rdf:li>2576980480/4294967295</rdf:li><rdf:li>644245120/4294967295</rdf:li><rdf:li>257698032/4294967295</rdf:li>

</rdf:Seq></ns3:PrimaryChromaticities>

</rdf:Description><rdf:Description rdf:about=""><ns4:ColorSpace>1</ns4:ColorSpace><ns4:PixelXDimension>2528</ns4:PixelXDimension><ns4:PixelYDimension>1424</ns4:PixelYDimension><ns4:FocalLength>353/100</ns4:FocalLength><ns4:GPSAltitudeRef>0</ns4:GPSAltitudeRef><ns4:GPSAltitude>0/1</ns4:GPSAltitude><ns4:GPSMapDatum>WGS-84</ns4:GPSMapDatum><ns4:DateTimeOriginal>2013-02-09T14:47:53</ns4:DateTimeOriginal><ns4:ISOSpeedRatings>

<rdf:Seq><rdf:li>76</rdf:li>

</rdf:Seq></ns4:ISOSpeedRatings><ns4:ExifVersion>0220</ns4:ExifVersion><ns4:FlashpixVersion>0100</ns4:FlashpixVersion><ns4:ComponentsConfiguration>

<rdf:Seq><rdf:li>1</rdf:li><rdf:li>2</rdf:li><rdf:li>3</rdf:li><rdf:li>0</rdf:li>

</rdf:Seq></ns4:ComponentsConfiguration><ns4:GPSLatitude>42,20.56N</ns4:GPSLatitude><ns4:GPSLongitude>71,5.29W</ns4:GPSLongitude><ns4:GPSTimeStamp>2013-02-09T19:47:53Z</ns4:GPSTimeStamp><ns4:GPSProcessingMethod>NETWORK</ns4:GPSProcessingMethod>

</rdf:Description><rdf:Description rdf:about=""><ns5:DateCreated>2013-02-09T14:47:53</ns5:DateCreated>

</rdf:Description><rdf:Description rdf:about=""><dc:Creator>

<rdf:Seq><rdf:li>Glymur</rdf:li><rdf:li>Python XMP Toolkit</rdf:li>

</rdf:Seq></dc:Creator>

</rdf:Description></rdf:RDF>

</ns0:xmpmeta>Contiguous Codestream Box (jp2c) @ (3223, 1132296)

Main header:SOC marker segment @ (3231, 0)SIZ marker segment @ (3233, 47)

Profile: 2Reference Grid Height, Width: (1456 x 2592)Vertical, Horizontal Reference Grid Offset: (0 x 0)Reference Tile Height, Width: (1456 x 2592)Vertical, Horizontal Reference Tile Offset: (0 x 0)

3.3. ... display metadata? 9

Page 14: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

Bitdepth: (8, 8, 8)Signed: (False, False, False)Vertical, Horizontal Subsampling: ((1, 1), (1, 1), (1, 1))

COD marker segment @ (3282, 12)Coding style:

Entropy coder, without partitionsSOP marker segments: FalseEPH marker segments: False

Coding style parameters:Progression order: LRCPNumber of layers: 2Multiple component transformation usage: reversibleNumber of resolutions: 2Code block height, width: (64 x 64)Wavelet transform: 5-3 reversiblePrecinct size: default, 2^15 x 2^15Code block context:

Selective arithmetic coding bypass: FalseReset context probabilities on coding pass boundaries: FalseTermination on each coding pass: FalseVertically stripe causal context: FalsePredictable termination: FalseSegmentation symbols: False

QCD marker segment @ (3296, 7)Quantization style: no quantization, 2 guard bitsStep size: [(0, 8), (0, 9), (0, 9), (0, 10)]

CME marker segment @ (3305, 37)"Created by OpenJPEG version 2.0.0"

That’s fairly overwhelming, and perhaps lost in the flood of information is the fact that the codestream metadata islimited to just what’s in the main codestream header. You can suppress the codestream and XML details by makinguse of the set_printoptions() function:

>>> glymur.set_printoptions(codestream=False, xml=False)>>> print(jp2)File: nemo.jp2JPEG 2000 Signature Box (jP ) @ (0, 12)

Signature: 0d0a870aFile Type Box (ftyp) @ (12, 20)

Brand: jp2Compatibility: [’jp2 ’]

JP2 Header Box (jp2h) @ (32, 45)Image Header Box (ihdr) @ (40, 22)

Size: [1456 2592 3]Bitdepth: 8Signed: FalseCompression: waveletColorspace Unknown: False

Colour Specification Box (colr) @ (62, 15)Method: enumerated colorspacePrecedence: 0Colorspace: sRGB

UUID Box (uuid) @ (77, 3146)UUID: be7acfcb-97a9-42e8-9c71-999491e3afac (XMP)

Contiguous Codestream Box (jp2c) @ (3223, 1132296)

It is possible to print all the gory codestream details as well, i.e.

10 Chapter 3. How do I...?

Page 15: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

>>> print(j.get_codestream()) # details not shown

3.4 ... add XML metadata?

You can append any number of XML boxes to a JP2 file (not to a raw codestream). Consider the following XML filedata.xml :

<?xml version="1.0"?><info>

<locality><city>Boston</city><snowfall>24.9 inches</snowfall>

</locality><locality>

<city>Portland</city><snowfall>31.9 inches</snowfall>

</locality><locality>

<city>New York City</city><snowfall>11.4 inches</snowfall>

</locality></info>

The append() method can add an XML box as shown below:

>>> import shutil>>> import glymur>>> shutil.copyfile(glymur.data.nemo(), ’myfile.jp2’)>>> jp2 = glymur.Jp2k(’myfile.jp2’)>>> xmlbox = glymur.jp2box.XMLBox(filename=’data.xml’)>>> jp2.append(xmlbox)>>> print(jp2)

3.5 ... add metadata in a more general fashion?

An existing raw codestream (or JP2 file) can be wrapped (re-wrapped) in a user-defined set of JP2 boxes. To get justa minimal JP2 jacket on the codestream provided by goodstuff.j2k (a file consisting of a raw codestream), you can usethe wrap() method with no box argument:

>>> import glymur>>> glymur.set_printoptions(codestream=False)>>> jp2file = glymur.data.goodstuff()>>> j2k = glymur.Jp2k(jp2file)>>> jp2 = j2k.wrap("newfile.jp2")>>> print(jp2)File: newfile.jp2JPEG 2000 Signature Box (jP ) @ (0, 12)

Signature: 0d0a870aFile Type Box (ftyp) @ (12, 20)

Brand: jp2Compatibility: [’jp2 ’]

JP2 Header Box (jp2h) @ (32, 45)Image Header Box (ihdr) @ (40, 22)

Size: [800 480 3]

3.4. ... add XML metadata? 11

Page 16: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

Bitdepth: 8Signed: FalseCompression: waveletColorspace Unknown: False

Colour Specification Box (colr) @ (62, 15)Method: enumerated colorspacePrecedence: 0Colorspace: sRGB

Contiguous Codestream Box (jp2c) @ (77, 115228)

The raw codestream was wrapped in a JP2 jacket with four boxes in the outer layer (the signature, file type, JP2 header,and contiguous codestream), with two additional boxes (image header and color specification) contained in the JP2header superbox.

XML boxes are not in the minimal set of box requirements for the JP2 format, so in order to add an XML box intothe mix before the codestream box, we’ll need to re-specify all of the boxes. If you already have a JP2 jacket in place,you can just reuse that, though. Take the following example content in an XML file favorites.xml :

<?xml version="1.0"?><favorite_things>

<category>Light Ale</category></favorite_things>

In order to add the XML after the JP2 header box, but before the codestream box, the following will work.

>>> boxes = jp2.box # The box attribute is the list of JP2 boxes>>> xmlbox = glymur.jp2box.XMLBox(filename=’favorites.xml’)>>> boxes.insert(3, xmlbox)>>> jp2_xml = jp2.wrap("newfile_with_xml.jp2", boxes=boxes)>>> print(jp2_xml)File: newfile_with_xml.jp2JPEG 2000 Signature Box (jP ) @ (0, 12)

Signature: 0d0a870aFile Type Box (ftyp) @ (12, 20)

Brand: jp2Compatibility: [’jp2 ’]

JP2 Header Box (jp2h) @ (32, 45)Image Header Box (ihdr) @ (40, 22)

Size: [800 480 3]Bitdepth: 8Signed: FalseCompression: waveletColorspace Unknown: False

Colour Specification Box (colr) @ (62, 15)Method: enumerated colorspacePrecedence: 0Colorspace: sRGB

XML Box (xml ) @ (77, 76)<favorite_things>

<category>Light Ale</category></favorite_things>

Contiguous Codestream Box (jp2c) @ (153, 115236)

As to the question of which method you should use, append() or wrap(), to add metadata, you should keep inmind that wrap() produces a new JP2 file, while append() modifies an existing file and is currently limited toXML and UUID boxes.

12 Chapter 3. How do I...?

Page 17: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

3.6 ... create an image with an alpha layer?

OpenJPEG can create JP2 files with more than 3 components (use version 2.1.0+ for this), but by default, any extracomponents are not described as such. In order to do so, we need to rewrap such an image in a set of boxes thatincludes a channel definition box.

This example is based on SciPy example code found at http://scipy-lectures.github.io/advanced/image_processing/#basic-manipulations . Instead of a circular mask we’ll make it an ellipse since the source image isn’t square.

>>> import numpy as np>>> import glymur>>> from glymur import Jp2k>>> rgb = Jp2k(glymur.data.goodstuff()).read()>>> lx, ly = rgb.shape[0:2]>>> X, Y = np.ogrid[0:lx, 0:ly]>>> mask = ly**2*(X - lx / 2) ** 2 + lx**2*(Y - ly / 2) ** 2 > (lx * ly / 2)**2>>> alpha = 255 * np.ones((lx, ly, 1), dtype=np.uint8)>>> alpha[mask] = 0>>> rgba = np.concatenate((rgb, alpha), axis=2)>>> jp2 = Jp2k(’tmp.jp2’, ’wb’)>>> jp2[:] = rgba

Next we need to specify what types of channels we have. The first three channels are color channels, but we identifythe fourth as an alpha channel:

>>> from glymur.core import COLOR, OPACITY>>> ctype = [COLOR, COLOR, COLOR, OPACITY]

And finally we have to specify just exactly how each channel is to be interpreted. The color channels are straight-forward, they correspond to R-G-B, but the alpha (or opacity) channel in this case is to be applied against the entireimage (it is possible to apply an alpha channel to a single color channel, but we aren’t doing that).

>>> from glymur.core import RED, GREEN, BLUE, WHOLE_IMAGE>>> asoc = [RED, GREEN, BLUE, WHOLE_IMAGE]>>> cdef = glymur.jp2box.ChannelDefinitionBox(ctype, asoc)>>> print(cdef)Channel Definition Box (cdef) @ (0, 0)

Channel 0 (color) ==> (1)Channel 1 (color) ==> (2)Channel 2 (color) ==> (3)Channel 3 (opacity) ==> (whole image)

It’s easiest to take the existing jp2 jacket and just add the channel definition box in the appropriate spot. The channeldefinition box must go into the jp2 header box, and then we can rewrap the image.

>>> boxes = jp2.box # The box attribute is the list of JP2 boxes>>> boxes[2].box.append(cdef)>>> jp2_rgba = jp2.wrap("goodstuff_rgba.jp2", boxes=boxes)

Here’s how the Preview application on the mac shows the RGBA image.

3.6. ... create an image with an alpha layer? 13

Page 18: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

14 Chapter 3. How do I...?

Page 19: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

3.7 ... work with XMP UUIDs?

Wikipedia states that “The Extensible Metadata Platform (XMP) is an ISO standard, originally created by Adobe Sys-tems Inc., for the creation, processing and interchange of standardized and custom metadata for all kinds of resources.”

The example JP2 file shipped with glymur has an XMP UUID.

>>> import glymur>>> j = glymur.Jp2k(glymur.data.nemo())>>> print(j.box[3]) # formatting added to the XML below<ns0:xmpmeta xmlns:dc="http://purl.org/dc/elements/1.1/"

xmlns:ns0="adobe:ns:meta/"xmlns:ns2="http://ns.adobe.com/xap/1.0/"xmlns:ns3="http://ns.adobe.com/tiff/1.0/"xmlns:ns4="http://ns.adobe.com/exif/1.0/"xmlns:ns5="http://ns.adobe.com/photoshop/1.0/"xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"ns0:xmptk="Exempi + XMP Core 5.1.2">

<rdf:RDF><rdf:Description rdf:about=""><ns2:CreatorTool>Google</ns2:CreatorTool><ns2:CreateDate>2013-02-09T14:47:53</ns2:CreateDate>

</rdf:Description>

.

.

.</ns0:xmpmeta>

Since the UUID data in this case is returned as an lxml ElementTree instance, one can use lxml to access the data. Forexample, to extract the CreatorTool attribute value, one could do the following

>>> xmp = j.box[3].data>>> rdf = ’{http://www.w3.org/1999/02/22-rdf-syntax-ns#}’>>> ns2 = ’{http://ns.adobe.com/xap/1.0/}’>>> name = ’{0}RDF/{0}Description/{1}CreatorTool’.format(rdf, ns2)>>> elt = xmp.find(name)>>> elt<Element ’{http://ns.adobe.com/xap/1.0/#}CreatorTool’ at 0xb50684a4>>>> elt.text’Google’

But that would be painful. A better solution is to install the Python XMP Toolkit (make sure it is version 2.0):

>>> from libxmp import XMPMeta>>> from libxmp.consts import XMP_NS_XMP as NS_XAP>>> meta = XMPMeta()>>> meta.parse_from_str(j.box[3].raw_data.decode(’utf-8’))>>> meta.get_property(NS_XAP, ’CreatorTool’)’Google’

Where the Python XMP Toolkit can really shine, though, is when you are converting an image from another for-mat such as TIFF or JPEG into JPEG 2000. For example, if you were to be converting the TIFF image found athttp://photojournal.jpl.nasa.gov/tiff/PIA17145.tif info JPEG 2000:

>>> import skimage.io>>> image = skimage.io.imread(’PIA17145.tif’)>>> from glymur import Jp2k

3.7. ... work with XMP UUIDs? 15

Page 20: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

>>> jp2 = Jp2k(’PIA17145.jp2’, ’wb’)>>> jp2[:] = image

Next you can extract the XMP metadata.

>>> from libxmp import XMPFiles>>> xf = XMPFiles()>>> xf.open_file(’PIA17145.tif’)>>> xmp = xf.get_xmp()>>> print(xmp)<?xpacket begin="" id="W5M0MpCehiHzreSzNTczkc9d"?><x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Exempi + XMP Core 5.1.2"><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"><rdf:Description rdf:about=""xmlns:tiff="http://ns.adobe.com/tiff/1.0/">

<tiff:ImageWidth>1016</tiff:ImageWidth><tiff:ImageLength>1016</tiff:ImageLength><tiff:BitsPerSample><rdf:Seq><rdf:li>8</rdf:li>

</rdf:Seq></tiff:BitsPerSample><tiff:Compression>1</tiff:Compression><tiff:PhotometricInterpretation>1</tiff:PhotometricInterpretation><tiff:SamplesPerPixel>1</tiff:SamplesPerPixel><tiff:PlanarConfiguration>1</tiff:PlanarConfiguration><tiff:ResolutionUnit>2</tiff:ResolutionUnit></rdf:Description><rdf:Description rdf:about=""xmlns:dc="http://purl.org/dc/elements/1.1/">

<dc:description><rdf:Alt><rdf:li xml:lang="x-default">converted PNM file</rdf:li>

</rdf:Alt></dc:description></rdf:Description>

</rdf:RDF></x:xmpmeta><?xpacket end="w"?>

If you are familiar with TIFF, you can verify that there’s no XMP tag in the TIFF file, but the Python XMP Toolkittakes advantage of the TIFF header structure to populate an XMP packet for you. If you were working with a JPEGfile with Exif metadata, that information would be included in the XMP packet as well. Now you can append the XMPpacket in a UUIDBox. In order to do this, though, you have to know the UUID that signifies XMP data.:

>>> import uuid>>> xmp_uuid = uuid.UUID(’be7acfcb-97a9-42e8-9c71-999491e3afac’)>>> box = glymur.jp2box.UUIDBox(xmp_uuid, str(xmp).encode())>>> jp2.append(box)>>> print(jp2.box[-1])UUID Box (uuid) @ (592316, 1053)

UUID: be7acfcb-97a9-42e8-9c71-999491e3afac (XMP)UUID Data:<ns0:xmpmeta xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:ns0="adobe:ns:meta/" xmlns:ns2="http://ns.adobe.com/tiff/1.0/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" ns0:xmptk="Exempi + XMP Core 5.1.2">

<rdf:RDF><rdf:Description rdf:about=""><ns2:ImageWidth>1016</ns2:ImageWidth><ns2:ImageLength>1016</ns2:ImageLength><ns2:BitsPerSample>

16 Chapter 3. How do I...?

Page 21: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

<rdf:Seq><rdf:li>8</rdf:li>

</rdf:Seq></ns2:BitsPerSample><ns2:Compression>1</ns2:Compression><ns2:PhotometricInterpretation>1</ns2:PhotometricInterpretation><ns2:SamplesPerPixel>1</ns2:SamplesPerPixel><ns2:PlanarConfiguration>1</ns2:PlanarConfiguration><ns2:ResolutionUnit>2</ns2:ResolutionUnit>

</rdf:Description><rdf:Description rdf:about=""><dc:description>

<rdf:Alt><rdf:li xml:lang="x-default">converted PNM file</rdf:li>

</rdf:Alt></dc:description>

</rdf:Description></rdf:RDF>

</ns0:xmpmeta>

You can also build up XMP metadata from scratch. For instance, if we try to wrap goodstuff.j2k again:

>>> import glymur>>> j2kfile = glymur.data.goodstuff()>>> j2k = glymur.Jp2k(j2kfile)>>> jp2 = j2k.wrap("goodstuff.jp2")

Now build up the metadata piece-by-piece. It would help to have the XMP standard close at hand:

>>> from libxmp import XMPMeta>>> from libxmp.consts import XMP_NS_TIFF as NS_TIFF>>> from libxmp.consts import XMP_NS_DC as NS_DC>>> xmp = XMPMeta()>>> ihdr = jp2.box[2].box[0]>>> xmp.set_property(NS_TIFF, "ImageWidth", str(ihdr.width))>>> xmp.set_property(NS_TIFF, "ImageHeight", str(ihdr.height))>>> xmp.set_property(NS_TIFF, "BitsPerSample", ’3’)>>> xmp.set_property(NS_DC, "Title", u’Stürm und Drang’)>>> xmp.set_property(NS_DC, "Creator", ’Glymur’)

We can then append the XMP in a UUID box just as before:

>>> import uuid>>> xmp_uuid = uuid.UUID(’be7acfcb-97a9-42e8-9c71-999491e3afac’)>>> box = glymur.jp2box.UUIDBox(xmp_uuid, str(xmp).encode())>>> jp2.append(box)>>> glymur.set_printoptions(codestream=False)>>> print(jp2)File: goodstuff.jp2JPEG 2000 Signature Box (jP ) @ (0, 12)

Signature: 0d0a870aFile Type Box (ftyp) @ (12, 20)

Brand: jp2Compatibility: [’jp2 ’]

JP2 Header Box (jp2h) @ (32, 45)Image Header Box (ihdr) @ (40, 22)

Size: [800 480 3]Bitdepth: 8Signed: False

3.7. ... work with XMP UUIDs? 17

Page 22: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

Compression: waveletColorspace Unknown: False

Colour Specification Box (colr) @ (62, 15)Method: enumerated colorspacePrecedence: 0Colorspace: sRGB

Contiguous Codestream Box (jp2c) @ (77, 115228)UUID Box (uuid) @ (115305, 671)

UUID: be7acfcb-97a9-42e8-9c71-999491e3afac (XMP)UUID Data:<ns0:xmpmeta xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:ns0="adobe:ns:meta/" xmlns:ns2="http://ns.adobe.com/tiff/1.0/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" ns0:xmptk="Exempi + XMP Core 5.1.2">

<rdf:RDF><rdf:Description rdf:about=""><ns2:ImageWidth>480</ns2:ImageWidth><ns2:ImageHeight>800</ns2:ImageHeight><ns2:BitsPerSample>3</ns2:BitsPerSample>

</rdf:Description><rdf:Description rdf:about=""><dc:Title>Stürm und Drang</dc:Title><dc:Creator>Glymur</dc:Creator>

</rdf:Description></rdf:RDF>

</ns0:xmpmeta>

18 Chapter 3. How do I...?

Page 23: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

CHAPTER 4

“What’s new” documents

These document the changes between minor (or major) versions of glymur.

4.1 Changes in glymur 0.7

4.1.1 Changes in 0.7.3

• added read support back for metadata only when the OpenJPEG library is not installed

4.1.2 Changes in 0.7.2

• added ellipsis support in array-style slicing

4.1.3 Changes in 0.7.1

• fixed release notes regarding Python 3.4

4.1.4 Changes in 0.7.0

• implemented array-style slicing

• added back windows support

• box_id and longname are class attributes now instead of instance attributes

4.2 Changes in glymur 0.6

4.2.1 Changes in 0.6.0

• Added Cinema2K, Cinema4K write support.

• Added irreversible 9-7 transform write support.

• Added set_printoptions, get_printoptions functions.

• Added write support for JP2 UUID, data entry URL, palette, and component mapping boxes.

19

Page 24: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

• Added read/write support for JPX free, number list, and data reference boxes

• Added read support for JPX fragment list and fragment table boxes

• Incompatible change to channel definition box constructor, channel_type and association are no longer keywordarguments

• Incompatible change to palette box constructor, it now takes a 2D numpy array instead of a list of 1D arrays

• Dropped support for 1.3 and 1.4.

• Dropped support for Python 2.6.

• Dropped windows support. It might still work, I don’t have access to a windows box with which to test it.

• Added lxml as a package dependency, replacing ElementTree.

4.3 Changes in glymur 0.5

4.3.1 Changes in 0.5.12

• Minor documentation fixes for grammar and style.

• The functions removed in 0.5.11 due to API changes in OpenJPEG 2.1.0 were restored for backwards compati-bility. They are deprecated, though, and will be removed in 0.6.0.

– glymur.lib.openjp2.stream_create_default_file_stream_v3

– glymur.lib.openjp2.opj.stream_destroy_v3

4.3.2 Changes in 0.5.11

• Added support for Python 3.4.

• OpenJPEG 1.5.2 and 2.0.1 are officially supported.

• OpenJPEG 2.1.0 is officially supported, but the ABI changes introduced by OpenJPEG 2.1.0 required corre-sponding changes to glymur’s ctypes interface. The functions

– glymur.lib.openjp2.stream_create_default_file_stream_v3

– glymur.lib.openjp2.opj.stream_destroy_v3

functions were renamed to

– glymur.lib.openjp2.stream_create_default_file_stream

– glymur.lib.openjp2.opj.stream_destroy

in order to follow OpenJPEG’s upstream changes. Unless you were using the svn version of OpenJPEG, youshould not be affected by this.

4.3.3 Changes in 0.5.10

• Fixed bad warning issued when an unsupported reader requirement box mask length was encountered.

20 Chapter 4. “What’s new” documents

Page 25: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

4.3.4 Changes in 0.5.9

• Fixed bad library load on linux as a result of botched 0.5.8 release. This release was primarily aimed at support-ing SunPy.

4.3. Changes in glymur 0.5 21

Page 26: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

22 Chapter 4. “What’s new” documents

Page 27: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

CHAPTER 5

Known Issues

• Creating a Jp2 file with the irreversible option does not work on windows.

• Eval-ing a repr() string does not work on windows.

23

Page 28: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

24 Chapter 5. Known Issues

Page 29: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

CHAPTER 6

Roadmap

Here’s an incomplete list of what I’d like to focus on in the future.

• continue to monitor upstream changes in the openjp2 library

• investigate JPIP (likely to be a big project)

25

Page 30: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

26 Chapter 6. Roadmap

Page 31: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

CHAPTER 7

API

7.1 Jp2k

class glymur.Jp2k(filename, mode=’rb’)JPEG 2000 file.

Attributes

file-name

str The path to the JPEG 2000 file.

mode str The mode used to open the file.box se-

quenceList of top-level boxes in the file. Each box may in turn contain its own list of boxes. Willbe empty if the file consists only of a raw codestream.

Methods

appendget_codestreamparseparse_superboxreadread_bandswrapwrite

get_codestream(header_only=True)Returns a codestream object.

Parameters header_only : bool, optional

If True, only marker segments in the main header are parsed. Supplying False mayimpose a large performance penalty.

Returns Object describing the codestream syntax. :

27

Page 32: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

Examples

>>> import glymur>>> jfile = glymur.data.nemo()>>> jp2 = glymur.Jp2k(jfile)>>> codestream = jp2.get_codestream()>>> print(codestream.segment[1])SIZ marker segment @ (3233, 47)

Profile: no profileReference Grid Height, Width: (1456 x 2592)Vertical, Horizontal Reference Grid Offset: (0 x 0)Reference Tile Height, Width: (1456 x 2592)Vertical, Horizontal Reference Tile Offset: (0 x 0)Bitdepth: (8, 8, 8)Signed: (False, False, False)Vertical, Horizontal Subsampling: ((1, 1), (1, 1), (1, 1))

read(**kwargs)Read a JPEG 2000 image.

Parameters rlevel : int, optional

Factor by which to rlevel output resolution. Use -1 to get the lowest resolution thumb-nail. This is the only keyword option available to use when the OpenJPEG version is1.5 or earlier.

layer : int, optional

Number of quality layer to decode.

area : tuple, optional

Specifies decoding image area, (first_row, first_col, last_row, last_col)

tile : int, optional

Number of tile to decode.

ignore_pclr_cmap_cdef : bool

Whether or not to ignore the pclr, cmap, or cdef boxes during any color transformation.Defaults to False.

verbose : bool, optional

Print informational messages produced by the OpenJPEG library.

Returns img_array : ndarray

The image data.

Raises glymur.LibraryNotFoundError :

If glymur is unable to load either the openjpeg or openjp2 libraries.

IOError :

If the image has differing subsample factors.

28 Chapter 7. API

Page 33: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

Examples

>>> import glymur>>> jfile = glymur.data.nemo()>>> jp = glymur.Jp2k(jfile)>>> image = jp.read()>>> image.shape(1456, 2592, 3)

Read the lowest resolution thumbnail.

>>> thumbnail = jp.read(rlevel=-1)>>> thumbnail.shape(728, 1296, 3)

read_bands(rlevel=0, layer=0, area=None, tile=None, verbose=False, ig-nore_pclr_cmap_cdef=False)

Read a JPEG 2000 image.

The only time you should use this method is when the image has different subsampling factors acrosscomponents. Otherwise you should use the read method.

Parameters layer : int, optional

Number of quality layer to decode.

rlevel : int, optional

Factor by which to rlevel output resolution.

area : tuple, optional

Specifies decoding image area, (first_row, first_col, last_row, last_col)

tile : int, optional

Number of tile to decode.

ignore_pclr_cmap_cdef : bool

Whether or not to ignore the pclr, cmap, or cdef boxes during any color transformation.Defaults to False.

verbose : bool, optional

Print informational messages produced by the OpenJPEG library.

Returns lst : list

The individual image components.

Raises glymur.LibraryNotFoundError :

If glymur is unable to load the openjp2 library.

See also:

read read JPEG 2000 image

Examples

7.1. Jp2k 29

Page 34: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

>>> import glymur>>> jfile = glymur.data.nemo()>>> jp = glymur.Jp2k(jfile)>>> components_lst = jp.read_bands(rlevel=1)

wrap(filename, boxes=None)Create a new JP2/JPX file wrapped in a new set of JP2 boxes.

This method is primarily aimed at wrapping a raw codestream in a set of of JP2 boxes (turning it into aJP2 file instead of just a raw codestream), or rewrapping a codestream in a JP2 file in a new “jacket” ofJP2 boxes.

Parameters filename : str

JP2 file to be created from a raw codestream.

boxes : list

JP2 box definitions to define the JP2 file format. If not provided, a default “”jacket” isassumed, consisting of JP2 signature, file type, JP2 header, and contiguous codestreamboxes. A JPX file rewrapped without the boxes argument results in a JP2 file encom-passing the first codestream.

Returns jp2 : Jp2k object

Newly wrapped Jp2k object.

Examples

>>> import glymur, tempfile>>> jfile = glymur.data.goodstuff()>>> j2k = glymur.Jp2k(jfile)>>> tfile = tempfile.NamedTemporaryFile(suffix=’jp2’)>>> jp2 = j2k.wrap(tfile.name)

write(img_array, verbose=False, **kwargs)Write image data to a JP2/JPX/J2k file. Intended usage of the various parameters follows that of Open-JPEG’s opj_compress utility.

This method can only be used to create JPEG 2000 images that can fit in memory.

Parameters img_array : ndarray

Image data to be written to file.

cbsize : tuple, optional

Code block size (DY, DX).

cinema2k : int, optional

frames per second, either 24 or 48

cinema4k : bool, optional

Set to True to specify Cinema4K mode, defaults to false.

colorspace : str, optional

Either ‘rgb’ or ‘gray’.

cratios : iterable

30 Chapter 7. API

Page 35: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

Compression ratios for successive layers.

eph : bool, optional

If true, write SOP marker after each header packet.

grid_offset : tuple, optional

Offset (DY, DX) of the origin of the image in the reference grid.

irreversible : bool, optional

If true, use the irreversible DWT 9-7 transform.

mct : bool, optional

Specifies usage of the multi component transform. If not specified, defaults to True ifthe colorspace is RGB.

modesw : int, optional

Mode switch. 1 = BYPASS(LAZY) 2 = RESET 4 = RESTART(TERMALL) 8 = VSC16 = ERTERM(SEGTERM) 32 = SEGMARK(SEGSYM)

numres : int, optional

Number of resolutions.

prog : str, optional

Progression order, one of “LRCP” “RLCP”, “RPCL”, “PCRL”, “CPRL”.

psnr : iterable, optional

Different PSNR for successive layers.

psizes : list, optional

List of precinct sizes. Each precinct size tuple is defined in (height x width).

sop : bool, optional

If true, write SOP marker before each packet.

subsam : tuple, optional

Subsampling factors (dy, dx).

tilesize : tuple, optional

Numeric tuple specifying tile size in terms of (numrows, numcols), not (X, Y).

verbose : bool, optional

Print informational messages produced by the OpenJPEG library.

Raises glymur.LibraryNotFoundError :

If glymur is unable to load the openjp2 library.

Examples

>>> import glymur>>> jfile = glymur.data.nemo()>>> jp2 = glymur.Jp2k(jfile)>>> data = jp2.read(rlevel=1)>>> from tempfile import NamedTemporaryFile

7.1. Jp2k 31

Page 36: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

>>> tfile = NamedTemporaryFile(suffix=’.jp2’, delete=False)>>> j = Jp2k(tfile.name, mode=’wb’)>>> j.write(data.astype(np.uint8))

7.2 Individual Boxes

7.2.1 Jp2kbox

class glymur.jp2box.Jp2kBox(offset=0, length=0)Superclass for JPEG 2000 boxes.

Attributes

box_id str 4-character identifier for the box.length int length of the box in bytes.offset int offset of the box from the start of the file.box list List of JPEG 2000 boxes.

Methods

parse_superboxwrite

parse_superbox(fptr)Parse a superbox (box consisting of nothing but other boxes.

Parameters fptr : file

Open file object.

Returns List of top-level boxes in the JPEG 2000 file. :

write(_)Must be implemented in a subclass.

7.2.2 AssociationBox

class glymur.jp2box.AssociationBox(box=None, length=0, offset=-1)Container for Association box information.

Attributes

box_idlongname

32 Chapter 7. API

Page 37: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

length int length of the box in bytes.offset int offset of the box from the start of the file.box list List of boxes contained in this superbox.

Methods

parseparse_superboxwrite

classmethod parse(fptr, offset, length)Parse association box.

Parameters fptr : file

Open file object.

offset : int

Start position of box in bytes.

length : int

Length of the box in bytes.

Returns AssociationBox instance :

write(fptr)Write an association box to file.

7.2.3 ColourSpecificationBox

class glymur.jp2box.ColourSpecificationBox(method=1, precedence=0, approximation=0,colorspace=None, icc_profile=None, length=0,offset=-1)

Container for JPEG 2000 color specification box information.

Attributes

box_idlongname

length int Length of the box in bytes.offset int Offset of the box from the start of the file.method int Method for defining the colorspace.prece-dence

int How this box ranks in priority compared to other color specification boxes.

approxi-mation

int Measure of colorspace accuracy.

col-orspace

int orNone

Enumerated colorspace, corresponds to one of ‘sRGB’, ‘greyscale’, or ‘YCC’. Ifnot None, then icc_profile must be None.

icc_profile dict ICC profile header according to ICC profile specification. If colorspace is not None,then icc_profile must be empty.

7.2. Individual Boxes 33

Page 38: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

Methods

parseparse_superboxwrite

classmethod parse(fptr, offset, length)Parse JPEG 2000 color specification box.

Parameters fptr : file

Open file object.

offset : int

Start position of box in bytes.

length : int

Length of the box in bytes.

Returns ColourSpecificationBox instance :

write(fptr)Write an Colour Specification box to file.

7.2.4 ChannelDefinitionBox

class glymur.jp2box.ChannelDefinitionBox(channel_type, association, index=None, **kwargs)Container for component definition box information.

Attributes

box_idlongname

length numericscalar

length of the box in bytes.

offset int offset of the box from the start of the file.index list number of the channel. Defaults to monotonically increasing sequence, i.e.

[0, 1, 2, ...]chan-nel_type

list type of the channel

association list index of the associated color

Methods

parseparse_superboxwrite

34 Chapter 7. API

Page 39: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

classmethod parse(fptr, offset, length)Parse component definition box.

Parameters fptr : file

Open file object.

offset : int

Start position of box in bytes.

length : int

Length of the box in bytes.

Returns ComponentDefinitionBox instance :

write(fptr)Write a channel definition box to file.

7.2.5 ComponentMappingBox

class glymur.jp2box.ComponentMappingBox(component_index, mapping_type, palette_index,length=0, offset=-1)

Container for component mapping information.

Attributes

box_idlongname

length numeric scalar Length of the box in bytes.offset int Offset of the box from the start of the file.component_index tuple Index of component in codestream that is mapped to this channel.mapping_type tuple mapping type, either direct use (0) or palette (1)palette_index tuple Index component from palette

Methods

parseparse_superboxwrite

classmethod parse(fptr, offset, length)Parse component mapping box.

Parameters fptr : file

Open file object.

offset : int

Start position of box in bytes.

length : int

7.2. Individual Boxes 35

Page 40: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

Length of the box in bytes.

Returns ComponentMappingBox instance :

write(fptr)Write a Component Mapping box to file.

7.2.6 ContiguousCodestreamBox

class glymur.jp2box.ContiguousCodestreamBox(main_header=None,main_header_offset=None, length=0, offset=-1)

Container for JPEG2000 codestream information.

Attributes

box_idlongnamemain_header

length int length of the box in bytes.offset int offset of the box from the start of the file.main_header_offset int offset of main header from start of file

Methods

parseparse_superboxwrite

classmethod parse(fptr, offset=0, length=0)Parse a codestream box.

Parameters fptr : file

Open file object.

offset : int

Start position of box in bytes.

length : int

Length of the box in bytes.

Returns ContiguousCodestreamBox instance :

7.2.7 DataEntryURLBox

class glymur.jp2box.DataEntryURLBox(version, flag, url, length=0, offset=-1)Container for data entry URL box information.

36 Chapter 7. API

Page 41: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

Attributes

box_idlongname

length int length of the box in bytes.offset int offset of the box from the start of the file.version byte Must be 0 for JP2.flag bytes Particular attributes of this box, consists of three bytes.URL str Associated URL.

Methods

parseparse_superboxwrite

classmethod parse(fptr, offset, length)Parse data entry URL box.

Parameters fptr : file

Open file object.

offset : int

Start position of box in bytes.

length : int

Length of the box in bytes.

Returns DataEntryURLbox instance :

write(fptr)Write a data entry url box to file.

7.2.8 FileTypeBox

class glymur.jp2box.FileTypeBox(brand=’jp2 ‘, minor_version=0, compatibility_list=None,length=0, offset=-1)

Container for JPEG 2000 file type box information.

Attributes

box_idlongname

7.2. Individual Boxes 37

Page 42: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

length int length of the box in bytes.offset int offset of the box from the start of the file.brand: str Specifies the governing recommendation or standard upon which this file is

based.minor_version: int Minor version number identifying the JP2 specification used.compatibility_list:list

List of file conformance profiles.

Methods

parseparse_superboxwrite

classmethod parse(fptr, offset, length)Parse JPEG 2000 file type box.

Parameters f : file

Open file object.

offset : int

Start position of box in bytes.

length : int

Length of the box in bytes.

Returns FileTypeBox instance :

write(fptr)Write a File Type box to file.

7.2.9 ImageHeaderBox

class glymur.jp2box.ImageHeaderBox(height, width, num_components=1, signed=False,bits_per_component=8, compression=7, col-orspace_unknown=False, ip_provided=False, length=0,offset=-1)

Container for JPEG 2000 image header box information.

Attributes

box_idlongname

38 Chapter 7. API

Page 43: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

length int length of the box in bytes.offset int offset of the box from the start of the file.height, width int Height and width of image.num_components int Number of image channels.bits_per_component int Bits per component.signed bool False if the image components are unsigned.compression int The compression type, should be 7 if JP2.colorspace_unknown bool False if the color space is known and correctly specified.ip_provided bool False if the file does not contain intellectual propery rights information.

Methods

parseparse_superboxwrite

classmethod parse(fptr, offset, length)Parse JPEG 2000 image header box.

Parameters fptr : file

Open file object.

offset : int

Start position of box in bytes.

length : int

Length of the box in bytes.

Returns ImageHeaderBox instance :

write(fptr)Write an Image Header box to file.

7.2.10 JP2HeaderBox

class glymur.jp2box.JP2HeaderBox(box=None, length=0, offset=-1)Container for JP2 header box information.

Attributes

box_idlongname

length int length of the box in bytes.offset int offset of the box from the start of the file.box list List of boxes contained in this superbox.

Methods

7.2. Individual Boxes 39

Page 44: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

parseparse_superboxwrite

classmethod parse(fptr, offset, length)Parse JPEG 2000 header box.

Parameters fptr : file

Open file object.

offset : int

Start position of box in bytes.

length : int

Length of the box in bytes.

Returns JP2HeaderBox instance :

write(fptr)Write a JP2 Header box to file.

7.2.11 JPEG2000SignatureBox

class glymur.jp2box.JPEG2000SignatureBox(signature=(13, 10, 135, 10), length=0, offset=-1)Container for JPEG 2000 signature box information.

Attributes

box_idlongname

length int length of the box in bytes.offset int offset of the box from the start of the file.signature tuple Four-byte tuple identifying the file as JPEG 2000.

Methods

parseparse_superboxwrite

classmethod parse(fptr, offset, length)Parse JPEG 2000 signature box.

Parameters fptr : file

Open file object.

offset : int

40 Chapter 7. API

Page 45: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

Start position of box in bytes.

length : int

Length of the box in bytes.

Returns JPEG2000SignatureBox instance :

write(fptr)Write a JPEG 2000 Signature box to file.

7.2.12 LabelBox

class glymur.jp2box.LabelBox(label, length=0, offset=-1)Container for Label box information.

Attributes

box_idlongname

length int length of the box in bytes.offset int offset of the box from the start of the file.label str Textual label.

Methods

parseparse_superboxwrite

classmethod parse(fptr, offset, length)Parse Label box.

Parameters fptr : file

Open file object.

offset : int

Start position of box in bytes.

length : int

Length of the box in bytes.

Returns LabelBox instance :

write(fptr)Write a Label box to file.

7.2. Individual Boxes 41

Page 46: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

7.2.13 PaletteBox

class glymur.jp2box.PaletteBox(palette, bits_per_component, signed, length=0, offset=-1)Container for palette box information.

Attributes

box_idlongname

length int length of the box in bytes.offset int offset of the box from the start of the file.palette ndarray Colormap array.

Methods

parseparse_superboxwrite

classmethod parse(fptr, offset, length)Parse palette box.

Parameters fptr : file

Open file object.

offset : int

Start position of box in bytes.

length : int

Length of the box in bytes.

Returns PaletteBox instance :

write(fptr)Write a Palette box to file.

7.2.14 ReaderRequirementsBox

class glymur.jp2box.ReaderRequirementsBox(fuam, dcm, standard_flag, standard_mask, ven-dor_feature, vendor_mask, length=0, offset=-1)

Container for reader requirements box information.

Attributes

box_idlongname

42 Chapter 7. API

Page 47: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

length int length of the box in bytes.offset int offset of the box from the start of the file.fuam int Fully Understand Aspects mask.dcm int Decode completely mask.standard_flag list Integers specifying standard features.standard_mask list Specifies the compatibility mask for each corresponding standard flag.vendor_feature list Each item is a UUID corresponding to a vendor defined feature.vendor_mask list Specifies the compatibility mask for each corresponding vendor feature.

Methods

parseparse_superboxwrite

classmethod parse(fptr, offset, length)Parse reader requirements box.

Parameters fptr : file

Open file object.

offset : int

Start position of box in bytes.

length : int

Length of the box in bytes.

Returns ReaderRequirementsBox instance :

7.2.15 ResolutionBox

class glymur.jp2box.ResolutionBox(box=None, length=0, offset=-1)Container for Resolution superbox information.

Attributes

box_idlongname

length int length of the box in bytes.offset int offset of the box from the start of the file.box list List of boxes contained in this superbox.

Methods

parseparse_superbox

Continued on next page

7.2. Individual Boxes 43

Page 48: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

Table 7.30 – continued from previous pagewrite

classmethod parse(fptr, offset, length)Parse Resolution box.

Parameters fptr : file

Open file object.

offset : int

Start position of box in bytes.

length : int

Length of the box in bytes.

Returns ResolutionBox instance :

7.2.16 CaptureResolutionBox

class glymur.jp2box.CaptureResolutionBox(vertical_resolution, horizontal_resolution, length=0,offset=-1)

Container for Capture resolution box information.

Attributes

box_idlongname

length int length of the box in bytes.offset int offset of the box from the start of the file.vertical_resolution, horizontal_resolution float Vertical, horizontal resolution.

Methods

parseparse_superboxwrite

classmethod parse(fptr, offset, length)Parse CaptureResolutionBox.

Parameters fptr : file

Open file object.

offset : int

Start position of box in bytes.

length : int

Length of the box in bytes.

44 Chapter 7. API

Page 49: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

Returns CaptureResolutionBox instance :

7.2.17 DisplayResolutionBox

class glymur.jp2box.DisplayResolutionBox(vertical_resolution, horizontal_resolution, length=0,offset=-1)

Container for Display resolution box information.

Attributes

box_idlongname

length int length of the box in bytes.offset int offset of the box from the start of the file.vertical_resolution, horizontal_resolution float Vertical, horizontal resolution.

Methods

parseparse_superboxwrite

classmethod parse(fptr, offset, length)Parse display resolution box.

Parameters fptr : file

Open file object.

offset : int

Start position of box in bytes.

length : int

Length of the box in bytes.

Returns DisplayResolutionBox instance :

7.2.18 UUIDBox

class glymur.jp2box.UUIDBox(the_uuid, raw_data, length=0, offset=-1)Container for UUID box information.

References

[XMP]

7.2. Individual Boxes 45

Page 50: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

Attributes

box_idlongname

Methods

parseparse_superboxwrite

classmethod parse(fptr, offset, length)Parse UUID box.

Parameters fptr : file

Open file object.

offset : int

Start position of box in bytes.

length : int

Length of the box in bytes.

Returns UUIDBox instance :

write(fptr)Write a UUID box to file.

7.2.19 UUIDInfoBox

class glymur.jp2box.UUIDInfoBox(box=None, length=0, offset=-1)Container for JPEG 2000 UUID Info superbox.

Attributes

box_idlongname

length int length of the box in bytes.offset int offset of the box from the start of the file.box list List of boxes contained in this superbox.

Methods

parseparse_superbox

Continued on next page

46 Chapter 7. API

Page 51: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

Table 7.38 – continued from previous pagewrite

classmethod parse(fptr, offset, length)Parse UUIDInfo super box.

Parameters fptr : file

Open file object.

offset : int

Start position of box in bytes.

length : int

Length of the box in bytes.

Returns UUIDInfoBox instance :

7.2.20 UUIDListBox

class glymur.jp2box.UUIDListBox(ulst, length=0, offset=-1)Container for JPEG 2000 UUID list box.

Attributes

box_idlongname

length int length of the box in bytes.offset int offset of the box from the start of the file.ulst list List of UUIDs.

Methods

parseparse_superboxwrite

classmethod parse(fptr, offset, length)Parse UUIDList box.

Parameters f : file

Open file object.

offset : int

Start position of box in bytes.

length : int

Length of the box in bytes.

7.2. Individual Boxes 47

Page 52: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

Returns UUIDListBox instance :

7.2.21 XMLBox

class glymur.jp2box.XMLBox(xml=None, filename=None, length=0, offset=-1)Container for XML box information.

Attributes

box_idlongname

length int length of the box in bytes.offset int offset of the box from the start of the file.xml ElementTree object XML section.

Methods

parseparse_superboxwrite

classmethod parse(fptr, offset, length)Parse XML box.

Parameters fptr : file

Open file object.

offset : int

Start position of box in bytes.

length : int

Length of the box in bytes.

Returns XMLBox instance :

write(fptr)Write an XML box to file.

48 Chapter 7. API

Page 53: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

CHAPTER 8

Indices and tables

• genindex

• modindex

• search

49

Page 54: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

50 Chapter 8. Indices and tables

Page 55: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

Bibliography

[XMP] International Organization for Standardication. ISO/IEC 16684-1:2012 - Graphic technology – Extensiblemetadata platform (XMP) specification – Part 1: Data model, serialization and core properties

51

Page 56: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

52 Bibliography

Page 57: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

Index

AAssociationBox (class in glymur.jp2box), 32

CCaptureResolutionBox (class in glymur.jp2box), 44ChannelDefinitionBox (class in glymur.jp2box), 34ColourSpecificationBox (class in glymur.jp2box), 33ComponentMappingBox (class in glymur.jp2box), 35ContiguousCodestreamBox (class in glymur.jp2box), 36

DDataEntryURLBox (class in glymur.jp2box), 36DisplayResolutionBox (class in glymur.jp2box), 45

FFileTypeBox (class in glymur.jp2box), 37

Gget_codestream() (glymur.Jp2k method), 27

IImageHeaderBox (class in glymur.jp2box), 38

JJP2HeaderBox (class in glymur.jp2box), 39Jp2k (class in glymur), 27Jp2kBox (class in glymur.jp2box), 32JPEG2000SignatureBox (class in glymur.jp2box), 40

LLabelBox (class in glymur.jp2box), 41

PPaletteBox (class in glymur.jp2box), 42parse() (glymur.jp2box.AssociationBox class method), 33parse() (glymur.jp2box.CaptureResolutionBox class

method), 44parse() (glymur.jp2box.ChannelDefinitionBox class

method), 35

parse() (glymur.jp2box.ColourSpecificationBox classmethod), 34

parse() (glymur.jp2box.ComponentMappingBox classmethod), 35

parse() (glymur.jp2box.ContiguousCodestreamBox classmethod), 36

parse() (glymur.jp2box.DataEntryURLBox classmethod), 37

parse() (glymur.jp2box.DisplayResolutionBox classmethod), 45

parse() (glymur.jp2box.FileTypeBox class method), 38parse() (glymur.jp2box.ImageHeaderBox class method),

39parse() (glymur.jp2box.JP2HeaderBox class method), 40parse() (glymur.jp2box.JPEG2000SignatureBox class

method), 40parse() (glymur.jp2box.LabelBox class method), 41parse() (glymur.jp2box.PaletteBox class method), 42parse() (glymur.jp2box.ReaderRequirementsBox class

method), 43parse() (glymur.jp2box.ResolutionBox class method), 44parse() (glymur.jp2box.UUIDBox class method), 46parse() (glymur.jp2box.UUIDInfoBox class method), 47parse() (glymur.jp2box.UUIDListBox class method), 47parse() (glymur.jp2box.XMLBox class method), 48parse_superbox() (glymur.jp2box.Jp2kBox method), 32

Rread() (glymur.Jp2k method), 28read_bands() (glymur.Jp2k method), 29ReaderRequirementsBox (class in glymur.jp2box), 42ResolutionBox (class in glymur.jp2box), 43

UUUIDBox (class in glymur.jp2box), 45UUIDInfoBox (class in glymur.jp2box), 46UUIDListBox (class in glymur.jp2box), 47

Wwrap() (glymur.Jp2k method), 30

53

Page 58: glymur Documentationglymur Documentation, Release 0.7.3 2.2Testing It is not necessary, but you may wish to download OpenJPEG’s test data for the purpose of configuring and run-

glymur Documentation, Release 0.7.3

write() (glymur.jp2box.AssociationBox method), 33write() (glymur.jp2box.ChannelDefinitionBox method),

35write() (glymur.jp2box.ColourSpecificationBox method),

34write() (glymur.jp2box.ComponentMappingBox

method), 36write() (glymur.jp2box.DataEntryURLBox method), 37write() (glymur.jp2box.FileTypeBox method), 38write() (glymur.jp2box.ImageHeaderBox method), 39write() (glymur.jp2box.JP2HeaderBox method), 40write() (glymur.jp2box.Jp2kBox method), 32write() (glymur.jp2box.JPEG2000SignatureBox method),

41write() (glymur.jp2box.LabelBox method), 41write() (glymur.jp2box.PaletteBox method), 42write() (glymur.jp2box.UUIDBox method), 46write() (glymur.jp2box.XMLBox method), 48write() (glymur.Jp2k method), 30

XXMLBox (class in glymur.jp2box), 48

54 Index