Python
From VipsWiki
Contents |
Introduction
The Python interface to VIPS is a very simple one generated automatically by SWIG from our C++ API, so it's a bit rough, not well tested, and not very Python-esque. It's also very large: the VIPS library is about a megabyte, the Python wrapping is about 3 MB. We've not tried building the Python interface on Windows yet so there may be some issues there. It works fine on OS X and Linux.
PIL is the usual Python imaging library. VIPS is a bit faster and (usually) needs a lot less RAM. We have some timings of VIPS vs. PIL on the Speed and Memory Use page.
VIPS has an operation database listing all operations and their arguments. A better way to make the bulk of the Python binding would be to intercept VImage.something (...), look up something in the database, convert the arguments and call the operation.
It'd be nice to add operator overloading as well. For example, nip2 allows arbitrary C-style expressions such as:
a = (0xff00 & (b << 8)) | (b & 0xff)
to swap the top and bottom bytes in a 16-bit image. No reason you couldn't do that in Python too. VIPS's chaining makes this sort of thing efficient.
Documentation
See the C++ and Python section of the VIPS Manual. We have a page with some Python Examples as well.
Additional notes:
Lists and std::vector
The C++ std::vector<> becomes a simple list in Python. For example, the C++
VImage::lin (std::vector<float>a, std::vector<float> b)
which calculates out = in * a + b with separate values of a and b for each band of in, can be called as:
out = in.lin ([1,2,3], [4,5,6])
The binding also knows about std::vector<int> and std::vector<VImage>.
Command-line arguments
When you start up the VIPS binding it will look for any VIPS-related command-line arguments. So you can enter:
myprog.py fred.tif jim.jpg --vips-concurrency=2
You can also use the IM_CONCURRENCY environment variable. Try myprog.py --help to see all the supported arguments.
Type conversions
C++-style casts are wrapped as convert_destination-type. For example, you can convert a mask (a matrix) to an image with:
im = mask.convert_VImage()
