PNG reader / writer

The flexx.util.png module is a standalone pure Python module for reading/writing PNG images. The code is restricted to “simple” variants of png.

flexx.util.png.read_png(f, return_ndarray=False)

Read a png image. This is a simple implementation; can only read PNG’s that are not interlaced, have a bit depth of 8, and are either RGB or RGBA.

  • f (file-object, bytes) – the source to read the png data from.
  • return_ndarray (bool) – whether to return the result as a numpy array. Default False. If False, returns (pixel_array, shape), with pixel_array a bytearray object and shape being (N, M, 3) or (N, M, 4), for RGB and RGBA, respectively.
flexx.util.png.write_png(im, shape=None, file=None)

Write a png image. The written image is in RGB or RGBA format, with 8 bit precision, and without interlacing.

  • im (bytes, bytearray, numpy-array) – the image data to write.
  • shape (tuple) – the shape of the image. If im is a numpy array, the shape can be omitted. The shape can be (N, M) for grayscale, (N, M, 3) for RGB and (N, M, 4) for RGBA. Note that grayscale images are converted to RGB.
  • file (file-like object, None) – where to write the resulting image. If omitted or None, the result is returned as bytes.