1

I am trying to convert a numpy array - containing the Red, Green, and Blue band obtained from the .TIFF file of the satellite image - into a simple .jpeg. The issue at hand is that the colours on the .jpeg are not like they should.

This is my code:

downloading_image = image * factor
im = Image.fromarray(downloading_image)
im.save("cloud_test_1.jpeg")

where image is a numpy array of shape (500,500,3) and factor is a float of 3.5/255

Here are the images of what I get and what I should be getting:

enter image description here enter image description here

4
  • Check out downloading_image.dtype, downloading_image.shape, downloading_image.min(), and downloading_image.max(). JPEGs can contain three 8-bit channels (values 0-255) so you'll have to rescale your values appropriately and select 3 bands if necessary
    – mikewatt
    Mar 25, 2022 at 19:21
  • With what factor should I rescale my arrays? I
    – Meki21
    Mar 25, 2022 at 19:36
  • As per the help center please do not include chit chat like thanks within your posts.
    – PolyGeo
    Mar 25, 2022 at 20:53
  • 255 / downloading_image.max() or 255 / the datatype's maximum
    – mikewatt
    Mar 25, 2022 at 22:18

1 Answer 1

1

Change the last part of your script as follows to take advantage of built in PIL functionality for brightening your image rather than converting the values in the array directly. I'm not sure exactly what your problem was before, but my best guess is that it was some sort of rendering issue. In any case, this should help you move forward.

## this is your original plot function
## factor 1/255 to scale between 0-1
## factor 3.5 to increase brightness
plot_image(image, factor=3.5 / 255, clip_range=(0, 1))
plt.show()


# read np array into PIL
im = Image.fromarray(image, mode="RGB")

# initialize image brightness enhancer
enhancer = ImageEnhance.Brightness(im)

#set factor
factor = 3.5  

# brightens the image by specified factor
im_output = enhancer.enhance(factor)

#plot original image
plt.imshow(im)
plt.show()

#plot brightened image
plt.imshow(im_output)
plt.show()

#write out both images
im.save("output_image.jpeg")
im_output.save('brightened-image.jpeg')

enter image description here

6
  • I tried but it still does not work. I get the exact same result as from before ...
    – Meki21
    Mar 25, 2022 at 20:56
  • Can you share the data?
    – Kartograaf
    Mar 25, 2022 at 21:23
  • I don't know how to share the data on this platform, so hence I put everything in this Google Colab file: colab.research.google.com/drive/…
    – Meki21
    Mar 25, 2022 at 21:30
  • You'll need to make the files public or accept my request for access
    – Kartograaf
    Mar 25, 2022 at 21:49
  • Invite sent via email!
    – Meki21
    Mar 25, 2022 at 21:53

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.