| IOLink Python 1.11.0

Loading...
Searching...
No Matches
copyImageView.py

This code demonstrates how it is possible to copy a whole ImageView content into another one.

This code demonstrates how it is possible to copy a whole ImageView content into another one.

1 import iolink
2 import numpy as np
3
4
6
7
8 def create_imageview() -> iolink.ImageView:
9 shape = iolink.VectorXu64(10, 20, 30)
10 dt = iolink.DataTypeId.VEC3_UINT8
11 image = iolink.ImageViewFactory.allocate(shape, dt)
12
13 # set some properties (not exhaustive)
14 image.axes_interpretation = iolink.AxesInterpretationId.IMAGE_SEQUENCE
15 image.image_interpretation = iolink.ImageInterpretation.RGB
16 image.has_alpha = False
17 image.spatial_origin = iolink.Vector3d(1.0, 2.0, 3.0)
18 image.spatial_spacing = iolink.Vector3d(0.1, 0.2, 0.3)
19 image.spatial_unit = "mm"
20
21 # create some metadata
22 rootNode = iolink.MetadataNodeFactory.create("root", None)
23 iolink.MetadataNodeHelper.create_path(rootNode, "level1/data1", iolink.VariantDataValueFactory.create("value1"))
24 iolink.MetadataNodeHelper.create_path(rootNode, "level1/data2", iolink.VariantDataValueFactory.create("value2"))
25 iolink.MetadataNodeHelper.create_path(rootNode, "level2/data3", iolink.VariantDataValueFactory.create("value3"))
26 image.metadata = rootNode
27
28 # write some data (anything)
29 fullRegion = iolink.RegionXu64.create_full_region(shape)
30 npBuf = np.ones(dtype = 'B', shape=(fullRegion.element_count * image.data_type.byte_count()))
31 image.write_region(fullRegion, npBuf)
32
33 return image
34
35
36 def copy_imageview(imageSrc: iolink.ImageView, imageDst:iolink.ImageView):
37 # when source image origin is not controlled, we should check if we have READ capability
38 # this test is not mandatory, but it is a good practice to check if we can read the source image
39 if not imageSrc.support(iolink.ImageCapability.READ):
40 raise AttributeError("Source image does not have READ capability")
41
42 # check if destination image has WRITE and RESHAPE capability
43 if not imageDst.support(iolink.ImageCapability.READ):
44 raise AttributeError("Destination image does not have WRITE and RESHAPE capability")
45
46 # destination image is reshaped to the same shape and data type as the source image
47 imageDst.reshape(imageSrc.shape, imageSrc.data_type)
48
49 # properties are copied from source to destination
50 imageDst.properties = imageSrc.properties
51
52 # metadata are copied from source to destination
53 if imageSrc.metadata is not None:
54 imageDst.metadata = imageSrc.metadata.clone()
55
56 # copy pixels/voxels one shot
57 # this is the fastest way to copy the whole image but it can require a lot of memory
58 # a copy chunk by chunk could be more memory efficient
59 fullRegion = iolink.RegionXu64.create_full_region(imageSrc.shape)
60 # dimension a buffer to store the whole image temporarily
61 npBuf = np.zeros(dtype = 'B', shape=(imageSrc.data_type.byte_count() * fullRegion.element_count))
62 # read/write pixels/voxels
63 imageSrc.read_region(fullRegion, npBuf)
64 imageDst.write_region(fullRegion, npBuf)
65
66# create an ImageView with some content
67imageSource = create_imageview()
68
69# create an ImageView with any shape and data type
70imageDestination = iolink.ImageViewFactory.allocate((1, 1), iolink.DataTypeId.FLOAT)
71
72# copy the content of the source image into the destination image
73copy_imageview(imageSource, imageDestination)
74
75print(imageDestination)
76print(imageDestination.properties)
77print(imageDestination.metadata)
78
79print("SUCCESS")