Multi-image support#
[1]:
import os
import numpy as np
import cupy as cp
from matplotlib import pyplot as plt
Setting resource folder
[2]:
resources_dir = os.getenv("PYNVIMGCODEC_EXAMPLES_RESOURCES_DIR", "../assets/images/")
Import nvImageCodec module and create Decoder
[3]:
from nvidia import nvimgcodec
decoder = nvimgcodec.Decoder()
Create CodeStream from tiff file
[4]:
img_full_path = os.path.abspath(os.path.join(resources_dir, "multi_page.tif"))
cs = nvimgcodec.CodeStream(img_full_path)
print(f"Number of tiles (Y): {cs.num_tiles_y}, Number of tiles (X): {cs.num_tiles_x}")
print(f"Tile height: {cs.tile_height}, Tile width: {cs.tile_width}")
print(f"Number of images: {cs.num_images}")
print(f"Image (0) height: {cs.height}, width: {cs.width}, number of channels: {cs.channels}, sample type: {cs.dtype}")
Number of tiles (Y): 5, Number of tiles (X): 1
Tile height: 106, Tile width: 640
Number of images: 7
Image (0) height: 426, width: 640, number of channels: 3, sample type: uint8
Get sub code stream for first image (index 0) and decode it
[5]:
cs_0 = cs.getSubCodeStream(0)
print(f"Number of tiles (Y): {cs_0.num_tiles_y}, Number of tiles (X): {cs_0.num_tiles_x}")
print(f"Tile height: {cs_0.tile_height}, Tile width: {cs_0.tile_width}")
print(f"Image (0) height: {cs_0.height}, width: {cs_0.width}, number of channels: {cs_0.channels}, sample type:{cs_0.dtype}")
img_0 = decoder.decode(cs_0, nvimgcodec.DecodeParams(allow_any_depth=True, color_spec=nvimgcodec.ColorSpec.UNCHANGED))
plt.imshow(img_0.cpu())
Number of tiles (Y): 5, Number of tiles (X): 1
Tile height: 106, Tile width: 640
Image (0) height: 426, width: 640, number of channels: 3, sample type:uint8
[5]:
<matplotlib.image.AxesImage at 0x7f86d7837c40>

Get sub code stream for image with index 2 and decode it
[6]:
cs_2 = cs.getSubCodeStream(2)
print(f"Number of tiles (Y): {cs_2.num_tiles_y}, Number of tiles (X): {cs_2.num_tiles_x}")
print(f"Tile height: {cs_2.tile_height}, Tile width: {cs_2.tile_width}")
print(f"Image (2) height: {cs_2.height}, width: {cs_2.width}, number of channels: {cs_2.channels}, sample type:{cs_2.dtype}")
img_2 = decoder.decode(cs_2, nvimgcodec.DecodeParams(allow_any_depth=True, color_spec=nvimgcodec.ColorSpec.UNCHANGED))
plt.imshow(img_2.cpu())
Number of tiles (Y): 5, Number of tiles (X): 1
Tile height: 106, Tile width: 640
Image (2) height: 426, width: 640, number of channels: 3, sample type:uint8
[6]:
<matplotlib.image.AxesImage at 0x7f86d5055880>

Get sub code streams for all images in the loop and decode them one by one
[7]:
for code_stream_idx in range(0, cs.num_images):
sub_code_stream = cs.getSubCodeStream(code_stream_idx)
img = decoder.decode(sub_code_stream, nvimgcodec.DecodeParams(allow_any_depth=True, color_spec=nvimgcodec.ColorSpec.UNCHANGED))
print(img.__cuda_array_interface__)
print ("precision", img.precision)
plt.figure()
img_h = img.cpu()
if img.precision == 16:
img_h = (np.asarray(img_h) / 65535.0 * 255).astype(np.uint8)
plt.imshow(img_h, cmap=('gray' if img_h.shape[2] == 1 else None))
{'shape': (426, 640, 3), 'strides': None, 'typestr': '|u1', 'data': (12920092672, False), 'version': 3, 'stream': 1}
precision 8
{'shape': (426, 640, 3), 'strides': None, 'typestr': '<u2', 'data': (12920910848, False), 'version': 3, 'stream': 1}
precision 16
{'shape': (426, 640, 3), 'strides': None, 'typestr': '|u1', 'data': (12920092672, False), 'version': 3, 'stream': 1}
precision 8
{'shape': (213, 200, 3), 'strides': None, 'typestr': '|u1', 'data': (12920910848, False), 'version': 3, 'stream': 1}
precision 8
{'shape': (536, 640, 3), 'strides': None, 'typestr': '<u2', 'data': (12921038848, False), 'version': 3, 'stream': 1}
precision 16
{'shape': (475, 640, 3), 'strides': None, 'typestr': '|u1', 'data': (12920092672, False), 'version': 3, 'stream': 1}
precision 8
{'shape': (497, 497, 4), 'strides': None, 'typestr': '|u1', 'data': (12921005056, False), 'version': 3, 'stream': 1}
precision 8







Get sub code streams for all images in the loop and decode them in batch
[8]:
sub_code_streams = []
for code_stream_idx in range(0, cs.num_images):
sub_code_streams.append(cs.getSubCodeStream(code_stream_idx))
imgs = decoder.decode(sub_code_streams, nvimgcodec.DecodeParams(allow_any_depth=True, color_spec=nvimgcodec.ColorSpec.UNCHANGED))
for img in imgs:
plt.figure()
img_h = img.cpu()
if img.precision == 16:
img_h = (np.asarray(img_h) / 65535.0 * 255).astype(np.uint8)
plt.imshow(img_h, cmap=('gray' if img_h.shape[2] == 1 else None))







Decode region from particular image
[9]:
cs_6 = cs.getSubCodeStream(5, nvimgcodec.Region(100, 175, 300, 400))
print(f"Number of tiles (Y): {cs_6.num_tiles_y}, Number of tiles (X): {cs_6.num_tiles_x}")
print(f"Tile height: {cs_6.tile_height}, Tile width: {cs_6.tile_width}")
print(f"Image (8) height: {cs_6.height}, width: {cs_6.width}, number of channels: {cs_6.channels}, sample type: {cs_6.dtype}")
img_6 = decoder.decode(cs_6, nvimgcodec.DecodeParams(allow_any_depth=True, color_spec=nvimgcodec.ColorSpec.UNCHANGED))
img_h = img_6.cpu()
if img_6.precision == 16:
img_h = (np.asarray(img_h) / 65535.0 * 255).astype(np.uint8)
plt.imshow(img_h, cmap=('gray' if img_h.shape[2] == 1 else None))
Number of tiles (Y): 5, Number of tiles (X): 1
Tile height: 118, Tile width: 640
Image (8) height: 475, width: 640, number of channels: 3, sample type: uint8
[9]:
<matplotlib.image.AxesImage at 0x7f8624075040>
