Image Rendering Script (Python)
In this article:
We recommend reading Scripting in Application (Python) first to learn the basics of scripting.
Running the same script, Python is roughly 10 to 20 times slower than C#, but 5 to 10 times faster than JavaScript. The script runs again every time the image needs to be rendered, so please consider C# for a script which processes a large image.
Rendering Script
There is only one script in each image rendering script, and it will be invoked one time every time when the image needs to be rendered. The script reads raw pixel data from context.InputStream and writes BGRA pixels into context.OutputStream. The type of returned value is ImageRenderingResult. Return ImageRenderingResult.Default after rendering the image. The following is sample script to render an image with single plane of 8-bit luminance to Bgra32 output color format:planeOptions = context.PlaneOptions[0]
sourceRow = context.CreateByteArray(planeOptions.RowStride)
outputRow = context.CreateByteArray(context.OutputRowBytes)
for y in range(context.OutputHeight):
context.InputStream.ReadExactly(sourceRow, 0, len(sourceRow))
for x in range(context.OutputWidth):
luminance = sourceRow[x * planeOptions.PixelStride]
outputRow[x * 4] = luminance
outputRow[x * 4 + 1] = luminance
outputRow[x * 4 + 2] = luminance
outputRow[x * 4 + 3] = 255
context.OutputStream.Write(outputRow, 0, len(outputRow))
return ImageRenderingResult.Default
The offset to the image data in the file has already been applied to context.InputStream, so the script can read raw pixel data from the beginning of the stream directly.
Samples
The following is sample script to render an image of NV21 (YUV 4:2:0, semi-planar, V before U) format into Bgra32 output color format: You can import the downloaded file through the dialog of managing image rendering scripts.Context
The following are members of Context other than basic members:-
CreateByteArray(count: int): Array[byte]
Create an array of bytes for bulk reading from InputStream or writing to OutputStream.
-
InputStream: System.IO.Stream
Stream to read source image data. The offset to the image data has already been applied.
-
OutputFormat: BitmapFormat
Format of the output bitmap buffer. The following are values defined in BitmapFormat:
- Bgra32
- Bgra64
-
OutputHeight: int
Height of the output bitmap buffer in pixels.
-
OutputRowBytes: int
Number of bytes of each row of the output bitmap buffer.
-
OutputStream: System.IO.Stream
Stream to write rendered image into the output bitmap buffer. The stream is valid only during the rendering call.
-
OutputWidth: int
Width of the output bitmap buffer in pixels.
-
PlaneOptions: IList<ImagePlaneOptions>
Options of each plane of the image. The following are members of ImagePlaneOptions:
-
BlackLevel: System.Nullable<uint>
Black level of the plane, or Null if black level is not defined.
-
EffectiveBits: int
Effective bits of each pixel of the plane.
-
PixelStride: int
Number of bytes of each pixel of the plane.
-
RowStride: int
Number of bytes of each row of the plane.
-
WhiteLevel: System.Nullable<uint>
White level of the plane, or Null if white level is not defined.
-
BlackLevel: System.Nullable<uint>
-
RenderingOptions: ImageRenderingOptions
Options of rendering the image. The following are members of ImageRenderingOptions:
- BlueGain: float
-
ByteOrdering: ByteOrdering
Byte ordering of source image data. The following are values defined in ByteOrdering:
- BigEndian
- LittleEndian
- GreenGain: float
- RedGain: float
Interacting with user is not supported inside an image rendering script, because the script runs on a background thread every time when the image needs to be rendered.