2012-05-30 3 views
6

Ich versuche, die RGB-Werte in einem Foto mit der Python Imaging Library zu ändern. Ich habe die Funktion Image.point benutzt und es macht was ich will, außer dass ich eine andere Funktion auf den R-, G- und B-Werten implementieren kann. Weiß jemand wie ich das machen kann?Edit rgb Werte in einem JPG mit Python

Danke!

Antwort

5

Sie sind besser dran mit numpy zusätzlich zu PIL für die Berechnung der einzelnen Bänder eines Bildes.

Als konstruiertes Beispiel, das nicht gemeint gut aussehen in irgendeiner Weise:

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

Eingang enter image description here


Ausgabe enter image description here

+0

okay, großen Dank für das Beispiel – clifgray