pyguymer3.image.makePng

pyguymer3.image.makePng(arrUint8, /, *, calcAdaptive: bool = True, calcAverage: bool = True, calcNone: bool = True, calcPaeth: bool = True, calcSub: bool = True, calcUp: bool = True, choices: str = 'all', debug: bool = True, dpi: None | int = None, levels: None | list[int] = None, memLevels: None | list[int] = None, modTime=None, palUint8=None, strategies: None | list[int] = None, wbitss: None | list[int] = None) bytearray[source]

Make a PNG

This function reads in a “height * width * colour” unsigned 8-bit integer NumPy array and returns the Python bytearray which is the binary source of the PNG file of the input.

By default, this function calculates the PNG image data stream using all five filters (as defined in the PNG specification [2]), as well as adaptive filtering, and this function then uses the one filter which ends up having the smallest compressed size.

This function also allows the user to declare sets of settings which are tried when compressing the PNG image data stream and this function then uses the one set of settings which ends up having the smallest compressed size. This allows the user to: either choose to spend CPU time trying different sets of settings in an attempt to find the one which produces the smallest compressed size for the supplied input; or try a stab in the dark at knowing the fastest (or best) set of settings.

Parameters:
  • arrUint8 (numpy.ndarray) – A “height * width * colour” unsigned 8-bit integer NumPy array.

  • calcAdaptive (bool, optional) – Calculate the compressed PNG image data stream using an adaptive filter type. Each of the five named filters is applied to a scanline and a prediction is made as to which one will produce the smallest compressed scanline. The chosen filtered uncompressed scanline is concatenated with all of the other filtered uncompressed scanlines and a single compression operation is performed once the whole image has been processed.

  • calcAverage (bool, optional) – Calculate the compressed PNG image data stream using the “average” filter type, as defined in the PNG specification [2].

  • calcNone (bool, optional) – Calculate the compressed PNG image data stream using the “none” filter type, as defined in the PNG specification [2].

  • calcPaeth (bool, optional) – Calculate the compressed PNG image data stream using the “Paeth” filter type, as defined in the PNG specification [2].

  • calcSub (bool, optional) – Calculate the compressed PNG image data stream using the “sub” filter type, as defined in the PNG specification [2].

  • calcUp (bool, optional) – Calculate the compressed PNG image data stream using the “up” filter type, as defined in the PNG specification [2].

  • choices (str, optional) – If any of the settings are not passed (or passed as None) then this string is used to set them. The accepted values are "fastest", "best" and "all".

  • debug (bool, optional) – Print debug messages.

  • dpi (None or float or int, optional) – If a number is passed then the ancillary “pHYs” chunk will get created and the resolution will be specified.

  • levels (None or list of int, optional) –

    The list of compression levels to loop over when trying to find the smallest compressed size. If not supplied, or None, then the value of choices will determine the value of levels.

    If levels is None and choices == "fastest" then levels = [0,].

    If levels is None and choices == "best" then levels = [9,].

    If levels is None and choices == "all" then levels = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9,].

    See zlib.compressobj() for what the valid compression levels are.

  • memLevels (None or list of int, optional) –

    The list of memory levels to loop over when trying to find the smallest compressed size. If not supplied, or None, then the value of choices will determine the value of memLevels.

    If memLevels is None and choices == "fastest" then memLevels = [9, ].

    If memLevels is None and choices == "best" then memLevels = [9,] .

    If memLevels is None and choices == "all" then memLevels = [1, 2, 3, 4, 5, 6, 7, 8, 9,].

    See zlib.compressobj() for what the valid memory levels are.

  • modTime (None or datetime.datetime, optional) – If a time is passed then the ancillary “tIME” chunk will get created and the image last-modification time will be specified.

  • palUint8 (None or numpy.ndarray, optional) – A “level * colour” unsigned 8-bit integer NumPy array. If the size of the “colours” axis in arrUint8 is 1 then arrUint8 is assumed to be either greyscale (if palUint8 is None) or paletted and palUint8 is the palette.

  • strategies (None or list of int, optional) –

    The list of strategies to loop over when trying to find the smallest compressed size. If not supplied, or None, then the value of choices will determine the value of strategies.

    If strategies is None and choices == "fastest" then strategies = [ zlib.Z_DEFAULT_STRATEGY,].

    If strategies is None and choices == "best" then strategies = [ zlib.Z_DEFAULT_STRATEGY,].

    If strategies is None and choices == "all" then strategies = [ zlib.Z_DEFAULT_STRATEGY, zlib.Z_FILTERED, zlib.Z_HUFFMAN_ONLY, zlib.Z_RLE, zlib.Z_FIXED,].

    See zlib.compressobj() for what the valid strategies are.

  • wbitss (None or list of int, optional) –

    The list of window sizes to loop over when trying to find the smallest compressed size. If not supplied, or None, then the value of choices will determine the value of wbitss.

    If wbitss is None and choices == "fastest" then wbitss = [15,].

    If wbitss is None and choices == "best" then wbitss = [15,].

    If wbitss is None and choices == "all" then wbitss = [9, 10, 11, 12, 13, 14, 15,].

    See zlib.compressobj() for what the valid window sizes are.

Returns:

src – The binary source of the PNG file of the input.

Return type:

bytearray

Notes

This function only creates 8-bit images (either greyscale, paletted or truecolour), without interlacing. It stores the entire image in a single “IDAT” chunk.

This function always writes out three of the four critical chunks: “IHDR”, “IDAT” and “IEND”. Depending on optional keyword arguments which may be provided then this function may also write out the critical chunk “PLTE” as well as the ancillary chunks “iTIM” and “pHYs”.

Copyright 2017 Thomas Guymer [1]

References