python

2012-05-30 16 views
6

के साथ एक जेपीजी में आरजीबी मान संपादित करें मैं पाइथन इमेजिंग लाइब्रेरी के साथ एक तस्वीर में आरजीबी मूल्यों को बदलने की कोशिश कर रहा हूं। मैं फ़ंक्शन Image.point का उपयोग कर रहा हूं और यह वही करता है जो मैं चाहता हूं सिवाय इसके कि मैं आर जी और बी मानों पर एक अलग फ़ंक्शन को लागू करने में सक्षम होना चाहता हूं। कोई भी जानता है कि मैं यह कैसे कर सकता हूं?python

धन्यवाद!

उत्तर

5

आप छवि के व्यक्तिगत बैंड के गणित करने के लिए पीआईएल के अतिरिक्त numpy का उपयोग करना बेहतर कर रहे हैं।

एक काल्पनिक उदाहरण है कि नहीं किसी भी तरह से अच्छे लग रहे करने के लिए होती है:

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

इनपुट enter image description here


आउटपुट enter image description here

+0

ठीक महान उदाहरण के लिए धन्यवाद – clifgray