nfnt / resize Goto Github PK
View Code? Open in Web Editor NEWPure golang image resizing
License: ISC License
Pure golang image resizing
License: ISC License
Hi,
we get this issue when resizing some user-supplied pictures.
And no, unfortunately I can't provide any sample pics. I'll try to get one.
goroutine 5456 [running]:
panic(0xc34040, 0xc42000c0c0)
/usr/local/go/src/runtime/panic.go:500 +0x1a1
github.com/nfnt/resize.resizeRGBA(0xc4203e4c40, 0xc4203e58c0, 0x0, 0xc422c18000, 0x1944, 0x1944, 0x
github.com/nfnt/resize/converter.go:119 +0x44d
github.com/nfnt/resize.Resize.func2(0xc420610140, 0xc4203e4c40, 0xc4203e58c0, 0x0, 0xc4205900a0, 0x
github.com/nfnt/resize/resize.go:130 +0xc5
created by github.com/nfnt/resize.Resize
github.com/nfnt/resize/resize.go:131 +0x345b
Is it possible to resize/thumbnail starting from other point rather than 0,0?
If not, do you have plans for it or would accept a PR?
I've noticed that when images are resized, specifically when they're reduced in size, the resulting file size is larger than the original. I'm not sure if I'm misunderstanding something but I'd expect the file size to decrease with the size of the image.
width := 10
height := 10
img, format, err := image.Decode(bytes.NewReader(file))
if err != nil {
return nil, "", err
}
//Only resize if new dimensions are specified
if width != 0 || height != 0 {
img = resize.Resize(width, height, img, resize.Lanczos3)
}
var buf bytes.Buffer
switch format {
case "jpeg":
jpeg.Encode(&buf, img, nil)
case "png":
png.Encode(&buf, img)
case "gif":
gif.Encode(&buf, img, nil)
default:
return nil, "", errors.New("not an image")
}
data := buf.Bytes()
fmt.Printf("Size: %d", binary.Size(data))
In this case, I'm serving the images from a web server and I've implemented this resizing to decrease request sizes. In one example, the original request with no resizing is 755B. With resizing the image to half its size, the request size is 4.5KB.
Any suggestions?
I have looked at your code and that's what I have to say: you are fundamentally wrong in dealing with alpha-premultiplied images. Alpha premultiplication was introduced to eliminate interpolation error on images with alpha channel. If image is not premultiplied you shoud premultipy it first before interpolation. Your code does opposite. https://en.wikipedia.org/wiki/Alpha_compositing#Description
this is the original image:
https://o9kwm2s9v.qnssl.com/production/test/iLXVN20170203114422.png
after resizing it to 1024, image not show on mobile browser:
https://ohe31mpy9.qnssl.com/test/test/FcvCj20170203114515.png
i changed nfnt to https://github.com/gographics/imagick, resizing works well. so whats the problem with nfnt, could someone help me?
The package resizes the images and saves them all at 96 dpi, even if the original dpi is higher (e.g. 300). Has anyone faced and solved this issue?
Thanks!
Hi, I've built Blade on top of resize and have nothing but high regards. See the following:
https://github.com/jondot/blade
http://blog.paracode.com/2015/10/01/a-modern-ios-app-icon-and-image-workflow-with-blade/
I'm very happy with the API and the options, resize strikes a sweet spot of being simple and powerful. At first, I was testing out c-based libraries but made a bet on this as a pure-go implementation. It paid off, I can compile Blade on Gopherjs and run image transformation within node.js.
Thanks!
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.
Resized image lost all metadata, like geocoordinates and so on.
I got a runtime panic when trying to resize a jpeg image that has 4x1 Chroma Subsampling, aka. image.YCbCrSubsampleRatio411. In ycc.go, it seems the default is 444 if no other sample is matched, which causes an index out of range panic.
I have a reproducible test program and image, if desired.
Running this give me some weird image results with the re sizing
Here is my parameters
cat inputfile.jpg | ./bin/images -width 2672 -height 1283 > outfile.jpg
package main
import (
"flag"
"github.com/nfnt/resize"
"image"
"image/jpeg"
"os"
)
func main() {
var w, h uint
flag.UintVar(&w, "w", 0, "width value")
flag.UintVar(&h, "h", 0, "height value")
flag.Parse()
if w == 0 || h == 0 {
fmt.Println("Width and Height can't be 0")
os.Exit(560)
}
// Decode the image.
m, _, _ := image.Decode(os.Stdin)
m = resize.Resize(w, h, m, resize.Lanczos3)
nfile, _ := os.Create("NewCostaRicanFrog.jpg")
jpeg.Encode(os.Stdout, m, nil)
file.Close()
}
It's convenience that Resize return a draw.Image instead of image.Image that can continue draw in the image.
What's the best way to detect the image file format so I can use the proper decoder?
As it may have been apparent by my long reaction time to PRs and issues, I haven't spend too much time working on this package in the last years. And I plan to keep it that way, hence I won't really look at PRs and issues anymore. Take a look at x/image/draw or gopkg.in/gographics/imagick.v2/imagick for packages that offer image resizing and much more.
Unfortunately I don't have access to image that caused the crash and don't know what was its size and what was the destination size. It could be related to some boundary conditions (very small source or very small destination size).
But this happened using the latest resize (ccddecd):
panic: runtime error: slice bounds out of range
goroutine 574 [running]:
runtime.panic(0x914ac0, 0xc524af)
/usr/local/Cellar/go/1.3/libexec/src/pkg/runtime/panic.c:279 +0xf5
github.com/nfnt/resize.resizeRGBA(0xc208256440, 0xc208256700, 0x3fff7b03531dec0d, 0xc20808a500, 0x268, 0x268, 0xc20808aa00, 0x9a, 0x9a, 0x4)
/var/folders/9t/62vbwdb93rg3tpvsy6y30tzh0000gn/T/godep/rev/cc/ddecd1bf0b15e36e2ffcfdef7c6832eaa0dbf6/src/github.com/nfnt/resize/converter.go:98 +0x5b3
github.com/nfnt/resize.func·001()
/var/folders/9t/62vbwdb93rg3tpvsy6y30tzh0000gn/T/godep/rev/cc/ddecd1bf0b15e36e2ffcfdef7c6832eaa0dbf6/src/github.com/nfnt/resize/resize.go:112 +0xcb
created by github.com/nfnt/resize.Resize
/var/folders/9t/62vbwdb93rg3tpvsy6y30tzh0000gn/T/godep/rev/cc/ddecd1bf0b15e36e2ffcfdef7c6832eaa0dbf6/src/github.com/nfnt/resize/resize.go:113 +0x27bc
This corresponds to the crash at the last line of this:
func resizeRGBA(in *image.RGBA, out *image.RGBA, scale float64, coeffs []int16, offset []int, filterLength int) {
oldBounds := in.Bounds()
newBounds := out.Bounds()
minX := oldBounds.Min.X * 4
maxX := (oldBounds.Max.X - oldBounds.Min.X - 1) * 4
for x := newBounds.Min.X; x < newBounds.Max.X; x++ {
row := in.Pix[(x-oldBounds.Min.Y)*in.Stride:]
This is on a 24-core amd64 ubuntu 12.4 box, go 1.3.
On a related note: it would be good to have an option to disable internal nfnt threading (i.e. make it do the resize using a single, current goroutine) so that it's possible to catch and recover from crashes in nfnt with recover(). Imagine a server application that does image resizing - if a crash in nfnt happens on some random goroutine, it'll crash the whole server. If it happens on current goroutine, I can write a wrapper that will recover() from the panic().
I assume this is related to #45, but have done some more looking in to it. Currently this seems to be an issue with transparent PNGs on testing, and is present with any interpolation function but "Nearest Neighbor".
In both the examples below the image is being read in with image.Decode
and resized with resize.Thumbnail(uint(*width), uint(*height), img, methods[*method])
, before using image.Encode
to a file.
For reference, the source image I am using, and settings on save are the following:
Along with the result of the various resize methods provided below. Resizing from the original 500x500 size to 100x100.
This example is even more obvious and unpredictable when you have something like anti-aliased text.
Then resizing down to 100px width.
Is there a way to create a resize/thumbnail from a subset of the original image?
I tried your module and been getting this.
/usr/lib/go/src/pkg/github.com/nfnt/resize/ycc.go:126: undefined: image.YCbCrSubsampleRatio440
This is on ubuntu 12.04
I like this: https://code.google.com/p/appengine-go/source/browse/example/moustachio/resize/resize.go
because I can supply a rectangle to crop the image.
Hi, I am using this package to resize JPEGs, and I received the below error message:
ERROR Failed to jpeg decodeconfig image:unsupported JPEG feature: bad Pq value
panic: runtime error: invalid memory address or nil pointer dereference
I couldn't find much in the existing issues:
https://code.google.com/p/go/issues/detail?id=4500
http://golang.org/src/pkg/image/jpeg/reader.go
I couldn't find much about pq value in general. Has anyone seen the same error message? Or any ideas?
Thanks!
github.com/nfnt/resize.imageYCbCrToYCC(0xc84cacfc80, 0x14)
D:/Workplace/src/github.com/nfnt/resize/ycc.go:220 +0x821
github.com/nfnt/resize.resizeNearest(0xc8, 0x7d, 0x4023333333333333, 0x4023333333333333, 0x7fb757e33348, 0xc84cacfc80, 0x0, 0x0, 0x0)
D:/Workplace/src/github.com/nfnt/resize/resize.go:415 +0x6b9d
github.com/nfnt/resize.Resize(0xc8, 0x7d, 0x7fb757e33348, 0xc84cacfc80, 0x0, 0x0, 0x0)
D:/Workplace/src/github.com/nfnt/resize/resize.go:96 +0x416
github.com/nfnt/resize.Thumbnail(0xc8, 0x7d, 0x7fb757e33348, 0xc84cacfc80, 0x0, 0x0, 0x0)
D:/Workplace/src/github.com/nfnt/resize/thumbnail.go:54 +0x15f
please help fix this problem :)
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.
hi, I'm using your lib to resize my photo. It seem consume too much time for one photo.
I'm using NearestNeighbor method. my photo is about 5Mbyte.
here is my code: https://gist.github.com/ifq/6268096 and the result for resize one photo is:
$ ./resize ~/tmp/testimg/2012-08-26/IMGA0598.JPG
time consumed sub: 0 6525402249
time consumed sub: 1 7193831897
time consumed: 14912730741
my laptop is i7, 8G memory. host OS win7 32bit, VM OS is linux mint inside virtualbox with 4G memory.
If it's normal performance of this lib, do you planing only performance improvement?
Hey @nfnt it looks like this is on a license similar to MIT. Are you okay with another open source project using resize
as a dependency if the project is on MIT? Do you have any issues with that?
Much appreciated! Thanks for your work on this as well.
In a goroutine spawned by the library no less, so it can't be recovered without patching it.
Here is the stack trace from the relevant goroutines:
panic: runtime error: index out of range
goroutine 34 [running]:
vendor/resize.resizeRGBA64(0xc20815e480, 0xc20815e140, 0x0, 0xc2080e7500, 0xc3c, 0xc3c, 0xc2080ef900, 0x30f, 0x30f, 0x4)
/var/lib/gopath/src/git.pixinote.net/postman/vendor/resize/converter.go:152 +0x6b7
vendor/resize.func012()
/var/lib/gopath/src/git.pixinote.net/postman/vendor/resize/resize.go:276 +0xce
created by vendor/resize.Resize
/var/lib/gopath/src/git.pixinote.net/postman/vendor/resize/resize.go:277 +0x2a59
goroutine 31 [runnable]:
sync.(*WaitGroup).Wait(0xc2080d5c00)
/usr/local/go/src/sync/waitgroup.go:132 +0x169
vendor/resize.Resize(0x30f, 0x30f, 0x7fcc97bda3d8, 0xc20815e440, 0x2, 0x0, 0x0)
/var/lib/gopath/src/git.pixinote.net/postman/vendor/resize/resize.go:279 +0x2a98
endpoint.handleResize(0x8b25f0, 0x4, 0x15)
/var/lib/gopath/src/git.pixinote.net/postman/endpoint/render.go:74 +0xa06
created by endpoint.putNoteImage
/var/lib/gopath/src/git.pixinote.net/postman/endpoint/notes.go:668 +0x9fc
Is there a console app version of this where you could resize all images in a folder to a certain size to a new path?
i wrote this simple program and it works but resulting image has color artifacts in it:
Source image
Result image
Expected image
code:
package main
import (
"github.com/nfnt/resize"
"image"
_ "image/jpeg"
"image/png"
"os"
)
func main() {
reader, _ := os.Open("test/1.jpg")
defer reader.Close()
m, _, _ := image.Decode(reader)
m = resize.Resize(32, 32, m, resize.Bicubic)
file, _ := os.OpenFile("test/1.png", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
png.Encode(file, m)
defer file.Close()
}
Is adding support for animated GIFs a possibility?
I called resize on the following png image and got: unknown format!
orig
Hi. This simple code generate a panic error:
package main
import (
"github.com/nfnt/resize"
"os"
"image/jpeg"
)
func main() {
f, err := os.Open("test.jpg")
if err != nil {
panic(err)
}
image, err := jpeg.Decode(f)
if err != nil {
panic(err)
}
resize.Resize(600, 0, image, resize.Lanczos3)
}
Message:
go run src/test.go
panic: runtime error: index out of range
goroutine 1 [running]:
github.com/nfnt/resize.imageYCbCrToYCC(0x18416140, 0x12)
/dev/src/github.com/nfnt/resize/ycc.go:220 +0x5af
github.com/nfnt/resize.Resize(0x1f4, 0x0, 0x810bf00, 0x18416140, 0x5, 0x0, 0x184000f0)
/dev/src/github.com/nfnt/resize/resize.go:173 +0x320a
main.main()
/dev/src/test.go:20 +0xa0
exit status 2
The image is attached. It is a movie poster. The error appears with any InterpolationFunction
. What can I do?
Hello,
Could you please tag this repository?
I am the Debian Maintainer for this project and tags would help Debian keep up with new releases/bugfixes.
See:
supper tiff
maybe and for different image formats
Hi nfnt,
thank you for creating this great library. Saves me from using another non-go dependency like imagemagick :)
I am resizing PNG images to JPEG. These images can include transparency. What is the best way to change the black background color in JPEG to for example white?
I know that JPEG doesn't support transparency and so it would be nice if one could just change the black to another color.
Best regards
Bijan
Thank you for this package. I downsized this image
https://www.dropbox.com/s/fs91vfx1y4khymv/button_cost_up.png?dl=0
and got artifacts
https://www.dropbox.com/s/0me0559p7nl3yf5/test_resized.png?dl=0
with all interpolations except NearestNeighbor.
How to avoid it?
Hi,
While trying out smlr cf. "link 1" (which uses butteraugli cf. "link 2"), I encountered this error:
$ go get github.com/jasonmoo/smlr
# github.com/nfnt/resize
/usr/lib/go/src/pkg/github.com/nfnt/resize/ycc.go:126: undefined: image.YCbCrSubsampleRatio440
/usr/lib/go/src/pkg/github.com/nfnt/resize/ycc.go:196: undefined: image.YCbCrSubsampleRatio440
Please tell me if you need me to provide more information.
[1] https://github.com/jasonmoo/smlr
[2] https://github.com/google/butteraugli
Is there any cropping functions?
It'd be nice if the pkg just returned the original image if the new dimensions are the same as the current dimensions. I looked at the source and it appears to still run through the whole thing.
Specified width / height ratio if the ratio is different to the original ratio, it will lead to some strange effects.
Can you have a benchmark for the algorithms.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.