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.
1import iolink
2import numpy as np
3
4
6
7
8def 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
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
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
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
36def copy_imageview(imageSrc: iolink.ImageView, imageDst:iolink.ImageView):
37
38
39 if not imageSrc.support(iolink.ImageCapability.READ):
40 raise AttributeError("Source image does not have READ capability")
41
42
43 if not imageDst.support(iolink.ImageCapability.READ):
44 raise AttributeError("Destination image does not have WRITE and RESHAPE capability")
45
46
47 imageDst.reshape(imageSrc.shape, imageSrc.data_type)
48
49
50 imageDst.properties = imageSrc.properties
51
52
53 if imageSrc.metadata is not None:
54 imageDst.metadata = imageSrc.metadata.clone()
55
56
57
58
59 fullRegion = iolink.RegionXu64.create_full_region(imageSrc.shape)
60
61 npBuf = np.zeros(dtype = 'B', shape=(imageSrc.data_type.byte_count() * fullRegion.element_count))
62
63 imageSrc.read_region(fullRegion, npBuf)
64 imageDst.write_region(fullRegion, npBuf)
65
66
67imageSource = create_imageview()
68
69
70imageDestination = iolink.ImageViewFactory.allocate((1, 1), iolink.DataTypeId.FLOAT)
71
72
73copy_imageview(imageSource, imageDestination)
74
75print(imageDestination)
76print(imageDestination.properties)
77print(imageDestination.metadata)
78
79print("SUCCESS")