File: TestImageWriters.py

package info (click to toggle)
paraview 5.13.2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 544,220 kB
  • sloc: cpp: 3,374,605; ansic: 1,332,409; python: 150,381; xml: 122,166; sql: 65,887; sh: 7,317; javascript: 5,262; yacc: 4,417; java: 3,977; perl: 2,363; lex: 1,929; f90: 1,397; makefile: 170; objc: 153; tcl: 59; pascal: 50; fortran: 29
file content (106 lines) | stat: -rwxr-xr-x 3,403 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python
import os
from vtkmodules.vtkCommonDataModel import vtkStructuredPoints
from vtkmodules.vtkIOImage import (
    vtkBMPWriter,
    vtkJPEGWriter,
    vtkPNGWriter,
    vtkPNMWriter,
    vtkPostScriptWriter,
    vtkTIFFReader,
    vtkTIFFWriter,
)
from vtkmodules.vtkImagingColor import vtkImageLuminance
from vtkmodules.vtkInteractionImage import vtkImageViewer
import vtkmodules.vtkInteractionStyle
import vtkmodules.vtkRenderingFreeType
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.util.misc import vtkGetDataRoot
VTK_DATA_ROOT = vtkGetDataRoot()

# Image pipeline
image1 = vtkTIFFReader()
image1.SetFileName(VTK_DATA_ROOT + "/Data/beach.tif")
# "beach.tif" image contains ORIENTATION tag which is
# ORIENTATION_TOPLEFT (row 0 top, col 0 lhs) type. The TIFF
# reader parses this tag and sets the internal TIFF image
# orientation accordingly.  To overwrite this orientation with a vtk
# convention of ORIENTATION_BOTLEFT (row 0 bottom, col 0 lhs ), invoke
# SetOrientationType method with parameter value of 4.
image1.SetOrientationType(4)
image1.Update()

sp = vtkStructuredPoints()
sp.SetDimensions(image1.GetOutput().GetDimensions())
sp.SetExtent(image1.GetOutput().GetExtent())
sp.SetScalarType(
  image1.GetOutput().GetScalarType(), image1.GetOutputInformation(0))
sp.SetNumberOfScalarComponents(
  image1.GetOutput().GetNumberOfScalarComponents(),
  image1.GetOutputInformation(0))
sp.GetPointData().SetScalars(image1.GetOutput().GetPointData().GetScalars())

luminance = vtkImageLuminance()
luminance.SetInputData(sp)

# Let's create a dictionary to test the writers, the key will be the writer
# and the value the file name used by the writer.
filenames = ["tiff1.tif", "tiff2.tif", "bmp1.bmp", "bmp2.bmp",
  "pnm1.pnm", "pnm2.pnm", "psw1.ps", "psw2.ps",
  "pngw1.png", "pngw2.png", "jpgw1.jpg", "jpgw2.jpg"]
writerObjects = list()
writerObjectTypes = ["vtkTIFFWriter()", "vtkBMPWriter()",
  "vtkPNMWriter()", "vtkPostScriptWriter()",
  "vtkPNGWriter()", "vtkJPEGWriter()" ]
idx = 0
for fn in filenames:
    # Create the writer object
    exec(fn.split(".")[0] + " = " + writerObjectTypes[int(idx / 2)])
    # Append the writer object to the list called writerObjects
    writerObjects.append(eval(fn.split(".")[0]))
    idx += 1

# Now create the dictionary.
writers = dict()
for idx in range(len(writerObjects)):
    writers.update({writerObjects[idx]: filenames[idx]})

#
# If the current directory is writable, then test the writers
#
try:
    for writer in writers:
        # The file name
        fn = writers[writer]
        # Use the file name to determine whether we are working
        # with the image or luninance.
        il = int(fn.split(".")[0][-1:])

        # Can we write to the directory?
        channel = open(fn, "wb")
        channel.close()

        if il == 1:
            writer.SetInputConnection(image1.GetOutputPort())
        elif il == 2:
            writer.SetInputConnection(luminance.GetOutputPort())
        else:
            continue
        writer.SetFileName(fn)
        writer.Write()

        # cleanup
        #
        try:
            os.remove(fn)
        except OSError:
            pass

    viewer = vtkImageViewer()
    viewer.SetInputConnection(luminance.GetOutputPort())
    viewer.SetColorWindow(255)
    viewer.SetColorLevel(127.5)
    viewer.Render()

except IOError:
    print("Unable to test the writers.")