nfnt / resize

Pure golang image resizing

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Wrong size of final image after resize

opened this issue · comments

I would like to report issue which occurs with some pictures.

When I resize this image:
https://twimg0-a.akamaihd.net/profile_images/2725838700/32f4e2dce13c0b983003eaa10efea6a5.png

using code

var m2 image.Image = resize.Resize(60, 60, m1, resize.Lanczos3)

it does not matter what filter I use, final dimensions are 59x59 and one column and row of pixels is missing at right and bottom.
For other images the result is as expected (60x60px).

I don't know if it is anyhow important, source image is 256x256px.

Click "Network" to see my suggested fix for this ("improve-canvas-size") and other issues.

Hi Jason,

thank you for your reply. I switched to your branch to give it a try and I have to say you did amazing work, results are perfect. I can see only one problem. It seems that after resize there appears some kind of border when I use other filter than NearestNeighbor and save as JPG, why is it so and is it intent?

Saved as JPG:

Bicubic
Bicubic

Bilinear
Bilinear

Lanczos2
Lanczos2

Lanczos3
Lanczos3

NearestNeighbor
NearestNeighbor

MitchellNetravali
MitchellNetravali

When I save result image as PNG the resulting images are with some black parts:

Bicubic
Bicubic

Bilinear
Bilinear

Lanczos2
Lanczos2

Lanczos3
Lanczos3

MitchellNetravali
MitchellNetravali

NearestNeighbor
NearestNeighbor

This isn't the appropriate place to suggest using fpresize instead (disclosure: I am the author), but GitHub doesn't have private messaging, and you have no contact info.

I don't know what the problem is with the PNG images. Looks like some sort of numeric overflow/underflow. I'll look into it when I get a chance.

The border around the JPEG images is there because nfnt/resize uses transparent virtual pixels. That's useful sometimes, but it's not good if you're going to write the image to a file format that does not support transparency.

Assuming the original image did not have any transparency, then stripping the alpha channel before writing the JPEG file is probably the best thing to do (i.e. set each pixel to have maximum opacity).

The PNG corruption is caused by invalid combinations of color and transparency values. I fixed it in my copy of the project.

Issue is fixed. I pulled in the fixes from Jason, thanks you!
FYI, the "black parts" issue was caused by the way Go's image packages handles accessing value outside the image boundaries. It returns constant zeros, also on the alpha channel, if the underlying image has one. I fixed it by replicating the first/last row/column values if accessing values outside the boundaries.