<p>I would like to know if anybody has any experience with image compression using Go.</p>
<p>I have to create multiple smaller versions of many images (mostly jpg, some png), which works really great using Go.</p>
<p>But well, the customer found Google Page Speed Insights, which tells him that images could be smaller by X % with compression blablabla...</p>
<p>Any ideas how I could shrink filesizes with native Go? I'm not a friend of using jpegoptim with cli calls, couldn't find a Go port and tbh my math-fu isn't good enough to understand and implement the used algorythms myself :( </p>
<p>Also, what do you think about stripping EXIF and similar unncessary things that photoshop/lightroom/etc add to files?</p>
<hr/>**评论:**<br/><br/>kjell_k: <pre><p>Hi.</p>
<p>In image/jpeg.Encode - you have a quality option - you can lower this a bit to reduce file size and stil have good quality.</p>
<p><a href="https://golang.org/pkg/image/jpeg/#Encode" rel="nofollow">https://golang.org/pkg/image/jpeg/#Encode</a></p>
<p>Sorry for brief reply. Just have my Phone here :)</p></pre>joetsai: <pre><p>The PNG package in the standard library uses compress/flate under the hood. The algorithm in flate has been vastly improved over the years, but the compression ratio still falls short slightly when aiming for highly compressed static content.</p>
<p>There are several reasons:</p>
<ul>
<li>For performance reasons, compress/flate only performs 4-byte offset matches, while the DEFLATE format actually allows for 3-byte matches. This sometimes matters for images without an alpha channel (RGB is 3-bytes).</li>
<li>Most PNG crushers use a specialized implementation of DEFLATE that heavily sacrifice compression speed for compression ratio (see <a href="https://en.wikipedia.org/wiki/Zopfli" rel="nofollow">https://en.wikipedia.org/wiki/Zopfli</a>). This algorithm does not exist in compress/flate.</li>
<li>(not specific to compress/flate) Some PNG crusher are actually lossy in that they discard some details from the image that humans cannot perceive so that DEFLATE can further benefit from more matches.</li>
</ul>
<p>I can't really speak for JPEG and other lossy formats.</p></pre>WikiTextBot: <pre><p><strong>Zopfli</strong></p>
<p>Zopfli is data compression software that encodes data into DEFLATE, gzip and zlib formats. It achieves higher compression than other DEFLATE/zlib implementations, but takes much longer to perform the compression. It was first released in February 2013 by Google as a free software programming library under the Apache License, Version 2.0. The name Zöpfli is the Swiss German diminutive of “Zopf”, a unsweetened type of Hefezopf.</p>
<hr/>
<p><sup>[</sup> <a href="https://www.reddit.com/message/compose?to=kittens_from_space" rel="nofollow"><sup>PM</sup></a> <sup>|</sup> <a href="https://reddit.com/message/compose?to=WikiTextBot&message=Excludeme&subject=Excludeme" rel="nofollow"><sup>Exclude</sup> <sup>me</sup></a> <sup>|</sup> <a href="https://np.reddit.com/r/golang/about/banned" rel="nofollow"><sup>Exclude</sup> <sup>from</sup> <sup>subreddit</sup></a> <sup>|</sup> <a href="https://np.reddit.com/r/WikiTextBot/wiki/index" rel="nofollow"><sup>FAQ</sup> <sup>/</sup> <sup>Information</sup></a> <sup>|</sup> <a href="https://github.com/kittenswolf/WikiTextBot" rel="nofollow"><sup>Source</sup></a> <sup>]</sup>
<sup>Downvote</sup> <sup>to</sup> <sup>remove</sup> <sup>|</sup> <sup>v0.27</sup></p></pre>peterhellberg: <pre><p>You can try this package if you want to experiment with lossy PNG compression: <a href="https://github.com/peterhellberg/lossypng" rel="nofollow">https://github.com/peterhellberg/lossypng</a></p>
<p>For JPEG you can use the quality option in the syandard library to reduce the file size (and visual quality)</p>
<p>Let me know if you find any (Go) native implementations of JPEG optimizers.</p></pre>SSoreil: <pre><p>On stripping EXIF, it's highly unlikely to be a significant amount of data. Files which do have a lot of EXIF data are probably straight out of camera and aren't what you are putting up to begin with. Also, stripping out EXIF data for no reason might not be what you want at all points.</p>
<p>Not that I know much about the rather arbitrary goal you are trying to reach here, if you want to crush files you probably want to call out to the CLI for that. I wrote some binding for PNGquant or whatever it's called a few years back to call it as a library and it was very inconvenient. I don't think the code still exists at this point.</p>
<p>image/jpeg and image/png are probably good enough.</p></pre>
![](https://static.golangjob.cn/static/img/banner.png)