Reputation: 305
So I'm trying to create a program that needs to save pictures with the RGB values. This is just a simple test I've set up to make sure I can open, get the data, then save the image with the data again:
inputIm = Image.open(args.target)
#denoises image and saves it to disk
#data = DenoiseImage(inputIm, factor, iters)
data = np.array(inputIm.getdata())
n, m = inputIm.size
print data
outputIm = Image.new("RGB", (n, m))
outputIm.putdata(data)
outputIm.save(args.fileName)
I get this error when I run it:
Traceback (most recent call last):
File "DenoiseNumpyWeave.py", line 253, in <module>
outputIm.putdata(data)
File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 1215, in putdata
self.im.putdata(data, scale, offset)
SystemError: new style getargs format but argument is not a tuple
However, the print data line DOES print out an array with tuples in it, so I don't really understand what I'm doing wrong. I've done the exact same thing with black/white pictures ("L" instead of "RGB"), and put in an array without tuples (single values instead), and that worked.
Upvotes: 1
Views: 3535
Reputation: 308166
Numpy has converted the tuples provided by getdata
into arrays. You need to convert them back to tuples for putdata
to work.
outputIm.putdata([tuple(pixel) for pixel in data])
Upvotes: 0
Reputation: 15349
Your black-and-white image had a single channel ("L") so an n-by-m array worked just fine. An RGB image has 3 channels though, so you may need an n-by-m-by-3 array.
Upvotes: 1