Scripton / Docs

Image

The multi-purpose show function supports displaying images. For images, the concrete parameters are:

def show( # The image to display image: PIL.Image.Image | numpy.ndarray, # An optional title displayed above the image title: str | None = None, # When image is a numpy array, whether its channels are # ordered BGR(A). By default, RGB(A) is assumed. # See section on OpenCV for when this is relevant. bgr = False ): ...

In its simplest form, just invoke show with an image:

from scripton import show from PIL import Image # Load an image using PIL image = Image.open('landsat.jpg') # Display the image show(image)

Supported Formats

The show function automatically determines how to display the given argument based on its type and attributes. The following types are supported and interpreted as images:

  • Instances PIL.Image.Image as shown in the example above.

  • 3D NumPy uint8 arrays where the final dimension is either 3 (interpreted as RGB channel ordering) or 4 (interpreted as RGBA)

    from scripton import show from imageio import v3 as iio img = iio.imread('flower.png') show( img, title=f'Flower / shape: {img.shape} / dtype: {img.dtype}' )

  • For displaying single channel grayscale images stored as numpy matrices, the easiest option is to treat it as a colormapped matrix with a grayscale colormap.

OpenCV Support

OpenCV's imread functions return regular numpy arrays which are supported by Scripton's show function as described above. However, an important deviation here is that OpenCV uses the BGR channel ordering by default (in contrast to the RGB ordering assumed by show).

There are two ways to deal with this BGR vs RGB issue. First, Scripton directly supports OpenCV's imshow function which assumes BGR ordering by default. So, the following works as expected:

img = cv2.imread('cameraman.jpg') cv2.imshow('Cameraman', img)

Alternatively, the show function accepts an optional bgr boolean argument that can be set to True:

img = cv2.imread('cameraman.jpg') show( img, bgr=True )