سؤال

I'm trying to convert .tiff's to an array, so that I can add images together pixel to pixel and do several other operations on them. I'm using numpy.fromstring() and im.tostring to convert.

def image2array(im):
  newArr = numpy.fromstring(im.tostring(),numpy.uint16)
  newArr2 = numpy.reshape(newArr,im.size)
  return newArr2

According to the documentation, tostring returns a string containing pixel data, using the standard "raw" encoder. And fromstring creates an image memory from pixel data in a string, using the standard "raw" decoder.

The code works, but I don't quite understand how the raw image encoder works, though. Is the array produced the string data, something like data from bits that can be decoded into an image? Or is it the actual pixel values? I'm trying to get the actual pixel values, so should I be using im.getdata()?

هل كانت مفيدة؟

المحلول 2

Yes. You should be using getdata(). You can create your array straight from using it.

This post gives a good example how to use numpy and PIL together.

نصائح أخرى

Probably there is an easier way to achieve what you have in mind via numpy.array(pilIm) and PIL.Image.fromarray(numpyArray): NumPy, PIL adding an image

Check out tifffile.py in case you want to load compressed tiff files, too: http://code.google.com/p/pylibtiff/

edit: numpy.array instead of numpy.asarray

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top