disintegration / imaging

Imaging is a simple image processing package for Go
MIT License
5.28k stars 440 forks source link

Add Scale function #96

Closed vasyvasilie closed 5 years ago

vasyvasilie commented 5 years ago

Scale should scale image (up or down) with the best aspect ratio

coveralls commented 5 years ago

Coverage Status

Coverage remained the same at 100.0% when pulling 42a9c9c60a0e01764a4a4d7ec93ef42fa4a21240 on vasyvasilie:add-scale into 465faf0892b5c7b3325643b0e47282e1331672e7 on disintegration:master.

disintegration commented 5 years ago

First of all, thank you for the contribution!

The Scale function takes the ratio value (a scale factor) as it's main argument. I believe it should return an image where both dimensions are scaled using the given ratio in the closest way possible (multiplying by ratio and rounding the result). At least this is what I would expect. E.g.

w := int(ratio*float64(img.Bounds().Dx()) + 0.5)
h := int(ratio*float64(img.Bounds().Dy()) + 0.5)
dst = Resize(src, w, h, filter)

As far as I can see this is what ImageMagick does with its percentage resize (`convert src.png -resize 50% dst.png).

On the other hand, it's just three lines of code, so I'm not sure we need to add another function to the API.

vasyvasilie commented 5 years ago

Addition of half-one and combined operations of floats and ints in this place gave for me not determined results with different percentages of scaling. My point was in combination of determined result and better xy-ratio after scaling, that's why i created this pr for mb help others get more accurate results.

But you're right, that's my code is only layer on your base code and can be simple realized

disintegration commented 5 years ago

I guess it depends on the use case. I think the algorithm you provided, while it works for you, provides less predictable results. For example:

source image size scaling factor output image size
256x128 0.3 76x38
256x129 0.3 77x39

Both images have width = 265px, both are scaled with factor of 0.3 but the output image widths are different - 76px vs 77px. While I understand the intent of the algorithm, I believe it would be confusing for end-users.