Edit rgb values ​​in jpg using python

I am trying to change the RGB values ​​in a photograph using a Python image library. I use the Image.point function and it does what I want, except that I want to be able to implement another function in the values ​​of R and G. Does anyone know how I can do this?

Thanks!

+6
source share
1 answer

You are better off using numpy in addition to PIL to do the math of individual bands of the image.

As a contrived example that should not look good in any way:

 import Image import numpy as np im = Image.open('snapshot.jpg') # In this case, it a 3-band (red, green, blue) image # so we'll unpack the bands into 3 separate 2D arrays. r, g, b = np.array(im).T # Let make an alpha (transparency) band based on where blue is < 100 a = np.zeros_like(b) a[b < 100] = 255 # Random math... This isn't meant to look good... # Keep in mind that these are unsigned 8-bit integers, and will overflow. # You may want to convert to floats for some calculations. r = (b + g) * 5 # Put things back together and save the result... im = Image.fromarray(np.dstack([item.T for item in (r,g,b,a)])) im.save('output.png') 

Input enter image description here


Exit enter image description here

+5
source

Source: https://habr.com/ru/post/917061/


All Articles