I/O
hdrconv.io
HDR format I/O operations.
This module provides functions for reading and writing various HDR formats:
- ISO 21496-1 (Adaptive Gainmap):
read_21496,write_21496 - ISO 22028-5 (PQ/HLG AVIF):
read_22028_pq,write_22028_pq - Apple HEIC with gainmap:
read_apple_heic - iOS HDR screenshot:
read_ios_hdr_screenshot
JPEG building blocks (encode a plain JPEG and embed ICC/APPn segments
without PIL): encode_jpeg, build_icc_segments, insert_segments.
build_icc_segments(icc)
Split an ICC profile into chunked APP2 segments (ICC.1 Annex B).
Returns an empty list when icc is None or empty, so results can be
spliced unconditionally into a segment list.
Source code in src/hdrconv/io/_jpeg.py
encode_jpeg(img_arr, quality=95)
Encode a numpy array as baseline JPEG with 4:4:4 chroma subsampling.
Accepts uint8, float ([0, 1] range) or deeper integer arrays (rescaled
from the dtype's full range — use normalize_to_uint8 first when the
true bit depth is known), shaped (H, W), (H, W, 1), (H, W, 3) or
(H, W, 4); single-channel input becomes grayscale JPEG and an alpha
channel is dropped. No metadata segments are embedded — callers add
ICC/MPF/XMP segments via insert_segments.
Source code in src/hdrconv/io/_jpeg.py
insert_segments(jpeg, segments)
Insert prebuilt marker segments into a JPEG stream's header area.
read_21496(filepath)
Read an ISO 21496-1 Gainmap image.
Routes JPEG files to the MPF parser and HEIF/AVIF files to the ISOBMFF parser.
Source code in src/hdrconv/io/iso21496.py
write_21496(data, filepath, baseline_quality=95, gainmap_quality=95)
Write ISO 21496-1 Gainmap JPEG file.
Creates a JPEG file with ISO 21496-1 compliant gainmap structure using Multi-Picture Format (MPF) container.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
GainmapImage
|
GainmapImage dict containing:
- |
required |
filepath
|
str
|
Output path for the JPEG file. |
required |
baseline_quality
|
int
|
JPEG quality for baseline image (1-100, default 95). |
95
|
gainmap_quality
|
int
|
JPEG quality for gainmap image (1-100, default 95). |
95
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If file writing fails. |
Note
The output file structure places the baseline image first with an MPF index, followed by the gainmap with ISO 21496-1 metadata. JPEG quality is set to 95 with 4:4:4 chroma subsampling.
See Also
read_21496: Read ISO 21496-1 Gainmap JPEG.hdr_to_gainmap: Convert HDR image to GainmapImage.
Source code in src/hdrconv/io/iso21496.py
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 | |
read_22028_pq(filepath)
Read ISO 22028-5 PQ AVIF file.
Decodes an AVIF file encoded with Perceptual Quantizer (PQ) transfer function as specified in ISO 22028-5 and SMPTE ST 2084.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
Path to the PQ AVIF file. |
required |
Returns:
| Type | Description |
|---|---|
HDRImage
|
HDRImage dict containing: |
HDRImage
|
|
HDRImage
|
|
HDRImage
|
|
HDRImage
|
|
Note
Sample values are normalized using the actual coded bit depth (8, 10, or 12), parsed from the file's av1C box. If the depth cannot be determined for 16-bit samples, 10-bit is assumed.
See Also
write_22028_pq: Write HDR image to PQ AVIF format.colour.eotf(data, 'ITU-R BT.2100 PQ'): Convert PQ-encoded data to linear light (seeexamples/pq_to_gainmap.py).
Source code in src/hdrconv/io/iso22028.py
write_22028_pq(data, filepath)
Write ISO 22028-5 PQ AVIF file.
Encodes an HDR image to AVIF format with Perceptual Quantizer (PQ) transfer function as specified in ISO 22028-5 and SMPTE ST 2084.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
HDRImage
|
HDRImage dict with PQ-encoded data. Must contain:
- |
required |
filepath
|
str
|
Output path for the AVIF file. |
required |
Note
Output is encoded at 10-bit depth with quality level 90. Color primaries and transfer characteristics are embedded in AVIF metadata.
See Also
read_22028_pq: Read PQ AVIF file.colour.eotf_inverse(data, 'ITU-R BT.2100 PQ'): Convert linear HDR to PQ-encoded values (seeexamples/gainmap_to_pq.py).
Source code in src/hdrconv/io/iso22028.py
read_apple_heic(filepath)
Read Apple HEIC HDR file with gain map.
Extracts the base SDR image, HDR gain map, and headroom metadata from iPhone HEIC photos containing Apple's proprietary HDR format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
Path to the Apple HEIC file. |
required |
Returns:
| Type | Description |
|---|---|
AppleHeicData
|
AppleHeicData dict containing: |
AppleHeicData
|
|
AppleHeicData
|
|
AppleHeicData
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If base image, gainmap, or headroom cannot be extracted. |
Note
Requires exiftool to be installed and accessible in PATH for headroom extraction from EXIF/MakerNotes metadata.
See Also
apple_heic_to_hdr: Convert AppleHeicData to linear HDR.has_gain_map: Check if HEIC file contains gain map.
Source code in src/hdrconv/io/apple_heic.py
read_ios_hdr_screenshot(filepath, grid_cols=None, grid_rows=None, tile_size=512, real_width=None, real_height=None)
Read iOS HDR screenshot HEIC file.
Extracts the main image, gainmap, and metadata from iOS HDR screenshots
and returns a standard GainmapImage structure suitable for use with
gainmap_to_hdr.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
Path to the iOS HDR screenshot HEIC file. |
required |
grid_cols
|
Optional[int]
|
Number of tile columns (auto-detected if None). |
None
|
grid_rows
|
Optional[int]
|
Number of tile rows (auto-detected if None). |
None
|
tile_size
|
int
|
Size of each square tile in pixels. Default: 512. |
512
|
real_width
|
Optional[int]
|
Actual image width (auto-detected if None). |
None
|
real_height
|
Optional[int]
|
Actual image height (auto-detected if None). |
None
|
Returns:
| Type | Description |
|---|---|
GainmapImage
|
GainmapImage dict containing: |
GainmapImage
|
|
GainmapImage
|
|
GainmapImage
|
|
GainmapImage
|
|
GainmapImage
|
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If external tools (MP4Box, ffmpeg) are not available. |
ValueError
|
If the file cannot be parsed or is not a valid iOS HDR screenshot. |
FileNotFoundError
|
If the input file does not exist. |
Note
Requires MP4Box (from GPAC) and ffmpeg to be installed and available in PATH.
The gainmap_min is always 0 and gainmap_gamma is always 1 for iOS HDR screenshots. Both baseline_offset and alternate_offset are set to the same value extracted from the tmap metadata.
See Also
gainmap_to_hdr: Convert the returned GainmapImage to linear HDR.
Source code in src/hdrconv/io/ios_hdr_screenshot.py
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 | |
read_ultrahdr(filepath)
Read UltraHDR JPEG file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
Path to the UltraHDR JPEG file. |
required |
Returns:
| Type | Description |
|---|---|
GainmapImage
|
GainmapImage dict containing baseline, gainmap, metadata, and ICC data. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If gainmap stream or HDR gainmap metadata is missing. |
Source code in src/hdrconv/io/ultrahdr.py
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 | |
write_ultrahdr(data, filepath, baseline_quality=95, gainmap_quality=95)
Write UltraHDR JPEG file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
GainmapImage
|
GainmapImage dict containing baseline, gainmap, and metadata. |
required |
filepath
|
str
|
Output path for the JPEG file. |
required |
baseline_quality
|
int
|
JPEG quality for baseline image (1-100, default 95). |
95
|
gainmap_quality
|
int
|
JPEG quality for gainmap image (1-100, default 95). |
95
|