# Save an image to an IOBuffer

**URL:** <https://discourse.julialang.org/t/save-an-image-to-an-iobuffer/78006>\
**Category:** New to Julia\
**Created:** [March 17, 2022, 4:50am UTC](https://discourse.julialang.org/t/save-an-image-to-an-iobuffer/78006 "2022-03-17T04:50:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![theta](https://avatars.discourse-cdn.com/v4/letter/t/54ee81/32.png) [@theta](https://discourse.julialang.org/u/theta)\
**Post date:** [March 17, 2022, 4:50am UTC](https://discourse.julialang.org/t/save-an-image-to-an-iobuffer/78006/1 "2022-03-17T04:50:06Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [March 17, 2022, 6:26am UTC](https://discourse.julialang.org/t/save-an-image-to-an-iobuffer/78006/2 "2022-03-17T06:26:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [March 17, 2022, 7:14am UTC](https://discourse.julialang.org/t/save-an-image-to-an-iobuffer/78006/3 "2022-03-17T07:14:09Z")

</div>

It looks like [PNGFiles.jl](https://github.com/JuliaIO/PNGFiles.jl) supports this. For example, compressing a 50x50 single channel image of zeros from 2500 bytes to 213:

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

julia> buf = IOBuffer();
       PNGFiles.save(buf, img)
       length(take!(buf))
213

```

---

<div class="post-metadata">

**Author:** ![drvi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drvi/32/13236_2.png) [@drvi](https://discourse.julialang.org/u/drvi)\
**Post date:** [March 17, 2022, 7:49am UTC](https://discourse.julialang.org/t/save-an-image-to-an-iobuffer/78006/4 "2022-03-17T07:49:36Z")

</div>

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](https://github.com/JuliaIO/PNGFiles.jl/blob/master/src/io.jl#L325-L329) and this chapter from the [libpng book](http://www.libpng.org/pub/png/book/chapter09.html)). 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.

---

<div class="post-metadata">

**Author:** ![theta](https://avatars.discourse-cdn.com/v4/letter/t/54ee81/32.png) [@theta](https://discourse.julialang.org/u/theta)\
**Post date:** [March 17, 2022, 4:33pm UTC](https://discourse.julialang.org/t/save-an-image-to-an-iobuffer/78006/5 "2022-03-17T16:33:05Z")

</div>

Thank you! This works beautifully.
