Reputation: 35
I have split a jpeg image into r,g,b and I converted them into numpy arrays. Then I changed the pixel values of r,g,b. Now I want to merge these three into one jpeg and save it. My original image is a 1024 * 500 image. If someone can give me an idea, it will be a great help
im =Image.open("new_image.jpg")
r,g,b=im.split()
r=np.array(r)
g=np.array(g)
b=np.array(b)
Then I changed the values of the pixels. I want to merge the resulting r,g,b. Thanks in advance
Upvotes: 1
Views: 1779
Reputation: 879561
To convert a PIL Image to a NumPy array:
img = Image.open(FILENAME).convert('RGB')
arr = np.array(img)
r, g, b = arr[:,:,0], arr[:,:,1], arr[:,:,2]
...
To convert r
, g
, b
(2-dimensional) NumPy arrays of dtype uint8 to a PIL Image:
arr = np.dstack([r, g, b])
img = Image.fromarray(arr, 'RGB')
This works because Image.fromarray
can create a PIL Image from any object that supports the NumPy array interface.
Upvotes: 2
Reputation: 222541
Based on this document (page 4 in the end), you can do this with merge:
r, g, b = im.split()
im = Image.merge("RGB", (b, g, r))
Upvotes: 1