You're not saying how exactly putdata() is not behaving. I'm assuming you're doing
>>> pic.putdata(a)
Traceback (most recent call last):
File "...blablabla.../PIL/Image.py", line 1185, in putdata
self.im.putdata(data, scale, offset)
SystemError: new style getargs format but argument is not a tuple
This is because putdata expects a sequence of tuples and you're giving it a numpy array. This
>>> data = list(tuple(pixel) for pixel in pix)
>>> pic.putdata(data)
will work but it is very slow.
As of PIL 1.1.6, the "proper" way to convert between images and numpy arrays is simply
>>> pix = numpy.array(pic)
although the resulting array is in a different format than yours (3-d array or rows/columns/rgb in this case).
Then, after you make your changes to the array, you should be able to do either pic.putdata(pix) or create a new image with Image.fromarray(pix).
You're not saying how exactly putdata() is not behaving. I'm assuming you're doing
>>> pic.putdata(a)
Traceback (most recent call last):
File "...blablabla.../PIL/Image.py", line 1185, in putdata
self.im.putdata(data, scale, offset)
SystemError: new style getargs format but argument is not a tuple
This is because putdata expects a sequence of tuples and you're giving it a numpy array. This
>>> data = list(tuple(pixel) for pixel in pix)
>>> pic.putdata(data)
will work but it is very slow.
As of PIL 1.1.6, the "proper" way to convert between images and numpy arrays is simply
>>> pix = numpy.array(pic)
although the resulting array is in a different format than yours (3-d array or rows/columns/rgb in this case).
Then, after you make your changes to the array, you should be able to do either pic.putdata(pix) or create a new image with Image.fromarray(pix).
Open I as an array:
>>> I = numpy.asarray(PIL.Image.open('test.jpg'))
Do some stuff to I, then, convert it back to an image:
>>> im = PIL.Image.fromarray(numpy.uint8(I))
Source: Filter numpy images with FFT, Python
If you want to do it explicitly for some reason, there are pil2array() and array2pil() functions using getdata() on this page in correlation.zip.
python - How to convert a NumPy array to PIL image applying matplotlib colormap - Stack Overflow
Python PIL image list to numpy array help
Converting between PIL Image and 2d pixel array?
Numpy array won't convert PIL Image
Videos
Quite a busy one-liner, but here it is:
- First ensure your NumPy array,
myarray, is normalised with the max value at1.0. - Apply the colormap directly to
myarray. - Rescale to the
0-255range. - Convert to integers, using
np.uint8(). - Use
Image.fromarray().
And you're done:
from PIL import Image
from matplotlib import cm
im = Image.fromarray(np.uint8(cm.gist_earth(myarray)*255))
with plt.savefig():

with im.save():

input = numpy_image
np.uint8 -> converts to integers
convert('RGB') -> converts to RGB
Image.fromarray -> returns an image object
from PIL import Image import numpy as np PIL_image = Image.fromarray(np.uint8(numpy_image)).convert('RGB') PIL_image = Image.fromarray(numpy_image.astype('uint8'), 'RGB')
Heya all,
I am basically trying to get a numpy array of images as input for my neural network. But I seem to be unable to use asarray on my PIL images list
Code snippet
def getImages(path) :
paths = []
images = []
#path = "/home/drBunsen/Downloads/code/yadayada/
valid_images = [".jpg",".gif",".png",".tga"]
for f in os.listdir(path):
ext = os.path.splitext(f)[1]
if ext.lower() not in valid_images:
continue
#imgs.append(Image.open(os.path.join(path,f)))
paths.append(os.path.join(path,f))
paths.sort() #make sure every image is sorted correctly
for p in paths :
images.append(Image.open(os.path.join(p)))
return imagesNext I get my images and want them in a numpy array
np.asarray(getImages("path"))
np.array(getImages("path"))neihter of these work
TypeError: int() argument must be a string, a bytes-like object or a number, not 'JpegImageFile'
I can only get the following to work
np.asarray(getImages("path")[0])
np.array(getImages("path")[0])Is there anyway to just attack the whole list?
Thanks in advance.