Home > other >  Convert RGB values to jpg Image in Python
Convert RGB values to jpg Image in Python

Time:03-10

This is my first question in stack overflow, so be kind.

  1. I have an Image uploaded using PIL.
  2. The size of the Image is x,y.
  3. I have managed to create a x*y Matrix storing the RGB values of every pixel.
  4. For clarity I provide only a part of the first Row of the Image.

image_RGB = [[[18, 18, 20], [12, 12, 14], [7, 7, 9], [9, 9, 11], [15, 15, 17], [15, 15, 17], [8, 8, 10], [1, 1, 3], [6, 6, 8], [6, 6, 8], [6, 6, 8], [7, 7, 9], [7, 7, 9], [8, 8, 10], ...]]]

I would like to:

  • Reconstruct the Image from the RGB values.
  • Save the reconstructed Image as a jpg file.

Is there any way to do this?

I have a thought of:

  • Creating a new jpg Image using

    reconstructed_image = Image.new('RGB', (x, y))

  • Changing reconstructed_image's pixel RGB values with the ones of the image_RGB.

However I am not so experienced so I ask for advice. Yours Sincerely, SioU.

CodePudding user response:

You can use PIL again:

from PIL import Image
import numpy as np
image_RGB = np.array([[[18, 18, 20], [12, 12, 14], [7, 7, 9], [9, 9, 11], [15, 15, 17], [15, 15, 17], [8, 8, 10], [1, 1, 3], [6, 6, 8], [6, 6, 8], [6, 6, 8], [7, 7, 9], [7, 7, 9], [8, 8, 10]]])
image = Image.fromarray(image_RGB.astype('uint8')).convert('RGB')
image.save('image.jpg')

CodePudding user response:

Thank you for your accurate and immediate response.

I did as you wrote above.

  • image was reconstructed.
  • image was saved. However, it looks different than the original.

How could this be fixed?

I Provide you with the original and the saved one.

Original image

Reconstructed image

  • Related