82

I've recently come across a problem which requires at least a basic degree of image processing, can I do this in Python, and if so, with what?

akdom
  • 32,264
  • 27
  • 73
  • 79
  • 4
    Realtime image processing using PyPy: http://morepypy.blogspot.com/2011/07/realtime-image-processing-in-python.html – Mikhail Poda Oct 15 '11 at 11:57

8 Answers8

53

The best-known library is PIL. However if you are simply doing basic manipulation, you are probably better off with the Python bindings for ImageMagick, which will be a good deal more efficient than writing the transforms in Python.

Jim
  • 72,985
  • 14
  • 101
  • 108
  • 7
    I think you have that backward: PIL provides only the absolute most basic barebones functionality, but it does integrate nicely to Python GUIs. IM is tremendously more full-featured but requires some manual work trading buffers back and forth to integrate with a GUI. – SilverbackNet Aug 10 '11 at 02:48
  • 19
    Let's put it this way, sad but **PIL seams to be abandon-ware** and it doesn't even load BMP files created with Paint. If you check project web-page you'll figure it out, look for bug tracker (none), mailing list activity, SCM, ... – sorin Oct 03 '11 at 16:07
  • 19
    [Pillow](https://pypi.python.org/pypi/Pillow/2.1.0) is a fork of PIL that is being maintained and developed. It's probably better to use it instead. – David Johnstone Sep 06 '13 at 10:45
46

Depending on what you mean by "image processing", a better choice might be in the numpy based libraries: mahotas, scikits.image, or scipy.ndimage. All of these work based on numpy arrays, so you can mix and match functions from one library and another.

I started the website http://pythonvision.org which has more information on these.

luispedro
  • 6,934
  • 4
  • 35
  • 45
  • "The scikits.image SciKit (toolkit for SciPy) extends scipy.ndimage to provide a versatile set of image processing routines." I wonder why they didn't just contribute directly to ndimage. – endolith Nov 23 '11 at 03:52
  • 1
    All these packages have different internal structures. At least my package, mahotas, took a lot of code from ndimage, though. – luispedro Nov 25 '11 at 16:35
  • `scikit-image` is now hosted at http://scikit-image.org – Stefan van der Walt Feb 27 '13 at 15:54
  • `scipy.ndimage` link is dead. Current link, most probably: http://scipy.github.io/devdocs/tutorial/ndimage.html – kmonsoor Feb 07 '16 at 20:30
19

You also have an approach to image processing based on "standard" scientific modules: SciPy has a whole package dedicated to image processing: scipy.ndimage. Scipy is in effect the standard general numerical calculations package; it is based on the de facto standard array-manipulation module NumPy: images can also be manipulated as array of numbers. As for image display, Matplotlib (also part of the "science trilogy") makes displaying images quite simple.

SciPy is still actively maintained, so it's a good investment for the future. Furthermore, SciPy currently runs with Python 3 too, while the Python Imaging Library (PIL) does not.

Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260
  • For future reference, ndimage requires PIL anyway. – davidtbernal Nov 20 '11 at 08:44
  • 1
    @notJim: does it? PIL is not listed in the dependencies of SciPy by MacPorts, and I can do `import scipy` with no PIL install (`import Image` fails with `ImportError`). – Eric O. Lebigot Nov 20 '11 at 09:05
  • 2
    Yes, when I tried to read an image using ImRead, I got the error “raise ImportError("Could not import the Python Imaging Library (PIL)"” – davidtbernal Nov 20 '11 at 09:11
  • 2
    @notJim: It is true that SciPy relies on PIL for image reading (I tried with a "simple" JPEG file, and it failed in the same was as in your example). SciPy can still do image *processing* without PIL (PIL is an *optional* dependency). – Eric O. Lebigot Nov 20 '11 at 09:16
11

To complete the list: opencv http://opencv.willowgarage.com/documentation/python/index.html

Piti Ongmongkolkul
  • 2,110
  • 21
  • 20
7

There's also pycairo, which might be more suitable depending on your needs.

sgraham
  • 113
  • 1
  • 5
6

There is actually a wonderful Python Imaging Library (PIL). It gives you the ability to alter existing images, including anti-aliasing capabilities, and create new images with text and such. You can also find a decent introductory tutorial in the PIL handbook provided on the aforementioned site.

akdom
  • 32,264
  • 27
  • 73
  • 79
  • 9
    Did you use Google at all before asking this question? :-) I'm not saying that you _should_, just that it seems a bit funny to answer your own question in less than a minute. – Alexandra Franks Sep 18 '08 at 17:52
  • 5
    The whole idea of this site is to provide (question,answer) pairs which will serve as useful information for someone searching on the subject. It is encouraged that if you find out something useful you did not know before, and it isn't already on the site, that you share it with StackOverflow. – akdom Sep 18 '08 at 18:17
  • 8
    From the FAQ: It's also perfectly fine to ask and answer your own programming question, but pretend you're on Jeopardy: phrase it in the form of a question. Although, I think this question could easily be answered by anyone in need quickly through a google search... – Martin W Sep 18 '08 at 18:36
  • 5
    @MartinW - I found this question via google search – Bulwersator Nov 18 '13 at 13:24
4

If you are creating a custom image processing effect, you may find PythonPixels useful. http://halfhourhacks.blogspot.com/2008/03/pythonpixels.html It is intended for writing and experimenting with image processing.

moltenform
  • 793
  • 7
  • 13
3

VIPS should be fast and uses multiple CPUs:

https://github.com/libvips/libvips/wiki/Speed-and-memory-use

jcupitt
  • 10,213
  • 2
  • 23
  • 39
guettli
  • 25,042
  • 81
  • 346
  • 663