I am passing the name of a .jpg file.
def split_image_into_bands(filename):
img = Image.open(filename)
data = img.getdata()
red = [(d[0], 0, 0) for d in data] # List Comprehension!
green = [(0, d[1], 0) for d in data]
blue = [(0, 0, d[2]) for d in data]
img.putdata(red)
img.save(os.path.splitext(filename)[0] + "_red.jpg")
img.putdata(green)
img.save(os.path.splitext(filename)[0] + "_green.jpg")
img.putdata(blue)
img.save(os.path.splitext(filename)[0] + "_blue.jpg")
# Put the 3 images back together
rimage = Image.new(img.mode, img.size)
rimage.putdata(red)
gimage = Image.new(img.mode, img.size)
gimage.putdata(green)
bimage = Image.new(img.mode, img.size)
bimage.putdata(blue)
# Error on the following line: "ValueError: mode mismatch"
img = Image.merge(img.mode, (rimage, gimage, bimage)) # Second argument is a tuple
img.save(os.path.splitext(filename)[0] + "_merged.jpg")
The code works up to the merge function. Then it throws a "ValueError: mode mismatch"