nfnt / resize

Pure golang image resizing
ISC License
3.02k stars 321 forks source link

Resizing seems to cause artifacts. #19

Closed aarondl closed 10 years ago

aarondl commented 10 years ago

Here is an image resized with nfnt/resize on the left, and imagemagick on the right (using resize_to_fit from rmagick which should fallback to a resize using Lanczos followed by a crop).

Edit: I've tried multiple filters and they all seem to have this issue of making these lines. The one in the picture is from Lanczos 3.

I'm not sure what to make of it, only that it appears to have a lot of artifacts (scanlines?) in the one created by this package. Could be something I'm doing but all I do is use image.Draw to translate & crop.

2014-09-04-131125_1267x929_scrot

jaredfolkins commented 10 years ago

Yup. I'm seeing something similar here. Extremely subtle at first glance but take a look for yourself. I had a hunch and rolled resize back to the previous commit (like I did here).

resize git:(581d15c) profile_large-1

resize git:(bdfbbea) profile_large

nfnt commented 10 years ago

Could you tell me which size the input images have and to which size you're resizing them to? While doing the optimizations, I had some numerical instabilities that where only visible at certain scale ratios. I thought that I sorted that out. Doesn't seem to be the case.

jaredfolkins commented 10 years ago

The original image is 600x450.

adam-levine-blonde-600x450

I rotated the image to 450x600.

adam-levine-blonde-450x600

I then processed it and the artifacts still exist.

profile_large

jaredfolkins commented 10 years ago

Interestingly cropping gifs doesn't appear to have the issue?

apple

profile_large

Gifs are being processed as RGBA where I'm thinking pngs and jpgs are coming across as YCbCr.

I noticed this function call is the primary difference in the process. Just an observation for now. Who knows if it has value.

https://github.com/nfnt/resize/blob/master/resize.go#L137

jaredfolkins commented 10 years ago

A different gif sample to show it isn't being effected.

amalgamation_micael_reynaud

profile_large-1

aarondl commented 10 years ago

This is the input image 28.jpg JPEG 320x180 320x180+0+0 8-bit DirectClass 11.3KB 0.000u 0:00.000

28

I'm resizing it so that the aspect ratio is kept, to a height of 630. Again, tried multiple filters, each seem to suffer from this in some form or another.

mrcwinn commented 10 years ago

Hey there, I'm seeing this too.

My sample image from unsplash.com (http://33.media.tumblr.com/537d8986d6b8df79a9fd6901e7174fe2/tumblr_nbk5oaEN2n1st5lhmo1_1280.jpg) is 1280x848. Downsampled to 320x212 and the image suffers heavily from artifacting.

nfnt commented 10 years ago

Fixed in 0c6a628df540d88783b3797728e6ad777e4722bc. The indexes used for reading the input images values were faulty and led to these artifacts. Regression from ccddecd1bf0b15e36e2ffcfdef7c6832eaa0dbf6.

aarondl commented 10 years ago

Thanks so much for fixing this. This package is great by the way.

nfnt commented 10 years ago

Thank you!

mrcwinn commented 10 years ago

Hey @nfnt - I'm noticing the artifacts have returned using Go 1.3.1 and current resize. Here are the images:

resized

original

The resize quality if really poor. I'm trying to gather more information. Oddly, I haven't noticed the issues when building from my other machine (same versions!). In both cases, I'm building with a target platform VM — so the environment is literally the same. It's strange!

mrcwinn commented 10 years ago

I can confirm the issue is on both my computers - I just didn't notice it on the laptop because it's retina and the @2x was getting shrunk in the browser.

nfnt commented 10 years ago

The artifacts you see are JPEG compression artifact and are not caused by this package. Try encoding the JPEG with a higher quality or encoding to a lossless image format like PNG.

mrcwinn commented 10 years ago

You're right. Sorry for the false alarm @nfnt — I just realized the standard jpeg library uses a 75% compression const when encoding, which I need to override.

Great work on the package!