Save an image to an IOBuffer

I’m trying to find the sizes of a bunch of images post-PNG compression, as a way of measuring complexity. But I don’t want to save the images to disk. It’s unnecessary and I’m processing lots of images. Is there a simple way of saving an image as a .PNG file into an IOBuffer? Then I can just read the filesize from there. Or is there an easier way?

you don’t say how you are generating said png files

It looks like PNGFiles.jl supports this. For example, compressing a 50x50 single channel image of zeros from 2500 bytes to 213:

julia> using PNGFiles
       img = zeros(UInt8, 50, 50)

julia> buf = IOBuffer();, img)

Note you can tweak how a PNG is compressed in multiple ways. PNGFiles.jl lets you chose the compression_level, compression_strategy and filters (see the method docstring and this chapter from the libpng book). In theory, libpng allows to treat each row of the image differently by applying a different filter (this is not yet implemented in PNGFiles.jl, we use the same filter for all rows).

That is to say, depending on these options, PNGFiles.jl would handle some kinds of “complexity” better than others.


Thank you! This works beautifully.