2012-05-30 11 views
6

Sto cercando di cambiare i valori RGB in una foto con la libreria di immagini Python. Ho usato la funzione Image.point e fa quello che voglio, tranne che voglio essere in grado di implementare una funzione diversa sui valori R the G e B. Qualcuno sa come posso fare questo?modifica i valori rgb in un jpg con python

Grazie!

risposta

5

È meglio usare numpy oltre a PIL per eseguire la matematica delle singole bande di un'immagine.

Come un esempio inventato che è non destinati a guardare bene in qualsiasi modo:

import Image 
import numpy as np 

im = Image.open('snapshot.jpg') 

# In this case, it's 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's 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') 

ingresso enter image description here


uscita enter image description here

+0

bene grande grazie per l'esempio – clifgray