[Numpy-discussion] image to array doubt

devnew at gmail.com devnew at gmail.com
Fri Feb 29 09:54:49 EST 2008


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 a-128 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



More information about the NumPy-Discussion mailing list