简体   繁体   中英

saving a picture with the RGB values in Python with PIL

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.

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.

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])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM