Coder Social home page Coder Social logo

vips's Introduction

Vips for go

This package is powered by the blazingly fast libvips image processing library, originally created in 1989 at Birkbeck College and currently maintained by JohnCupitt.

This is a loosely port of sharp an awesome module for node.js built by Lovell Fuller

The typical use case for this high speed package is to convert large images of many formats to smaller, web-friendly JPEG, PNG images of varying dimensions.

The performance of JPEG resizing is typically 8x faster than ImageMagick and GraphicsMagick, based mainly on the number of CPU cores available.

When generating JPEG output all metadata is removed and Huffman tables optimised without having to use separate command line tools like jpegoptim and jpegtran.

Installation

go get github.com/daddye/vips

libvips can take advantage of liborc if present.

Install libvips on Mac OS

brew install homebrew/science/vips --without-fftw --without-libexif --without-libgsf \
  --without-little-cms2 --without-orc --without-pango --without-pygobject3 \
  --without-gobject-introspection --without-python

Install libvips on Linux

Compiling from source is recommended:

sudo apt-get install automake build-essential git gobject-introspection \
  libglib2.0-dev libjpeg-turbo8-dev libpng12-dev gtk-doc-tools
git clone https://github.com/jcupitt/libvips.git
cd libvips
./bootstrap.sh
./configure --enable-debug=no --without-python --without-fftw --without-libexif \
  --without-libgf --without-little-cms --without-orc --without-pango --prefix=/usr
make
sudo make install
sudo ldconfig

Usage

You can use package from the command line (go install github.com/daddye/vips/vips-cmd):

vips-cmd -file test.jpg -width 400 -height 600 > /tmp/test.jpg

Or simply importing the package and then:

options := vips.Options{
	Width:        800,
	Height:       600,
	Crop:         false,
	Extend:       vips.EXTEND_WHITE,
	Interpolator: vips.BILINEAR,
	Gravity:      vips.CENTRE,
	Quality:      95,
}
f, _ := os.Open("/tmp/test.jpg")
inBuf, _ := ioutil.ReadAll(f)
buf, err := vips.Resize(inBuf, options)
if err != nil {
	fmt.Fprintln(os.Stderr, err)
	return
}
// do some with your resized image `buf`

Performance

Test by @lovell

Test environment

  • Intel Xeon L5520 2.27GHz 8MB cache
  • Ubuntu 13.10
  • libvips 7.38.5

The contenders

  • imagemagick-native - Supports Buffers only and blocks main V8 thread whilst processing.
  • imagemagick - Supports filesystem only and "has been unmaintained for a long time".
  • gm - Fully featured wrapper around GraphicsMagick.
  • sharp - Caching within libvips disabled to ensure a fair comparison.

The task

Decompress a 2725x2225 JPEG image, resize and crop to 720x480, then compress to JPEG.

Results

Module Input Output Ops/sec Speed-up
imagemagick-native buffer buffer 0.97 1
imagemagick file file 2.49 2.6
gm buffer file 3.72 3.8
gm buffer buffer 3.80 3.9
gm file file 3.67 3.8
gm file buffer 3.67 3.8
sharp buffer file 13.62 14.0
sharp buffer buffer 12.43 12.8
sharp file file 13.02 13.4
sharp file buffer 11.15 11.5
sharp +sharpen file buffer 10.26 10.6
sharp +progressive file buffer 9.44 9.7
sharp +sequentialRead file buffer 11.94 12.3

You can expect much greater performance with caching enabled (default) and using 16+ core machines.

Thanks

This module would never have been possible without the help and code contributions of the following people:

License

Copyright (C) 2014 Davide D'Agostino

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

vips's People

Contributors

bfitzsimmons avatar crahles avatar daddye avatar fawick avatar lwalen avatar mistobaan avatar pinglamb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

vips's Issues

Error while installing on Mac

brew install vips --without-fftw --without-libexif --without-libgsf \
>   --without-little-cms2 --without-orc --without-pango --without-pygobject3 \
>   --without-gobject-introspection --without-python
==> Installing vips from homebrew/homebrew-science
==> Downloading http://www.vips.ecs.soton.ac.uk/supported/8.0/vips-8.0.2.tar.gz
Already downloaded: /Library/Caches/Homebrew/vips-8.0.2.tar.gz
==> ./configure --prefix=/usr/local/Cellar/vips/8.0.2
==> make check
============================================================================
make[3]: *** [test-suite.log] Error 1
make[2]: *** [check-TESTS] Error 2
make[1]: *** [check-am] Error 2
make: *** [check-recursive] Error 1
couldn't understand kern.osversion `14.3.0'

READ THIS: https://git.io/brew-troubleshooting
If reporting this issue please do so at (not Homebrew/homebrew):
  https://github.com/homebrew/homebrew-science/issues

Support multiple output images format

Currently it support both jpeg and png, but it only generates jpeg images

For instance, this is an important limitation when processing png images with transparency

It would be great if image format conversion could be optional, like in sharp

Memory leaks

Initializing with C.vips_image_new() is needed only for im_* functions. If you do it for vips_* functions you will leak memory.

Specific Crop Value

Ability to specify top, left, inwidth, inheight, outheight, outwidth for the cropper

Bundle syso for darwin_64 and linux_64

I know this is not the best thing but will allow two things:

  1. Don't have to install libvips/libjpeg and friends
  2. Avoid nasty configurations (since will be bundle with jpeg-turbo)

check for not found

How do we check for file not found when going through jpeg/png load? Without matching against the string returned from vips_error_buffer.

Output image size not correct

Resize a image (size 4928x3280) to width=150 using the vips-cmd tool:

vips-cmd -file test.jpg -width 150 -height 0 > out.jpg

The output image has size 149x99 (should be 150x100)

Installation fails w/ "undefined reference to `VIPS_INIT`"

Environment: Vanilla installation of Ubuntu 14.04, installed libvips with apt-get install libvips37 libvips-dev (Version: 7.38.5-2)

$ go get -x github.com/DAddYE/vips
WORK=/tmp/go-build039786364
mkdir -p $WORK/github.com/DAddYE/vips/_obj/
mkdir -p $WORK/github.com/DAddYE/
cd /home/fabian/go/src/github.com/DAddYE/vips
pkg-config --cflags vips
pkg-config --libs vips
CGO_LDFLAGS="-g" "-O2" "-Wl,--export-dynamic" "-pthread" "-lvips" "-ljpeg" "-lm" "-lstdc++" "-lxml2" "-lgmodule-2.0" "-lfftw3" "-lMagickWand" "-lorc-0.4" "-llcms2" "-lIlmImf" "-lImath" "-lHalf" "-lIex" "-lIlmThread" "-lopenslide" "-lmatio" "-lhdf5" "-lz" "-lcfitsio" "-lpthread" "-lwebp" "-lpangoft2-1.0" "-ltiff" "-lpng12" "-lexif" "-lMagickCore" "-lpango-1.0" "-lfontconfig" "-lgobject-2.0" "-lglib-2.0" "-lfreetype" /usr/local/go/pkg/tool/linux_amd64/cgo -objdir $WORK/github.com/DAddYE/vips/_obj/ -- -pthread -fopenmp -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/libxml2 -I/usr/include/ImageMagick -I/usr/include/orc-0.4 -I/usr/include/OpenEXR -I/usr/include/openslide -I/usr/include/pango-1.0 -I/usr/include/x86_64-linux-gnu -I/usr/include/libpng12 -I/usr/include/libexif -I/usr/include/harfbuzz -I/usr/include/freetype2 -I $WORK/github.com/DAddYE/vips/_obj/ vips.go
/usr/local/go/pkg/tool/linux_amd64/6c -F -V -w -trimpath $WORK -I $WORK/github.com/DAddYE/vips/_obj/ -I /usr/local/go/pkg/linux_amd64 -o $WORK/github.com/DAddYE/vips/_obj/_cgo_defun.6 -D GOOS_linux -D GOARCH_amd64 $WORK/github.com/DAddYE/vips/_obj/_cgo_defun.c
gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -print-libgcc-file-name
gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -pthread -fopenmp -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/libxml2 -I/usr/include/ImageMagick -I/usr/include/orc-0.4 -I/usr/include/OpenEXR -I/usr/include/openslide -I/usr/include/pango-1.0 -I/usr/include/x86_64-linux-gnu -I/usr/include/libpng12 -I/usr/include/libexif -I/usr/include/harfbuzz -I/usr/include/freetype2 -I $WORK/github.com/DAddYE/vips/_obj/ -g -O2 -o $WORK/github.com/DAddYE/vips/_obj/_cgo_main.o -c $WORK/github.com/DAddYE/vips/_obj/_cgo_main.c
gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -pthread -fopenmp -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/libxml2 -I/usr/include/ImageMagick -I/usr/include/orc-0.4 -I/usr/include/OpenEXR -I/usr/include/openslide -I/usr/include/pango-1.0 -I/usr/include/x86_64-linux-gnu -I/usr/include/libpng12 -I/usr/include/libexif -I/usr/include/harfbuzz -I/usr/include/freetype2 -I $WORK/github.com/DAddYE/vips/_obj/ -g -O2 -o $WORK/github.com/DAddYE/vips/_obj/_cgo_export.o -c $WORK/github.com/DAddYE/vips/_obj/_cgo_export.c
gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -pthread -fopenmp -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/libxml2 -I/usr/include/ImageMagick -I/usr/include/orc-0.4 -I/usr/include/OpenEXR -I/usr/include/openslide -I/usr/include/pango-1.0 -I/usr/include/x86_64-linux-gnu -I/usr/include/libpng12 -I/usr/include/libexif -I/usr/include/harfbuzz -I/usr/include/freetype2 -I $WORK/github.com/DAddYE/vips/_obj/ -g -O2 -o $WORK/github.com/DAddYE/vips/_obj/vips.cgo2.o -c $WORK/github.com/DAddYE/vips/_obj/vips.cgo2.c
gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -o $WORK/github.com/DAddYE/vips/_obj/_cgo_.o $WORK/github.com/DAddYE/vips/_obj/_cgo_main.o $WORK/github.com/DAddYE/vips/_obj/_cgo_export.o $WORK/github.com/DAddYE/vips/_obj/vips.cgo2.o -g -O2 -Wl,--export-dynamic -pthread -lvips -ljpeg -lm -lstdc++ -lxml2 -lgmodule-2.0 -lfftw3 -lMagickWand -lorc-0.4 -llcms2 -lIlmImf -lImath -lHalf -lIex -lIlmThread -lopenslide -lmatio -lhdf5 -lz -lcfitsio -lpthread -lwebp -lpangoft2-1.0 -ltiff -lpng12 -lexif -lMagickCore -lpango-1.0 -lfontconfig -lgobject-2.0 -lglib-2.0 -lfreetype
# github.com/DAddYE/vips
/tmp/go-build039786364/github.com/DAddYE/vips/_obj/vips.cgo2.o: In function `vips_initialize':
./vips.h:6: undefined reference to `VIPS_INIT'
c

Width of returned image is off by one

We replaced http://github.com/disintegration/imaging with vips in an image proxy to speed up resize operations. They are faster, but in our test suite I keep getting errors where the returned image width is 1 pixel greater than the specified width. I suspect (but have not been able to verify) that the difference is due to our calculations for height. Since I always want proportional scaling, can I just include the width and let vips figure out the height? I can't seem to find the documentation I'm looking for to figure this out on my own.

Incompatible library version: libvips.42.dylib requires version 4601.0.0 or later, but libgmodule-2.0.0.dylib provides version 4401.0.0

I install libvips already succeed in my mac osx,but there's error like this when I check the version.

$ vips --version
dyld: Library not loaded: /usr/local/lib/libgmodule-2.0.0.dylib
Referenced from: /usr/local/bin/vips
Reason: Incompatible library version: vips requires version 4601.0.0 or later, but libgmodule-2.0.0.dylib provides version 4401.0.0

how can I upgrade the libgmodule,I google it but there's nothing help.

Thumbnail

Hi,

Any plans to merge feature/thumbnail branch?

Best regards

Bindings will hard crash on invalid color space

The attached patch addresses this crash, which happens with specific ill-formed jpegs.

diff --git a/vips.go b/vips.go
index 36118e1..14cd90a 100644
--- a/vips.go
+++ b/vips.go
@@ -311,6 +311,10 @@ func Resize(buf []byte, o Options) ([]byte, error) {
        C.vips_colourspace_0(image, &tmpImage, C.VIPS_INTERPRETATION_sRGB)
        C.g_object_unref(C.gpointer(image))
        image = tmpImage
+       if image == nil {
+               debug("colourspace conversion failed")
+               return nil, resizeError()
+       }

        // Finally save
        length := C.size_t(0)

Support gif, webp

Hi.
Vips has vips_gifload and vips_webpload functions.
Why don't you use them?
Also you determine image type by byte_marker, could you please check if http.DetectContentType function is more suitable for this case?
Thank you

Install libvips using supported release instead of git source

I was install vips on a remote server running Debian linux. At first I was compiling libvips from Git source according to README.md, but it took me a lot of time to download the full Git repo and then fix package dependence issues. It also required installing of many GTK packages, which is a concern for the server.

Later, I found out a better way is to just download the supported libvips release from http://www.vips.ecs.soton.ac.uk/supported/current/. Install only a small number of packages and everything was OK. And this method works for both Debian and Ubuntu linux.

Install packages for Ubuntu:

sudo apt-get install build-essential libjpeg-turbo8-dev libpng12-dev

Install packages for Debian:

sudo apt-get install build-essential libjpeg62-turbo-dev libpng12-dev

Make and install libvips

wget http://www.vips.ecs.soton.ac.uk/supported/current/vips-8.0.2.tar.gz
tar xf vips-8.0.2.tar.gz
cd vips-8.0.2

./configure --enable-debug=no --without-python --without-fftw --without-libexif \
  --without-libgf --without-little-cms --without-orc --without-pango --prefix=/usr
make
sudo make install
sudo ldconfig

issues on go get

vips-7.38.5

go get github.com/daddye/vips
/opt/go/src/github.com/daddye/vips/vips.go:129: cannot use &image (type *__Ctype_struct__VipsImage) as type *__Ctype_VipsImage in function argument
/opt/go/src/github.com/daddye/vips/vips.go:131: cannot use &image (type *__Ctype_struct__VipsImage) as type *__Ctype_VipsImage in function argument
/opt/go/src/github.com/daddye/vips/vips.go:225: cannot use &tmpImage (type *__Ctype_struct__VipsImage) as type *__Ctype_VipsImage in function argument
/opt/go/src/github.com/daddye/vips/vips.go:236: cannot use image (type __Ctype_struct__VipsImage) as type *_Ctype_VipsImage in function argument
/opt/go/src/github.com/daddye/vips/vips.go:236: cannot use &tmpImage (type *__Ctype_struct__VipsImage) as type *__Ctype_VipsImage in function argument
/opt/go/src/github.com/daddye/vips/vips.go:265: cannot use image (type *_Ctype_struct__VipsImage) as type *_Ctype_VipsImage in function argument
/opt/go/src/github.com/daddye/vips/vips.go:265: cannot use &tmpImage (type *__Ctype_struct__VipsImage) as type *__Ctype_VipsImage in function argument
/opt/go/src/github.com/daddye/vips/vips.go:289: cannot use image (type *_Ctype_struct__VipsImage) as type *_Ctype_VipsImage in function argument
/opt/go/src/github.com/daddye/vips/vips.go:289: cannot use &tmpImage (type *__Ctype_struct__VipsImage) as type **_Ctype_VipsImage in function argument
/opt/go/src/github.com/daddye/vips/vips.go:299: cannot use image (type *_Ctype_struct__VipsImage) as type *_Ctype_VipsImage in function argument

Memory problems in web application

I'm creating a very simple webserver in Go responsible for resizing images. However, I found out that after some time running (or under heavy loads), the server ran out of memory. After some testing, I realized something curious that only happened when a resize was done (some requests do not trigger the resize process).

After each resize, the memory usage of the Go applications was significantly increased (don't know if normal, but definitely reasonable considering that I'm working with in-memory images). The problem is that this memory was never released. Thus, after some time, it finally ate all the system memory until crashing. I guess this is a bug as, actually, changing to gographics/imagick, although slower and more difficult, has finished these memory problems.

This is the part of my code where the vips library is used:

func transform_image(image []byte, dimension int) ([]byte, error) {
    options := vips.Options{
        Width:          dimension,
        Height:         dimension,
        Interpolator:       vips.BILINEAR,
        Quality:        95,
    }

    return vips.Resize(image, options)
}

With the result (after checking for errors), I perform a writer.Write(image).

Any idea of why this could happen or solutions? Am I doing something wrong? Maybe the library is not suited for long-standing processes such as web applications?

Thanks in advance!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.