3d0c / imagio

Image Processing Web Service.
MIT License
90 stars 12 forks source link

Image processing web service.

Version 0.2 Changes:

It uses:

Right now it can:

It's pretty fast.

It scales well.

Installation.

Prerequisites.

1. Install GroupCache.

go get github.com/golang/groupcache

2. Building imagio.

There are two ways to build it, using the statically linked OpenCV libraries, which are attached to the release package or build it self.
If You don't have OpenCV and don't plan to use it further, just follow step 2.1.

2.1 Using statically linked OpenCV libraries from the package.

Be sure, that GOROOT and GOPATH variables are set, and run script inside the imagio directory.

~/imagio# ./build-static.sh

That's all. You will get ready to use imagio binary.

2.2 Using shared OpenCV libraries

Install OpenCV and ensure that pkgconfig file is available, add it to PKG_CONFIG_PATH if needed.

# check it
pkg-config --libs opencv
# If You see an error about 'opencv.pc', run the following command
# with corresponding opencv path:
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/opencv-2.4.7/lib/pkgconfig

In couse of this bug Bug #1925, You should patch opencv.pc by running following command:

# copy-paste it
pcPrefix=`grep "prefix=" $PKG_CONFIG_PATH/opencv.pc | grep -v exec | sed 's/prefix=//g'`;pcLibs=`grep "Libs: " $PKG_CONFIG_PATH/opencv.pc`" -L$pcPrefix/lib";sed -i.old 's#libdir=#libdir='"$pcPrefix/lib"'#g' $PKG_CONFIG_PATH/opencv.pc;sed -i.old 's#Libs:.*#'"$pcLibs"'#g' $PKG_CONFIG_PATH/opencv.pc
# Install package by running:
go get github.com/3d0c/imagio

# Run it
$GOPATH/bin/imagio

Usage.

For example:

curl -o test-800.jpg \
  http://localhost:15900/\?scale=800x\&quality=80 \
  \&source=farm5.staticflickr.com/4130/5088414872_0856bb93ed_o.jpg

You will get a downscaled to 800 px width jpeg, saved with 80% quality.

Available options:

  1. source
    Possible values:

    • http://some.host.com/1.jpg
    • some.host.com/1.jpg — scheme could be ommitted, http scheme is default
    • 1.jpg — host could be ommitted, if it given in config
    • file://some/path/1.jpg root option should be defined in config file, default is /tmp
  2. scale
    Prototype: ([0-9]+x) or (x[0-9]+) or ([0-9]+) or (0.[0-9]+)
    E.g.:

    • 800x scale to width 800px, height will be calculated
    • x600 scale to height 600px, width will be calculated
    • 640 maximum dimension is 640px, e.g. original 1024x768 pixel image will be scaled to 640x480, same option applied for 900x1600 image results 360x640
    • 0.5 50% of original dimensions, e.g. 1024x768 = 512x384
  3. crop
    Prototype: crop=x,y,width,height

    • x,y are the coordinates of top left corner of crop ROI and could be replaced by one of the following shortcuts:
    • left
    • bleft
    • right
    • bright
    • center
    • E.g:
    • &crop=15,20,200,200
    • &crop=center,500,500
  4. quality
    Jpeg quality. Integer value from 0 to 100. (more is better)

  5. format
    jpg or png. Could be omitted if no format conversion needed.

  6. method
    Scaling method. Default is Bicubic.
    Possible values:

    • 1 Nearest-neighbor interpolation
    • 2 Bilinear interpolation
    • 3 Bicubic interpolation
    • 4 Area based interpolation
    • 5 Lanczos resampling
  7. blend_with Source for image, which will blended with the source image.

  8. blend_mask Source for mask file.

  9. blend_roi (x,y) coordinates of the top left corner or one of the following shortcuts: left, bleft, right, bright, center Default is (0,0)

  10. blend_alpha Desired froreground image transparency. from 0.0 to 1.0 double. Use it only for blending two images without alpha channel. (See examples.)

imagio.conf

If You need to change some default behavior, create an imagio.conf by running:

imagio -dumpcfg

It will create a default config file in the same directory:

{
    "listen": "127.0.0.1:15900",
    "source": {
        "http": {
            "root": "",
            "default": true
        },
        "file": {
            "root": "",
            "default": false
        }
    },

    "defaults": {
        "format": "jpeg",
        "method": 3,
        "quality": 80,
        "blend_alpha": 0.5
    },

    "groupcache": {
        "self": "http://127.0.0.1:9100",
        "peers": [],
        "size": "512M"
    }
}

It's pretty straightforward. Few comments:

Watermark

To get a persistent watermark on every image add blend section to the config file. E.g.:

    "blend": {
        "with": "file://watermark.png",              // 'source->root' should be configured
        //      "http://localhost/watermark.png",    // or common http url

        "mask": "file://mask.png",
        //      "http://localhost/mask.png"

        "roi": "0,0"
    }

Note,

Blending examples

&source= &blend_with= &blend_mask= result
base 250x250 white mask result
&source= &blend_with= result
base foreground result
&source= &blend_with= result
base foreground result