1

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

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

thanks for the response. I tried that already. But it's not working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.