Sunday, January 25, 2009

Python: an image to an array and an array to an image

Converting 8-bit gray-scale level images to numpy array, and generating a 8-bit gray-scale level image from array.


  1. Anonymous3:12 AM

    thanks, that was useful! Can't believe there is no "to_array" routine in PIL.

  2. its not working . i m providing 8-bit gray scale bmp image and as a result i want the same 8-bit gray scale bmp image

  3. Anonymous2:45 AM

    to whom it may concern: this works with color images:
    def image2array(im):
    newArr = numpy.fromstring(im.tostring(),numpy.uint8)
    newArr = numpy.reshape(newArr,(im.size[1], im.size[0], 3))
    return newArr