Mailing List Archive

PIL-object to NumPy
Hi,

I'm using Python Image Library (PIL) and NumPy.

I would like to open a image and make som imageprocessing on it. I would
like to do the imageprocessing i NumPy, but I can't find out how to open af
Image in NumPy??

Q1: How do I open a image so I can use it with NumPy??

Q2: If not(Q1) then: How do I convert a PIL-object to a NymPy-array???

Mads Andresen, Odense, Denmark
PIL-object to NumPy [ In reply to ]
This is a multi-part message in MIME format.
--------------BB6054F9DFB97ADE973444C9
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Mads Andresen wrote:

> Hi,
>
> I'm using Python Image Library (PIL) and NumPy.
>
> I would like to open a image and make som imageprocessing on it. I would
> like to do the imageprocessing i NumPy, but I can't find out how to open af
> Image in NumPy??
>
> Q1: How do I open a image so I can use it with NumPy??
>
> Q2: If not(Q1) then: How do I convert a PIL-object to a NymPy-array???
>
> Mads Andresen, Odense, Denmark

I use the attached piece of code.

Ed Jones


--------------BB6054F9DFB97ADE973444C9
Content-Type: text/plain; charset=us-ascii;
name="numpil.py"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="numpil.py"

#! /usr/bin/python

import Numeric, Image

# NumPy thinks in terms of matrices (r,c) starting at the upper left hand
# corner.
def printarray(array, width=4):
rows, cols = array.shape
print width * ' ',
for c in range(cols):
print '%*d' % (width, c),
print
for r in range(rows):
print '%*d' % (width, r),
for c in range(cols):
print '%*d' % (width, array[r][c]),
print
print

# PIL thinks in terms of television rasters (x,y) starting at the upper left
# hand corner.
def printim(im, width=4):
xsize, ysize = im.size
print width * ' ',
for x in range(xsize):
print '%*d' % (width, x),
print
for y in range(ysize):
print '%*d' % (width, y),
for x in range(xsize):
print '%*d' % (width, im.getpixel( (x, y) )),
print
print

def pil2numpy(im, typecode='b'):
# tostring does something funny with '1' images (packs em tight).
# For 'P' images, the image data is not pased through the palette.
if im.mode != 'L' and im.mode != 'P':
print 'im.mode must be "L" or "P"'
raise 'terminate'
xsize = im.size[0]
ysize = im.size[1]
m = im.tostring()
t = Numeric.fromstring(m, 'b')
tt = Numeric.asarray(t, typecode)
# Note that ysize is first:
return Numeric.reshape(tt, (ysize, xsize))

# Convert a 2-d array with typecode 'b' to an image with mode 'L'
def numpy2pil(arr):
rows = arr.shape[0]
cols = arr.shape[1]
m = arr.tostring()
out = Image.new('L', (cols, rows) )
out.fromstring(m)
return out

--------------BB6054F9DFB97ADE973444C9--
PIL-object to NumPy [ In reply to ]
On Tue, 4 May 1999 07:52:31 +0200, "Mads Andresen" <laura@post.cybercity.dk>
wrote:

>Hi,
>
>I'm using Python Image Library (PIL) and NumPy.
>
>I would like to open a image and make som imageprocessing on it. I would
>like to do the imageprocessing i NumPy, but I can't find out how to open af
>Image in NumPy??
>
>Q1: How do I open a image so I can use it with NumPy??
>
>Q2: If not(Q1) then: How do I convert a PIL-object to a NymPy-array???
>
>Mads Andresen, Odense, Denmark
>
>
Mads,

Heres a little demo I put together for a co-worker with the same questions:



import Image
import Numeric

def imageToArray(img, type=Numeric.UnsignedInt8):
'''Given an 8bit Image object, return an Array object.
'''
arr = Numeric.array(img.tostring('raw', img.mode, 0, -1)).astype(type)
arr.shape = img.size[1], img.size[0]
return arr

def arrayToImage(arr, type='L'):
'''Given an Array object, return an 8bit Image object.
'''
height, width = arr.shape
img = Image.new(type, (width, height))
img.fromstring(arr.tostring(), 'raw', type, 0, -1)
return img

if __name__ == '__main__':
import os
import ImageChops

os.chdir('C:/CRI/Bin/Test')

# 8bit gray-scale
# test img->arr->img
img = Image.open('test_8bit.bmp')
arr = imageToArray(img)
testImg = arrayToImage(arr, 'L')
diffImg = ImageChops.difference(img, testImg)
for i in range(diffImg.size[0]):
for j in range(diffImg.size[1]):
val = diffImg.getpixel((i,j))
if val:
raise ValueError('Pixel %d,%d = %d, should be zero!'
% (i,j,val))

# test arr->img->arr
def func(i,j):
return (i*j + i + j) % 256
arr = Numeric.fromfunction(func, (128, 256)).astype(Numeric.UnsignedInt8)
img = arrayToImage(arr, 'L')
testArr = imageToArray(img)
diffArr = arr - testArr
for i in range(diffArr.shape[0]):
for j in range(diffArr.shape[1]):
if diffArr[i,j]:
raise ValueError('Element %d,%d = %d, should be zero!'
% (i,j,diffArr[i,j]))


Cheers,

Roger Burnham
Cambridge Research & Instrumentation
rburnham@cri-inc.com
http://www.cri-inc.com/
http://starship.python.net/crew/roger/
PGP Key: http://www.nai.com/default_pgp.asp
PGP Fingerprint: 5372 729A 9557 5F36 177F 084A 6C64 BE27 0BC4 CF2D
PIL-object to NumPy [ In reply to ]
> Hi,
>
> I'm using Python Image Library (PIL) and NumPy.
>
> I would like to open a image and make som imageprocessing on it. I would
> like to do the imageprocessing i NumPy, but I can't find out how to open af
> Image in NumPy??
>
> Q1: How do I open a image so I can use it with NumPy??

I have had the same questions in the past. It would be really nice to
have some way to access the underlying data as both a PIL object and a
NumPy array object.

But, as it stands now you must use the tostring method of the PIL object
and the fromstring function in the Numeric module.

Best,

Travis
PIL-object to NumPy [ In reply to ]
Thanks a lot - now i am up and running.

Mads, Odense, Denmark