Skip to content

You can create a imageserver of your own using this project. Image processor will be using mod_wsgi, Pillow-SIMD package for dynamically resizing a given image based on bandwidth and size of destination device. You can also force or request a no aspectloss based WXH while requesting the same.

Notifications You must be signed in to change notification settings

vishnuprasadh/imgprocessor

Repository files navigation

DYNAMIC IMAGE CONVERTOR

PURPOSE

Given a Image filename with screensize, bandwidth the program generates an optimal image output. The image output can be:

a. absolute filepath as json which can be used in ajax calls.

b. The output of resized image as Bytes

c. The entire image as bytes for a given url passed with content-type as image/jpeg

Primary intent of this program is to embed this and make it imageserver to generate or ouput image files dynamically. It provides capability to resize image when WXH is passed in querystring with option to forcefit the size and return or return the best fit option with no aspect ratio loss.

If you dont want to resize but just optimize for a given screensize of rendering with bandwidth in 2g, 3g etc - it optimizes image quality using Pillow-SIMD and returns the output.

The program supports option to configure runenvironment in multiple mode: 1 Standalone mode - Using mod_wsgi-express start-server wsgiimagehandler.py --options command you can run the program in standalone imgae servermode. 2. Updating the httpd.conf and httpd-vhosts.conf for virtualhost entry or run as independant handler for apache requests. 3. Mixed mode : Combination of 1 and 2 where you run wsgiimagehandler in mod_wsgi and for all other requests use apache httpd.conf.

In addition you can very easily create an nginx or varnish front end to cache/edge cache enable the outcomes.

Program can be run as commandline to just get output file or JSON handler to get path of image or Imagehandler to output content as image in bytes.

Primary case used is Imagehandler using wsgiimagehandler.py

Sample URL http://groc-images.com/images/name=vegetables.jpg&width=500&height=200&fsize=1 QueryString: Provide filename of image as firstparameter, width of screen as second param, height as third. fsize actually force sizes the input WXH when given as 1 which means your aspect ratio is lost. Default is no aspect ratio loss. All these params outside of name of file are optional.

Header values:

SCREENWIDTH i.e. only width e.g. 720 or 1024 etc and NW_TYPE i.e. bandwidth as third parameter i.e. 2g,3g,4g or * in case of default.

Secondary case of Imagehandler through modules of imgjsonhandler or cmdimgprocessor.

'--file', required=True, help='The name of image file to generate')

'--size' ,required=False, default=740 ,help='The size of client device, defaults to 740')

'-b', '--bandwidth',required=False, default='*' ,help='The bandwidth of device, defaults to high bandwidth.You can give 2g,3g,4g or *')

'-r', '--returnpath',required=False, default=False, help='If you need full returnpath provide true here' )

Note that in this case parameters of WXH, fsize which was available in Imagehandler is not yet supported.

FILES

  1. imgprocessor

The file which actually generates resized thumbnails based on screensize, bandwidth saves it as per the root configuration path in config file and returns the file path.

  1. imagehandler

The file which does very similar to imgprocessor excecpt that instead of saving it locally, it returns the Byte of the image which can directly be applied to or just output as images.

  1. imgjsonhandler

This internally uses imgprocessor and returns the output as json. You can wrap this with Content-Type:application/json and render it to frontend or directly call from ajax, parse and use it.

  1. wsgiimagehandler As the name suggests this is a wsgi program which runs on 8051 port and returns image data i.e. content-type:image/jpeg.Internally this uses imagehandler to get the bytes and render back. This looks for 3 key parameters as input with following constraints: a. /image/ pattern in url b. filename as querystring to find the name of the jpeg. In case you want to pass full path name then in config file set path: i.e. empty path: set. c. it looks for keys in httpheader in the request which are screensize and nw_type. Screensize can be 320,360,480,540,5;1080,720,640,240 and the network can be 2g,3g,4g or blank. If both screensize and network is not passed, the system assumes 320 as screen and 2g as network to give the most optimal output.

Queryparams: Width, height, fsize fsize =1 or 0 => 1 means will forcefit and ignore aspect ratio loss else will give best fit ratio with aspect maintained width = actual width we need height = actual height we need

DEPENDENCY

Run setup.py to ensure your dependencies are installed. The key dependencies are:

pip install Pillow-SIMD
pip install logging
pip install configparser
pip install setuptools
pip install mod_wsgi

Post above, goto httpd.conf in apache and enable for mod_cgi.so and also uncomment loadmodule for httpd-vhosts in case of virtualhost configuration.

TIP: Use this library with media asset randomization for max.benefit.

INSTALL Do not run installer in case you want to use this as mod_wsgi python script with apache for imagehandling. Instead copy the files into folder and run the pip commands given in dependency section(sudo access may be required) and then standalone python comamnd as given in section 3 Section 1: Python2 - Install as libraries and then run it as batch or standalone app

sudo python setup.py install 

Section 2: Python3- Install as libraries and then run it as batch or standalone app

python3 setup.py install

Section 3: Modcgi Mode To just test , you can run:

python wsgiimagehandler.py

To enable it through proper configuration etc in prodcution environment or SIT etc.

mod_wsgi-express start-server wsgiimagehandler.py --threads 20 --processes 1 --log-directory /usr/home/logs/imageserver/

You can also add WSGIDaemonProcess config which i will detail out later.

RUN TOOLS

Option A: CommandLine

python imgprocessor 'nasa.jpeg' 720 '2g'

Option B: Import

import imgprocessor
def gen():
  img = imgprocessor()
  img.generate('nasa.jpeg',720,'2g')

USAGE

You can either run this as a standalone python command by executing python commandline or hook this up with apache to dynamically render and return the images. you can also import imgprocessor and call generate method

About

You can create a imageserver of your own using this project. Image processor will be using mod_wsgi, Pillow-SIMD package for dynamically resizing a given image based on bandwidth and size of destination device. You can also force or request a no aspectloss based WXH while requesting the same.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages