How do I save an image file from data manipulated using image.load()?
This is my code used to merge two pictures of the same size
from PIL import Image
import random
image1 = Image.open("me.jpg")
image2 = Image.open("otherme.jpg")
im1 = image1.load()
im2 = image2.load()
width, height = image1.size
newimage = Image.new("RGB",image1.size)
newim = newimage.load()
xx = 0
yy = 0
while xx < width:
while yy < height:
if random.randint(0,1) == 1:
newim[xx,yy] = im1[xx,yy]
else:
newim[xx,yy] = im2[xx,yy]
yy = yy+1
xx = xx+1
newimage.putdata(newim)
newimage.save("new.jpg")
When I run it I get this error though.
Traceback (most recent call last):
File "/home/dave/Desktop/face/squares.py", line 27, in <module>
newimage.putdata(newim)
File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 1215, in putdata
self.im.putdata(data, scale, offset)
TypeError: argument must be a sequence
Isn’t the dictionary from using .load() a sequence? I can’t find anyone else on google having this problem.
The
dictionary(which isn’t really a dictionary) returned byloadis the data in the image. You don’t have to reload it withputdata. Just remove that line.Also, use a
forloop instead of awhileloop:Now there’s no need to initialize and increment
xxandyy.You could even use
itertools.product: