hi i came across a codebase by rice univ people..in that there are some functions for conversion btw image and vectors 1.<code> def image_to_vector(self, filename): try: im = Image.open(filename) except IOError: print 'couldn\'t load ' + filename sys.exit(1) self.im_size = im.size a = numpy.array(im.getdata()) return a  128 </code> what is the purpose of a128 here? i couldn't quite understand 2.<code> def vector_to_image(self, v, filename): v.shape = (1,) a, b = min(v), max(v) span = max(abs(b), abs(a)) im = Image.new('L', self.im_size) im.putdata((v * 127. / span) + 128) im.save(filename) </code> and why the calculations of a,b span? why do you need the extra maths ops inside im.putdata() I couldn't quite follow it can someone explain this? thanks D
On Fri, Feb 29, 2008 at 2:54 PM, devnew@gmail.com
hi i came across a codebase by rice univ people..in that there are some functions for conversion btw image and vectors
I'm not an expert by any means but I thought I'd try and help...
1.<code> def image_to_vector(self, filename): try: im = Image.open(filename) except IOError: print 'couldn\'t load ' + filename sys.exit(1) self.im_size = im.size a = numpy.array(im.getdata()) return a  128 </code>
what is the purpose of a128 here? i couldn't quite understand
It looks to me like im.getdata() is returning an unsigned 8 bit integer (0 to 256), which they are converting to a signed integer by subtracting 128.
2.<code> def vector_to_image(self, v, filename): v.shape = (1,) a, b = min(v), max(v) span = max(abs(b), abs(a)) im = Image.new('L', self.im_size) im.putdata((v * 127. / span) + 128) im.save(filename) </code>
and why the calculations of a,b span? why do you need the extra maths ops inside im.putdata() I couldn't quite follow it
Similarly, here it looks like they are rescaling whatever is in v to an unsigned 8bit range (0256). The span gives the absolute maximum value of the input data, so v/span is rescaled to have maximum value 1, so (v * 127. / span) is the (signed) input vector rescaled to have values in the range [127,127]. Adding 128 makes unsigned again in [0,256]. I'm not sure why they would be doing this  to me it looks they might be using Image as a convenient way to store some other kind of data... HTH, Robin
devnew@gmail.com wrote:
Robin wrote I'm not sure why they would be doing this  to me it looks they might be using Image as a convenient way to store some other kind of data...
thanks Robin, I am wondering if there is a more straightforward way to do these.. especially the vector to image function D
Check out scipy.misc.pilutil.imread() and imsave()
participants (3)

Andrew Straw

devnew＠gmail.com

Robin