How to convert between NumPy array and PIL Image

Converting a single channel PIL Image to a 1-D NumPy array and back is easy:

import numpy
import PIL

# Convert Image to array
img = PIL.Image.open("foo.jpg").convert("L")
arr = numpy.array(img)

# Convert array to Image
img = PIL.Image.fromarray(arr)

Tried with: Python 2.7.3 and Ubuntu 12.04

Advertisements

One thought on “How to convert between NumPy array and PIL Image”

  1. I am working on Python project involving Tkinter and OpenCV. I stumbled on this trick you used. Thank you very much for sharing.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s