Welcome, guest | Sign In | My Account | Store | Cart

The utility function that converts PIL image to numpy array and vice versa.

Python, 44 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""
   Copyright 2011 Shao-Chuan Wang <shaochuan.wang AT gmail.com>

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
"""

import numpy
import Image

def PIL2array(img):
    return numpy.array(img.getdata(),
                    numpy.uint8).reshape(img.size[1], img.size[0], 3)

def array2PIL(arr, size):
    mode = 'RGBA'
    arr = arr.reshape(arr.shape[0]*arr.shape[1], arr.shape[2])
    if len(arr[0]) == 3:
        arr = numpy.c_[arr, 255*numpy.ones((len(arr),1), numpy.uint8)]
    return Image.frombuffer(mode, size, arr.tostring(), 'raw', mode, 0, 1)

def main():
    img = loadImage('foo.jpg')
    arr = PIL2array(img)
    img2 = array2PIL(arr, img.size)
    img2.save('out.jpg')

if __name__ == '__main__':
    main()

2 comments

SQK 11 years ago  # | flag

Alternatively, to get a numpy array from an image use:

from PIL import Image
from numpy import array
img = Image.open("input.png")
arr = array(img)

And to get an image from a numpy array, use:

img = Image.fromarray(arr)
img.save("output.png")
sundeep 9 years, 3 months ago  # | flag

@SQK, I used your above code to get the image into an array and when I try to print the array, it prints a multidimensional array like below for one of the image that I am trying to get into array. How do I interpret this? I want to get the alpha value of each pixel in the image. So, how do I traverse the array quickly? Your help will be most appreciated.

[[[ 0 0 0 97] [ 0 0 0 157] [ 0 0 0 157] ...,

[ 0 0 0 157] [ 0 0 0 157] [ 0 0 0 157]]

[[ 0 0 0 169] [ 0 0 0 255] [ 0 0 0 255] ..., [161 147 0 255] [152 136 0 255] [143 131 0 255]]

[[ 0 0 0 157] [ 0 0 0 255] [ 0 0 0 255] ..., [181 163 0 255] [150 135 0 255] [184 166 0 255]]

..., [[254 125 0 157] [255 195 96 255] [255 255 255 255] ..., [ 60 60 59 255] [ 27 27 26 255] [ 70 82 92 255]]

[[254 125 0 157] [255 194 95 255] [255 255 255 255] ..., [ 27 27 27 255] [ 5 5 5 255] [ 74 86 96 255]]

[[255 138 0 169] [255 169 15 255] [255 169 40 255] ..., [ 70 82 92 255] [ 74 86 96 255] [ 93 108 120 255]]]