I want to save image data as a list/array in memory so I can read it somewhere else. The default scripting language is Python 2.7 with numpy added so I have to do can be done with those.
1 Answer
Creates a list of tuples
from PIL import Image
img = Image.open('ubuntu.jpg')
imglist = list(img.getdata())
print imglist
For numpy
import numpy
print numpy.array(img.getdata(), numpy.uint8).reshape(img.size[1], img.size[0], 3)
8 Comments
Joan Venge
Thanks but I don't have PIL installed on the company systems.
Joan Venge
It's png or bmp, but I can save to other formats if that helps.
Paul Rooney
I think that without a library you'd be looking at opening and parsing the image files yourself. Can you run in a virtualenv and install PIL?
Joan Venge
Thanks I don't know virtualenv but basically I am not allowed to use anything other than what the application comes out of the box which is python 2.7 with numpy.
Paul Rooney
virtualenv would be ideal. I don't think its there by default, so it might not be an option. So they will let you install numpy but no other packages? It seems incredibly restrictive. You may have no option but to parse the individual image file formats you want to use. That sounds like a lot of work and I don't know how to do this.
|