muto
A server and corresponding client library for ImageMagick-based image manipulation and conversion based on Wand for the ImageMagick bindings, Flask for the web server, Requests for the HTTP communication and boto for storing the resulting images on Amazon S3.
Concept
The idea behind muto is that you have a muto server sitting somewhere on a server in the “cloud” that does the heavy lifting, and connect to that server from your application code through the thin muto client. You tell the client where to get the source image from (currently that needs to be a public URL), define some commands (e.g. resize, crop, rotate) and send it off to the server. The server will then get the source image from the specified URL, apply the requested commands, upload the resulting image to an S3 bucket, and return some metadata including the URL pointing to the resulting image.
Installation
You can install the package from PyPI using pip or easy_install:
$ pip install muto
For the muto server, you need to install its dependencies:
$ pip install boto Flask requests Wand
For the muto client, you need these dependencies:
$ pip install requests
Or you can install from the latest source version:
$ git clone git://github.com/philippbosch/muto.git
$ cd muto/
$ python setup.py install
Setting up a muto server
The muto server is implemented as a Flask Blueprint. In
order to setup your own server create a file called server.py
:
import os
from flask import Flask
from muto.server import muto_server
app = Flask(__name__)
app.register_blueprint(muto_server)
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port, debug=True)
Since muto server stores the resulting images in an S3 bucket it expects the following environment variables to be set:
-
AWS_ACCESS_KEY_ID
, get this from Amazon Console IAM -
AWS_SECRET_ACCESS_KEY
, get this from Amazon Console IAM -
AWS_STORAGE_BUCKET_NAME
, set this to the name of a bucket you created on S3
Then run the server like this:
$ python server.py
If you want it to listen on a port other than 5000, try this:
$ PORT=8888 python server.py
Quick install for a Heroku-based muto server
$ mkdir mymuto
$ cd mymuto
$ git init
$ virtualenv venv
$ . venv/bin/activate
$ pip install muto
$ pip install boto Flask gunicorn requests Wand
$ pip freeze > requirements.txt
$ curl https://gist.github.com/philippbosch/5949419/raw/server.py > server.py
$ echo python-2.7.4 > runtime.txt
$ echo web: gunicorn server:app > Procfile
$ heroku create mymuto
$ git add .
$ git commit -m "initial commit"
$ heroku config add AWS_ACCESS_KEY_ID=<your aws access key id>
$ heroku config add AWS_SECRET_ACCESS_KEY=<your aws secret access key>
$ heroku config add AWS_STORAGE_BUCKET_NAME=<your S3 bucket>
Using the muto client
Say you have a source image at https://muto-tests.s3.amazonaws.com/test.jpg
that you want to resize to 960×540 and convert to a PNG.
from muto.client import MutoClient
API_ENDPOINT = 'http://127.0.0.1:5000/api/v1' # Set this to your server
client = MutoClient(API_ENDPOINT)
client.from_url('https://muto-tests.s3.amazonaws.com/test.jpg')
client.resize(960, 540)
client.format = 'png'
image = client.process()
print image.url # => http://the-url-to-the-resulting-image.png
Supported commands and properties
muto client tries to map the methods and properties exposed by Wand on
the wand.image
object as closely as possible. Currently supported:
Properties
Methods
crop(left=0, top=0, right=None, bottom=None, width=None, height=None, reset_coords=True)
flip()
flop()
liquid_rescale(width, height, delta_x=0, rigidity=0)
resize(width=None, height=None, filter='undefined', blur=1)
rotate(degree, background=None, reset_coords=True)
transform(crop='', resize='')
Please see the Wand documentation for details.