Glymur 0.13.8__py3-none-any.whl → 0.14.0.post2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- glymur/__init__.py +2 -1
- glymur/_core_converter.py +390 -0
- glymur/command_line.py +129 -1
- glymur/jp2box.py +31 -8
- glymur/jpeg.py +196 -0
- glymur/lib/tiff.py +132 -4
- glymur/tiff.py +28 -359
- glymur/version.py +1 -1
- {Glymur-0.13.8.dist-info → glymur-0.14.0.post2.dist-info}/METADATA +5 -4
- glymur-0.14.0.post2.dist-info/RECORD +27 -0
- {Glymur-0.13.8.dist-info → glymur-0.14.0.post2.dist-info}/WHEEL +1 -1
- {Glymur-0.13.8.dist-info → glymur-0.14.0.post2.dist-info}/entry_points.txt +1 -0
- Glymur-0.13.8.dist-info/RECORD +0 -25
- {Glymur-0.13.8.dist-info → glymur-0.14.0.post2.dist-info/licenses}/LICENSE.txt +0 -0
- {Glymur-0.13.8.dist-info → glymur-0.14.0.post2.dist-info}/top_level.txt +0 -0
glymur/__init__.py
CHANGED
|
@@ -5,7 +5,7 @@ __all__ = [
|
|
|
5
5
|
'get_option', 'set_option', 'reset_option',
|
|
6
6
|
'get_printoptions', 'set_printoptions',
|
|
7
7
|
'get_parseoptions', 'set_parseoptions',
|
|
8
|
-
'Jp2k', 'Jp2kr', 'Tiff2Jp2k',
|
|
8
|
+
'Jp2k', 'Jp2kr', 'JPEG2JP2', 'Tiff2Jp2k',
|
|
9
9
|
]
|
|
10
10
|
|
|
11
11
|
# Local imports
|
|
@@ -13,6 +13,7 @@ from glymur import version
|
|
|
13
13
|
from .options import (get_option, set_option, reset_option,
|
|
14
14
|
get_printoptions, set_printoptions,
|
|
15
15
|
get_parseoptions, set_parseoptions)
|
|
16
|
+
from .jpeg import JPEG2JP2
|
|
16
17
|
from .jp2k import Jp2k, Jp2kr
|
|
17
18
|
from .tiff import Tiff2Jp2k
|
|
18
19
|
from . import data
|
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
"""Core definitions to be shared amongst the modules."""
|
|
2
|
+
# standard library imports
|
|
3
|
+
import io
|
|
4
|
+
import logging
|
|
5
|
+
import shutil
|
|
6
|
+
import struct
|
|
7
|
+
from typing import Tuple
|
|
8
|
+
from uuid import UUID
|
|
9
|
+
|
|
10
|
+
# local imports
|
|
11
|
+
from . import jp2box
|
|
12
|
+
from .lib.tiff import DATATYPE2FMT
|
|
13
|
+
from .jp2k import Jp2k
|
|
14
|
+
from glymur.core import RESTRICTED_ICC_PROFILE
|
|
15
|
+
|
|
16
|
+
# Mnemonics for the two TIFF format version numbers.
|
|
17
|
+
TIFF = 42
|
|
18
|
+
BIGTIFF = 43
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class _2JP2Converter(object):
|
|
22
|
+
"""
|
|
23
|
+
This private class is used by both the TIFF2JP2 and the JPEG2JP2
|
|
24
|
+
converters.
|
|
25
|
+
|
|
26
|
+
Attributes
|
|
27
|
+
----------
|
|
28
|
+
create_exif_uuid : bool
|
|
29
|
+
Create a UUIDBox for the TIFF IFD metadata.
|
|
30
|
+
tilesize : tuple
|
|
31
|
+
The dimensions of a tile in the JP2K file.
|
|
32
|
+
verbosity : int
|
|
33
|
+
Set the level of logging, i.e. WARNING, INFO, etc.
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
def __init__(
|
|
37
|
+
self,
|
|
38
|
+
create_exif_uuid: bool,
|
|
39
|
+
create_xmp_uuid: bool,
|
|
40
|
+
include_icc_profile: bool,
|
|
41
|
+
tilesize: Tuple[int, int] | None,
|
|
42
|
+
verbosity: int
|
|
43
|
+
):
|
|
44
|
+
|
|
45
|
+
self.create_exif_uuid = create_exif_uuid
|
|
46
|
+
self.create_xmp_uuid = create_xmp_uuid
|
|
47
|
+
self.include_icc_profile = include_icc_profile
|
|
48
|
+
self.tilesize = tilesize
|
|
49
|
+
|
|
50
|
+
# Assume that there is no ICC profile tag until we know otherwise.
|
|
51
|
+
self.icc_profile = None
|
|
52
|
+
|
|
53
|
+
self.setup_logging(verbosity)
|
|
54
|
+
|
|
55
|
+
def read_ifd(self, tfp):
|
|
56
|
+
"""Process either the main IFD or an Exif IFD
|
|
57
|
+
|
|
58
|
+
Parameters
|
|
59
|
+
----------
|
|
60
|
+
tfp : file-like
|
|
61
|
+
FILE pointer for TIFF
|
|
62
|
+
|
|
63
|
+
Returns
|
|
64
|
+
-------
|
|
65
|
+
dictionary of the TIFF IFD
|
|
66
|
+
"""
|
|
67
|
+
|
|
68
|
+
self.found_geotiff_tags = False
|
|
69
|
+
|
|
70
|
+
tag_length = 20 if self.version == BIGTIFF else 12
|
|
71
|
+
|
|
72
|
+
# how many tags?
|
|
73
|
+
if self.version == BIGTIFF:
|
|
74
|
+
buffer = tfp.read(8)
|
|
75
|
+
(num_tags,) = struct.unpack(self.endian + "Q", buffer)
|
|
76
|
+
else:
|
|
77
|
+
buffer = tfp.read(2)
|
|
78
|
+
(num_tags,) = struct.unpack(self.endian + "H", buffer)
|
|
79
|
+
|
|
80
|
+
# Ok, so now we have the IFD main body, but following that we have
|
|
81
|
+
# the tag payloads that cannot fit into 4 bytes.
|
|
82
|
+
|
|
83
|
+
# the IFD main body in the TIFF. As it might be big endian, we
|
|
84
|
+
# cannot just process it as one big chunk.
|
|
85
|
+
buffer = tfp.read(num_tags * tag_length)
|
|
86
|
+
|
|
87
|
+
if self.version == BIGTIFF:
|
|
88
|
+
tag_format_str = self.endian + "HHQQ"
|
|
89
|
+
tag_payload_offset = 12
|
|
90
|
+
max_tag_payload_length = 8
|
|
91
|
+
else:
|
|
92
|
+
tag_format_str = self.endian + "HHII"
|
|
93
|
+
tag_payload_offset = 8
|
|
94
|
+
max_tag_payload_length = 4
|
|
95
|
+
|
|
96
|
+
tags = {}
|
|
97
|
+
|
|
98
|
+
for idx in range(num_tags):
|
|
99
|
+
|
|
100
|
+
self.logger.debug(f"tag #: {idx}")
|
|
101
|
+
|
|
102
|
+
tag_data = buffer[idx * tag_length:(idx + 1) * tag_length]
|
|
103
|
+
|
|
104
|
+
tag, dtype, nvalues, offset = struct.unpack(
|
|
105
|
+
tag_format_str, tag_data
|
|
106
|
+
) # noqa : E501
|
|
107
|
+
|
|
108
|
+
if tag == 34735:
|
|
109
|
+
self.found_geotiff_tags = True
|
|
110
|
+
|
|
111
|
+
payload_length = DATATYPE2FMT[dtype]["nbytes"] * nvalues
|
|
112
|
+
|
|
113
|
+
if tag in (34665, 34853):
|
|
114
|
+
|
|
115
|
+
# found exif or gps ifd
|
|
116
|
+
# save our location, go get that IFD, and come on back
|
|
117
|
+
orig_pos = tfp.tell()
|
|
118
|
+
tfp.seek(offset)
|
|
119
|
+
payload = self.read_ifd(tfp)
|
|
120
|
+
tfp.seek(orig_pos)
|
|
121
|
+
|
|
122
|
+
elif payload_length > max_tag_payload_length:
|
|
123
|
+
# the payload does not fit into the tag entry, so use the
|
|
124
|
+
# offset to seek to that position
|
|
125
|
+
current_position = tfp.tell()
|
|
126
|
+
tfp.seek(offset)
|
|
127
|
+
payload_buffer = tfp.read(payload_length)
|
|
128
|
+
tfp.seek(current_position)
|
|
129
|
+
|
|
130
|
+
# read the payload from the TIFF
|
|
131
|
+
payload_format = DATATYPE2FMT[dtype]["format"] * nvalues
|
|
132
|
+
payload = struct.unpack(
|
|
133
|
+
self.endian + payload_format,
|
|
134
|
+
payload_buffer
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
else:
|
|
138
|
+
# the payload DOES fit into the TIFF tag entry
|
|
139
|
+
payload_buffer = tag_data[tag_payload_offset:]
|
|
140
|
+
|
|
141
|
+
# read ALL of the payload buffer
|
|
142
|
+
fmt = DATATYPE2FMT[dtype]["format"]
|
|
143
|
+
nelts = max_tag_payload_length / DATATYPE2FMT[dtype]["nbytes"]
|
|
144
|
+
num_items = int(nelts)
|
|
145
|
+
payload_format = self.endian + fmt * num_items
|
|
146
|
+
payload = struct.unpack(payload_format, payload_buffer)
|
|
147
|
+
|
|
148
|
+
# Extract the actual payload. Two things going
|
|
149
|
+
# on here. First of all, not all of the items may
|
|
150
|
+
# be used. For example, if the payload length is
|
|
151
|
+
# 4 bytes but the format string was HHH, the that
|
|
152
|
+
# last 16 bit value is not wanted, so we should
|
|
153
|
+
# discard it. Second thing is that the signed and
|
|
154
|
+
# unsigned rational datatypes effectively have twice
|
|
155
|
+
# the number of values so we need to account for that.
|
|
156
|
+
if dtype in [5, 10]:
|
|
157
|
+
payload = payload[: 2 * nvalues]
|
|
158
|
+
else:
|
|
159
|
+
payload = payload[:nvalues]
|
|
160
|
+
|
|
161
|
+
tags[tag] = {"dtype": dtype, "nvalues": nvalues, "payload": payload}
|
|
162
|
+
|
|
163
|
+
return tags
|
|
164
|
+
|
|
165
|
+
def setup_logging(self, verbosity):
|
|
166
|
+
|
|
167
|
+
self.logger = logging.getLogger("tiff2jp2")
|
|
168
|
+
self.logger.setLevel(verbosity)
|
|
169
|
+
ch = logging.StreamHandler()
|
|
170
|
+
ch.setLevel(verbosity)
|
|
171
|
+
self.logger.addHandler(ch)
|
|
172
|
+
|
|
173
|
+
def read_tiff_header(self, tfp):
|
|
174
|
+
"""Get the endian-ness of the TIFF, seek to the main IFD"""
|
|
175
|
+
|
|
176
|
+
buffer = tfp.read(4)
|
|
177
|
+
data = struct.unpack("BB", buffer[:2])
|
|
178
|
+
|
|
179
|
+
# big endian or little endian?
|
|
180
|
+
if data[0] == 73 and data[1] == 73:
|
|
181
|
+
# little endian
|
|
182
|
+
self.endian = "<"
|
|
183
|
+
elif data[0] == 77 and data[1] == 77:
|
|
184
|
+
# big endian
|
|
185
|
+
self.endian = ">"
|
|
186
|
+
# no other option is possible, libtiff.open would have errored out
|
|
187
|
+
# else:
|
|
188
|
+
# msg = (
|
|
189
|
+
# f"The byte order indication in the TIFF header "
|
|
190
|
+
# f"({data}) is invalid. It should be either "
|
|
191
|
+
# f"{bytes([73, 73])} or {bytes([77, 77])}."
|
|
192
|
+
# )
|
|
193
|
+
# raise RuntimeError(msg)
|
|
194
|
+
|
|
195
|
+
# version number and offset to the first IFD
|
|
196
|
+
(version,) = struct.unpack(self.endian + "H", buffer[2:4])
|
|
197
|
+
self.version = TIFF if version == 42 else BIGTIFF
|
|
198
|
+
|
|
199
|
+
if self.version == BIGTIFF:
|
|
200
|
+
buffer = tfp.read(12)
|
|
201
|
+
_, _, offset = struct.unpack(self.endian + "HHQ", buffer)
|
|
202
|
+
else:
|
|
203
|
+
buffer = tfp.read(4)
|
|
204
|
+
(offset,) = struct.unpack(self.endian + "I", buffer)
|
|
205
|
+
tfp.seek(offset)
|
|
206
|
+
|
|
207
|
+
def append_exif_uuid_box(self):
|
|
208
|
+
"""Append an EXIF UUID box onto the end of the JPEG 2000 file. It will
|
|
209
|
+
contain metadata from the TIFF IFD.
|
|
210
|
+
"""
|
|
211
|
+
if not self.create_exif_uuid:
|
|
212
|
+
return
|
|
213
|
+
|
|
214
|
+
# create a bytesio object for the IFD
|
|
215
|
+
b = io.BytesIO()
|
|
216
|
+
|
|
217
|
+
# write this 32-bit header into the UUID, no matter if we had bigtiff
|
|
218
|
+
# or regular tiff or big endian
|
|
219
|
+
data = struct.pack("<BBHI", 73, 73, 42, 8)
|
|
220
|
+
b.write(data)
|
|
221
|
+
|
|
222
|
+
self.write_ifd(b, self.tags)
|
|
223
|
+
|
|
224
|
+
# create the Exif UUID
|
|
225
|
+
if self.found_geotiff_tags:
|
|
226
|
+
# geotiff UUID
|
|
227
|
+
the_uuid = UUID("b14bf8bd-083d-4b43-a5ae-8cd7d5a6ce03")
|
|
228
|
+
payload = b.getvalue()
|
|
229
|
+
else:
|
|
230
|
+
# Make it an exif UUID.
|
|
231
|
+
the_uuid = UUID(bytes=b"JpgTiffExif->JP2")
|
|
232
|
+
payload = b"EXIF\0\0" + b.getvalue()
|
|
233
|
+
|
|
234
|
+
# the length of the box is the length of the payload plus 8 bytes
|
|
235
|
+
# to store the length of the box and the box ID
|
|
236
|
+
box_length = len(payload) + 8
|
|
237
|
+
|
|
238
|
+
uuid_box = jp2box.UUIDBox(the_uuid, payload, box_length)
|
|
239
|
+
with self.jp2_path.open(mode="ab") as f:
|
|
240
|
+
uuid_box.write(f)
|
|
241
|
+
|
|
242
|
+
self.jp2.finalize(force_parse=True)
|
|
243
|
+
|
|
244
|
+
def write_ifd(self, b, tags):
|
|
245
|
+
"""Write the IFD out to the UUIDBox. We will always write IFDs
|
|
246
|
+
for 32-bit TIFFs, i.e. 12 byte tags, meaning just 4 bytes within
|
|
247
|
+
the tag for the tag data
|
|
248
|
+
"""
|
|
249
|
+
|
|
250
|
+
little_tiff_tag_length = 12
|
|
251
|
+
max_tag_payload_length = 4
|
|
252
|
+
|
|
253
|
+
# exclude any unwanted tags
|
|
254
|
+
if self.exclude_tags is not None:
|
|
255
|
+
for tag in self.exclude_tags:
|
|
256
|
+
if tag in tags:
|
|
257
|
+
tags.pop(tag)
|
|
258
|
+
|
|
259
|
+
num_tags = len(tags)
|
|
260
|
+
write_buffer = struct.pack("<H", num_tags)
|
|
261
|
+
b.write(write_buffer)
|
|
262
|
+
|
|
263
|
+
# Ok, so now we have the IFD main body, but following that we have
|
|
264
|
+
# the tag payloads that cannot fit into 4 bytes.
|
|
265
|
+
|
|
266
|
+
ifd_start_loc = b.tell()
|
|
267
|
+
after_ifd_position = ifd_start_loc + num_tags * little_tiff_tag_length
|
|
268
|
+
|
|
269
|
+
for idx, tag in enumerate(tags):
|
|
270
|
+
|
|
271
|
+
tag_offset = ifd_start_loc + idx * little_tiff_tag_length
|
|
272
|
+
self.logger.debug(f"tag #: {tag}, writing to {tag_offset}")
|
|
273
|
+
self.logger.debug(f"tag #: {tag}, after IFD {after_ifd_position}")
|
|
274
|
+
|
|
275
|
+
b.seek(tag_offset)
|
|
276
|
+
|
|
277
|
+
try:
|
|
278
|
+
dtype = tags[tag]["dtype"]
|
|
279
|
+
except IndexError:
|
|
280
|
+
breakpoint()
|
|
281
|
+
pass
|
|
282
|
+
|
|
283
|
+
nvalues = tags[tag]["nvalues"]
|
|
284
|
+
payload = tags[tag]["payload"]
|
|
285
|
+
|
|
286
|
+
payload_length = DATATYPE2FMT[dtype]["nbytes"] * nvalues
|
|
287
|
+
|
|
288
|
+
if payload_length > max_tag_payload_length:
|
|
289
|
+
|
|
290
|
+
# the payload does not fit into the tag entry
|
|
291
|
+
|
|
292
|
+
# read the payload from the TIFF
|
|
293
|
+
payload_format = DATATYPE2FMT[dtype]["format"] * nvalues
|
|
294
|
+
|
|
295
|
+
# write the tag entry to the UUID
|
|
296
|
+
new_offset = after_ifd_position
|
|
297
|
+
buffer = struct.pack("<HHII", tag, dtype, nvalues, new_offset)
|
|
298
|
+
b.write(buffer)
|
|
299
|
+
|
|
300
|
+
# now write the payload at the outlying position and then come
|
|
301
|
+
# back to the same position in the file stream
|
|
302
|
+
cpos = b.tell()
|
|
303
|
+
b.seek(new_offset)
|
|
304
|
+
|
|
305
|
+
format = "<" + DATATYPE2FMT[dtype]["format"] * nvalues
|
|
306
|
+
buffer = struct.pack(format, *payload)
|
|
307
|
+
b.write(buffer)
|
|
308
|
+
|
|
309
|
+
# keep track of the next position to write out-of-IFD data
|
|
310
|
+
after_ifd_position = b.tell()
|
|
311
|
+
b.seek(cpos)
|
|
312
|
+
|
|
313
|
+
else:
|
|
314
|
+
|
|
315
|
+
# the payload DOES fit into the TIFF tag entry
|
|
316
|
+
# write the tag metadata
|
|
317
|
+
buffer = struct.pack("<HHI", tag, dtype, nvalues)
|
|
318
|
+
b.write(buffer)
|
|
319
|
+
|
|
320
|
+
payload_format = DATATYPE2FMT[dtype]["format"] * nvalues
|
|
321
|
+
|
|
322
|
+
# we may need to alter the output format
|
|
323
|
+
if payload_format in ["H", "B", "I"]:
|
|
324
|
+
# just write it as an integer
|
|
325
|
+
payload_format = "I"
|
|
326
|
+
|
|
327
|
+
if tag in (34665, 34853):
|
|
328
|
+
|
|
329
|
+
# special case for an EXIF or GPS IFD
|
|
330
|
+
buffer = struct.pack("<I", after_ifd_position)
|
|
331
|
+
b.write(buffer)
|
|
332
|
+
b.seek(after_ifd_position)
|
|
333
|
+
after_ifd_position = self.write_ifd(b, payload)
|
|
334
|
+
|
|
335
|
+
else:
|
|
336
|
+
|
|
337
|
+
buffer = struct.pack("<" + payload_format, *payload)
|
|
338
|
+
b.write(buffer)
|
|
339
|
+
|
|
340
|
+
return after_ifd_position
|
|
341
|
+
|
|
342
|
+
def append_xmp_uuid_box(self):
|
|
343
|
+
"""Append an XMP UUID box onto the end of the JPEG 2000 file if there
|
|
344
|
+
was an XMP tag in the TIFF IFD.
|
|
345
|
+
"""
|
|
346
|
+
|
|
347
|
+
if self.xmp_data is None:
|
|
348
|
+
return
|
|
349
|
+
|
|
350
|
+
if not self.create_xmp_uuid:
|
|
351
|
+
return
|
|
352
|
+
|
|
353
|
+
# create the XMP UUID
|
|
354
|
+
the_uuid = jp2box.UUID("be7acfcb-97a9-42e8-9c71-999491e3afac")
|
|
355
|
+
box_length = len(self.xmp_data) + 8
|
|
356
|
+
uuid_box = jp2box.UUIDBox(the_uuid, self.xmp_data, box_length)
|
|
357
|
+
with self.jp2_path.open(mode="ab") as f:
|
|
358
|
+
uuid_box.write(f)
|
|
359
|
+
|
|
360
|
+
def rewrap_for_icc_profile(self):
|
|
361
|
+
"""Consume an ICC profile, if one is there."""
|
|
362
|
+
if self.icc_profile is None and self.include_icc_profile:
|
|
363
|
+
self.logger.warning("No ICC profile was found.")
|
|
364
|
+
|
|
365
|
+
if self.icc_profile is None or not self.include_icc_profile:
|
|
366
|
+
return
|
|
367
|
+
|
|
368
|
+
self.logger.info(
|
|
369
|
+
"Consuming an ICC profile into JP2 color specification box."
|
|
370
|
+
)
|
|
371
|
+
|
|
372
|
+
colr = jp2box.ColourSpecificationBox(
|
|
373
|
+
method=RESTRICTED_ICC_PROFILE,
|
|
374
|
+
precedence=0,
|
|
375
|
+
icc_profile=self.icc_profile
|
|
376
|
+
)
|
|
377
|
+
|
|
378
|
+
# construct the new set of JP2 boxes, insert the color specification
|
|
379
|
+
# box with the ICC profile
|
|
380
|
+
jp2 = Jp2k(self.jp2_path)
|
|
381
|
+
boxes = jp2.box
|
|
382
|
+
boxes[2].box = [boxes[2].box[0], colr]
|
|
383
|
+
|
|
384
|
+
# re-wrap the codestream, involves a file copy
|
|
385
|
+
tmp_filename = str(self.jp2_path) + ".tmp"
|
|
386
|
+
|
|
387
|
+
with open(tmp_filename, mode="wb") as tfile:
|
|
388
|
+
jp2.wrap(tfile.name, boxes=boxes)
|
|
389
|
+
|
|
390
|
+
shutil.move(tmp_filename, self.jp2_path)
|
glymur/command_line.py
CHANGED
|
@@ -7,7 +7,7 @@ import warnings
|
|
|
7
7
|
|
|
8
8
|
# Local imports ...
|
|
9
9
|
from . import Jp2k, set_option, lib
|
|
10
|
-
from . import tiff
|
|
10
|
+
from . import tiff, jpeg
|
|
11
11
|
|
|
12
12
|
|
|
13
13
|
def main():
|
|
@@ -78,6 +78,134 @@ def main():
|
|
|
78
78
|
)
|
|
79
79
|
|
|
80
80
|
|
|
81
|
+
def jpeg2jp2():
|
|
82
|
+
"""Entry point for console script jpeg2jp2."""
|
|
83
|
+
|
|
84
|
+
kwargs = {
|
|
85
|
+
'description': 'Convert JPEG to JPEG 2000.',
|
|
86
|
+
'formatter_class': argparse.ArgumentDefaultsHelpFormatter,
|
|
87
|
+
'add_help': False
|
|
88
|
+
}
|
|
89
|
+
parser = argparse.ArgumentParser(**kwargs)
|
|
90
|
+
|
|
91
|
+
group1 = parser.add_argument_group(
|
|
92
|
+
'JP2K', 'Pass-through arguments to Jp2k.'
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
help = 'Capture resolution parameters'
|
|
96
|
+
group1.add_argument(
|
|
97
|
+
'--capture-resolution', nargs=2, type=float, help=help,
|
|
98
|
+
metavar=('VRESC', 'HRESC')
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
help = 'Display resolution parameters'
|
|
102
|
+
group1.add_argument(
|
|
103
|
+
'--display-resolution', nargs=2, type=float, help=help,
|
|
104
|
+
metavar=('VRESD', 'HRESD')
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
help = 'Compression ratios for successive layers.'
|
|
108
|
+
group1.add_argument('--cratio', nargs='+', type=int, help=help)
|
|
109
|
+
|
|
110
|
+
help = 'PSNR for successive layers.'
|
|
111
|
+
group1.add_argument('--psnr', nargs='+', type=int, help=help)
|
|
112
|
+
|
|
113
|
+
help = 'Codeblock size.'
|
|
114
|
+
group1.add_argument(
|
|
115
|
+
'--codeblocksize', nargs=2, type=int, help=help,
|
|
116
|
+
metavar=('cblkh', 'cblkw')
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
help = 'Number of decomposition levels.'
|
|
120
|
+
group1.add_argument('--numres', type=int, help=help, default=6)
|
|
121
|
+
|
|
122
|
+
help = 'Progression order.'
|
|
123
|
+
choices = ['lrcp', 'rlcp', 'rpcl', 'prcl', 'cprl']
|
|
124
|
+
group1.add_argument('--prog', choices=choices, help=help, default='lrcp')
|
|
125
|
+
|
|
126
|
+
help = 'Use irreversible 9x7 transform.'
|
|
127
|
+
group1.add_argument('--irreversible', help=help, action='store_true')
|
|
128
|
+
|
|
129
|
+
help = 'Generate EPH markers.'
|
|
130
|
+
group1.add_argument('--eph', help=help, action='store_true')
|
|
131
|
+
|
|
132
|
+
help = 'Generate PLT markers.'
|
|
133
|
+
group1.add_argument('--plt', help=help, action='store_true')
|
|
134
|
+
|
|
135
|
+
help = 'Generate SOP markers.'
|
|
136
|
+
group1.add_argument('--sop', help=help, action='store_true')
|
|
137
|
+
|
|
138
|
+
help = 'Use this many threads/cores.'
|
|
139
|
+
group1.add_argument(
|
|
140
|
+
'--num-threads', type=int, default=1, help=help,
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
help = (
|
|
144
|
+
'Dimensions of JP2K tile. If not provided, the JPEG2000 image will '
|
|
145
|
+
'be written as a single tile.'
|
|
146
|
+
)
|
|
147
|
+
group1.add_argument(
|
|
148
|
+
'--tilesize', nargs=2, type=int, help=help, metavar=('NROWS', 'NCOLS')
|
|
149
|
+
)
|
|
150
|
+
|
|
151
|
+
group2 = parser.add_argument_group(
|
|
152
|
+
'JPEG', 'Arguments specific to conversion of JPEG imagery.'
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
help = (
|
|
156
|
+
'If specified, subsume any ICC profile found in an APP2 segment(s) '
|
|
157
|
+
'into the colour specification box. This will involve a file copy '
|
|
158
|
+
'and is therefore a potentially costly operation.'
|
|
159
|
+
)
|
|
160
|
+
group2.add_argument(
|
|
161
|
+
'--include-icc-profile', help=help, action='store_true'
|
|
162
|
+
)
|
|
163
|
+
|
|
164
|
+
group2.add_argument('jpeg', help='Input JPEG file.')
|
|
165
|
+
group2.add_argument('jp2k', help='Output JPEG 2000 file.')
|
|
166
|
+
|
|
167
|
+
# These arguments are not specific to either group.
|
|
168
|
+
help = 'Show this help message and exit'
|
|
169
|
+
parser.add_argument('--help', '-h', action='help', help=help)
|
|
170
|
+
|
|
171
|
+
help = (
|
|
172
|
+
'Logging level, one of "critical", "error", "warning", "info", '
|
|
173
|
+
'or "debug".'
|
|
174
|
+
)
|
|
175
|
+
parser.add_argument(
|
|
176
|
+
'--verbosity', help=help, default='warning',
|
|
177
|
+
choices=['critical', 'error', 'warning', 'info', 'debug']
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
args = parser.parse_args()
|
|
181
|
+
|
|
182
|
+
logging_level = getattr(logging, args.verbosity.upper())
|
|
183
|
+
|
|
184
|
+
jpegp = pathlib.Path(args.jpeg)
|
|
185
|
+
jp2kp = pathlib.Path(args.jp2k)
|
|
186
|
+
|
|
187
|
+
kwargs = {
|
|
188
|
+
'cbsize': args.codeblocksize,
|
|
189
|
+
'cratios': args.cratio,
|
|
190
|
+
'capture_resolution': args.capture_resolution,
|
|
191
|
+
'display_resolution': args.display_resolution,
|
|
192
|
+
'eph': args.eph,
|
|
193
|
+
'include_icc_profile': args.include_icc_profile,
|
|
194
|
+
'irreversible': args.irreversible,
|
|
195
|
+
'numres': args.numres,
|
|
196
|
+
'num_threads': args.num_threads,
|
|
197
|
+
'plt': args.plt,
|
|
198
|
+
'prog': args.prog,
|
|
199
|
+
'psnr': args.psnr,
|
|
200
|
+
'sop': args.sop,
|
|
201
|
+
'tilesize': args.tilesize,
|
|
202
|
+
'verbosity': logging_level,
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
with jpeg.JPEG2JP2(jpegp, jp2kp, **kwargs) as j:
|
|
206
|
+
j.run()
|
|
207
|
+
|
|
208
|
+
|
|
81
209
|
def tiff2jp2():
|
|
82
210
|
"""Entry point for console script tiff2jp2."""
|
|
83
211
|
|
glymur/jp2box.py
CHANGED
|
@@ -33,6 +33,7 @@ except (ImportError, ModuleNotFoundError): # pragma: no cover
|
|
|
33
33
|
_HAVE_GDAL = False
|
|
34
34
|
else:
|
|
35
35
|
gdal.UseExceptions()
|
|
36
|
+
import lxml.objectify
|
|
36
37
|
import lxml.etree as ET
|
|
37
38
|
import numpy as np
|
|
38
39
|
|
|
@@ -3647,36 +3648,58 @@ class UUIDBox(Jp2kBox):
|
|
|
3647
3648
|
lst = [text]
|
|
3648
3649
|
|
|
3649
3650
|
if not get_option("print.xml") and self.uuid == _XMP_UUID:
|
|
3651
|
+
|
|
3650
3652
|
# If it's an XMP UUID, don't print the XML contents.
|
|
3651
3653
|
pass
|
|
3652
3654
|
|
|
3653
3655
|
elif self.uuid == _XMP_UUID:
|
|
3654
|
-
|
|
3655
|
-
s =
|
|
3656
|
-
|
|
3656
|
+
|
|
3657
|
+
s = self.raw_data.decode('utf-8').rstrip('\0')
|
|
3658
|
+
e = lxml.objectify.fromstring(s)
|
|
3659
|
+
xml = ET.tostring(e, pretty_print=True).decode('utf-8').strip()
|
|
3660
|
+
text = f"UUID Data:\n{xml}"
|
|
3657
3661
|
lst.append(text)
|
|
3662
|
+
|
|
3658
3663
|
elif self.uuid == _EXIF_UUID:
|
|
3664
|
+
|
|
3659
3665
|
s = io.StringIO()
|
|
3660
3666
|
|
|
3661
3667
|
if self.data is None:
|
|
3662
3668
|
# If the UUID was malformed, just say so and go on. This
|
|
3663
3669
|
# should not be a showstopper.
|
|
3664
3670
|
text = "UUID Data: Invalid Exif UUID"
|
|
3665
|
-
lst.append(text)
|
|
3666
3671
|
else:
|
|
3667
3672
|
with np.printoptions(threshold=4):
|
|
3668
3673
|
pprint.pprint(self.data, stream=s, indent=4)
|
|
3669
3674
|
text = f"UUID Data: {s.getvalue().rstrip()}"
|
|
3670
|
-
|
|
3675
|
+
|
|
3676
|
+
lst.append(text)
|
|
3677
|
+
|
|
3671
3678
|
elif self.uuid == _GEOTIFF_UUID:
|
|
3672
3679
|
|
|
3673
3680
|
options = gdal.InfoOptions(showColorTable=False)
|
|
3674
|
-
|
|
3675
|
-
|
|
3681
|
+
gdal_txt = gdal.Info(self._fptr.name, options=options)
|
|
3682
|
+
gdal_txt = textwrap.indent(gdal_txt, " " * 4).rstrip()
|
|
3683
|
+
|
|
3684
|
+
# now append the raw IFD
|
|
3685
|
+
s = io.StringIO()
|
|
3686
|
+
with np.printoptions(threshold=4):
|
|
3687
|
+
pprint.pprint(self.data, stream=s, indent=4)
|
|
3688
|
+
|
|
3689
|
+
ifd_txt = s.getvalue().rstrip()
|
|
3690
|
+
|
|
3691
|
+
txt = (
|
|
3692
|
+
f'UUID Data:'
|
|
3693
|
+
f'\n\n'
|
|
3694
|
+
f'Geo Metadata:\n{gdal_txt}'
|
|
3695
|
+
f'\n\n'
|
|
3696
|
+
f'Raw IFD Metadata:\n{ifd_txt}'
|
|
3697
|
+
)
|
|
3676
3698
|
|
|
3677
|
-
txt = f"UUID Data:\n{txt}"
|
|
3678
3699
|
lst.append(txt)
|
|
3700
|
+
|
|
3679
3701
|
else:
|
|
3702
|
+
|
|
3680
3703
|
text = f"UUID Data: {len(self.raw_data)} bytes"
|
|
3681
3704
|
lst.append(text)
|
|
3682
3705
|
|