Image Resize and Crop

878 views
Skip to first unread message

Vivi

unread,
May 10, 2020, 12:36:43 PM5/10/20
to golang-nuts
How do you advice to resize and crop JPEG and PNG or probably WebP images without rely on 3rd parties dependencies?

It was hard to find a good snippet or could be useful to have basic API function in Go standard library since it's a common feature.

Michael Jones

unread,
May 10, 2020, 2:12:55 PM5/10/20
to Vivi, golang-nuts
I have an extremely elaborate resizing library, but it is so complex it would not make sense as a standard tool for common uses. (Many convolution kernels, separate windows, forward and backward mapping, separable convolutions, upsampling first for Nyquist issues, strategy phase and then concurrent filtering, etc. good work but over the top) 

If nothing else, you could port Paul Heckbert’s filter program from days of yore. Check graphics gems. 

On Sun, May 10, 2020 at 9:36 AM Vivi <create...@gmail.com> wrote:
How do you advice to resize and crop JPEG and PNG or probably WebP images without rely on 3rd parties dependencies?

It was hard to find a good snippet or could be useful to have basic API function in Go standard library since it's a common feature.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/89e32097-6aa0-48a4-b2ca-8d3756e26af3%40googlegroups.com.
--
Michael T. Jones
michae...@gmail.com

robert engels

unread,
May 10, 2020, 2:28:48 PM5/10/20
to Michael Jones, Vivi, golang-nuts
All of the code to do scaling and cropping in the ‘image’ package in the stdlib. You ‘draw’ into a new image to do scaling. You use SubImage() to perform cropping. See https://blog.golang.org/image-draw

Alternatively, if you need more advanced scaling operations it would be fairly trivial to port the GIMP scaling operations to Go.

Nigel Tao

unread,
May 10, 2020, 7:00:11 PM5/10/20
to robert engels, Michael Jones, Vivi, golang-nuts
On Mon, May 11, 2020 at 4:28 AM robert engels <ren...@ix.netcom.com> wrote:
> All of the code to do scaling and cropping in the ‘image’ package in the stdlib.

Cropping is in the stdlib but scaling is not. Various scaling
algorithms (e.g. nearest neighbor, Catmull-Rom) are in the
golang.org/x/image/draw package instead. As
https://godoc.org/golang.org/x/image/draw says, "This package is a
superset of and a drop-in replacement for the image/draw package in
the standard library".

https://godoc.org/golang.org/x/image/draw#example-Draw has some example code.

robert engels

unread,
May 10, 2020, 7:19:28 PM5/10/20
to Nigel Tao, Michael Jones, Vivi, golang-nuts
My bad, I didn’t read the API docs completely. Kind of a strange interface declaration in the stdlib image package - that there is only a single rectangle for Draw() and it is bounded by the two images.
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAOeFMNXO6J87er5Gw_m782qjP5OHhoAnnvTyLpPBv6C%2B%2B16eHA%40mail.gmail.com.

Vivi

unread,
May 10, 2020, 10:50:04 PM5/10/20
to golang-nuts

How does it compare to vipsthumbnail that utilize low memory usage which I believe it use horizontal threading, does Go's image/draw package consume large memory if the image source is huge?

robert engels

unread,
May 11, 2020, 12:52:53 AM5/11/20
to Vivi, golang-nuts
I don’t know and I doubt anyone else does off the top of their head, so why don’t you write a test and see?

Image processing is highly image and usage dependent, e.g. acceptable quality vs. speed vs. memory.

Using non-quantifiable terms like “huge” is not testable nor verifiable.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.

Randall O'Reilly

unread,
May 11, 2020, 2:14:34 AM5/11/20
to robert engels, Vivi, golang-nuts
https://github.com/anthonynsimon/bild has parallel image ops of all sorts and is widely used & well supported.

- Randy
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/4ECA0CE7-D503-4331-84CC-80EF1A85FB0B%40ix.netcom.com.

Nick

unread,
May 11, 2020, 6:35:47 AM5/11/20
to Vivi, golang-nuts
Quoth Vivi:
I'd encourage you to consider trying to implement simple image
manipulation algorithms yourself, if only for practice. I've found the
interfaces in the image package really nice to work with, and have
learned a lot implementing useful things myself based on them.
There's a very good blog post about the package here:
https://blog.golang.org/image

Vivi

unread,
May 11, 2020, 11:21:58 AM5/11/20
to golang-nuts

I found a snippet, memory usage with PNG vs JPG (more than 2x memory than PNG)
https://gist.github.com/logrusorgru/570d64fd6a051e0441014387b89286ca

Why does JPG consume more memory?

Is it fine to have libvips as a dependency for content management system in the same way as PHP require with Imagick or GD?

Robert Engels

unread,
May 11, 2020, 11:35:58 AM5/11/20
to Vivi, golang-nuts
I’m sorry but that makes no sense. Do you mean file size is smaller? Depends on jpeg compression options. It can be many times smaller than a png. 

On May 11, 2020, at 10:22 AM, Vivi <create...@gmail.com> wrote:


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.

Vivi

unread,
May 11, 2020, 12:59:08 PM5/11/20
to golang-nuts
It will make sense when you change PNG to JPG and loop 100x to see the actual memory consumption with grtme -v ...

Robert Engels

unread,
May 11, 2020, 3:40:59 PM5/11/20
to Vivi, golang-nuts
That means there is a memory leak. Once an image is decoded it takes the same amount of memory based on resolution and bit depth.

> On May 11, 2020, at 11:59 AM, Vivi <create...@gmail.com> wrote:
>
> It will make sense when you change PNG to JPG and loop 100x to see the actual memory consumption with grtme -v ...
>
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/75693ebf-5476-4807-a54d-4a2ed4fca411%40googlegroups.com.

Robert Engels

unread,
May 11, 2020, 3:43:43 PM5/11/20
to Vivi, golang-nuts
That’s assuming the image is displayed. It is easier to keep portions of the image on disk with certain formats (eg tiled).

> On May 11, 2020, at 2:40 PM, Robert Engels <ren...@ix.netcom.com> wrote:
>
> That means there is a memory leak. Once an image is decoded it takes the same amount of memory based on resolution and bit depth.
Reply all
Reply to author
Forward
0 new messages