Skip to content
Sandro Turriate edited this page Nov 1, 2022 · 2 revisions

#Constraints

Example

{:compression => Zlib::BEST_COMPRESSION}

The constraints to use when encoding the canvas.

This can either be a hash with different constraints, or a symbol which acts as a preset for some constraints. If no constraints are given, ChunkyPNG will decide
for itself how to best create the PNG datastream.

  • Supported presets are :fast_rgba for quickly saving images with transparency, :fast_rgb for quickly saving opaque images, and :best_compression to obtain the smallest possible filesize.
  • :color_mode The color mode to use. Use one of the ChunkyPNG::COLOR_* constants.
  • :interlace true or false
  • :compression 0 - 9, or the Zlib constants such as Zlib::BEST_COMPRESSION
  • :bit_depth The bit depth to use. This option is only used for indexed images, in which case it overrides the determined minimal bit depth. For all the other color modes, a bit depth of 8 is used.

Saving a TRUECOLOR 24 bit image with a transparent pixel

  cv = ChunkyPNG::Image.from_rgb_stream(width, height, buffer)
  ds = cv.to_datastream(color_mode: ChunkyPNG::COLOR_TRUECOLOR, compression: Zlib::BEST_COMPRESSION, filtering: ChunkyPNG::FILTER_PAETH)
  pixel = [0,0,0,0,0,0] # color black
  ds.transparency_chunk = ChunkyPNG::Chunk::Transparency.new "tRNS", pixel.pack("C*")
  ds.save("out.png")