2014-04-16 2 views
0

미리 정의 된 목록에없는 이미지에서 일부 픽셀의 색상 만 변경하는 방법은 무엇입니까? 나는이 같은 시도 : 난 그냥 일부 픽셀이 검은 만들고 싶어Python에서 픽셀 색상을 변경하는 방법

from PIL import Image 
picture = Image.open("// location") 

imshow (picture) 

_colors = [[0, 128, 0], [128, 128, 0], [128, 128, 128], [192, 128, 0], [128, 64, 0], [0, 192, 0], [128, 64, 128], [0, 0, 0]] 
width, height = picture.size 

for x in range(0, width-1): 
for y in range(0, height-1): 
    current_color = picture.getpixel((x,y)) 
    if current_color!= _colors[0] and current_color!= _colors[1] and current_color!= _colors[2] and current_color!= _colors[3] and current_color!= _colors[4] and current_color!= _colors[5] and current_color!= _colors[6] and current_color!= _colors[7]: 
    picture.putpixel((x,y), (0, 0, 0)) 

    imshow (picture) 

을하지만, 어떻게 든이 검은 색 이미지를 반환 모두

+0

"IN"으로 문이'picture.putpixel 경우 ((x, y)는 (0, 0, 0))' 안에 있으면? 들여 쓰기가 꺼져 있습니다. – WeaselFox

+0

들여 쓰기가있는 질문을 편집했습니다. – valentin

답변

0

이 줄 :

if current_color!= _colors[0] and current_color!= _colors[1] and current_color!= _colors[2] and current_color!= _colors[3] and current_color!= _colors[4] and current_color!= _colors[5] and current_color!= _colors[6] and current_color!= _colors[7]: 

항상 True을 반환 그래서 전체 그림을 반복하여 검정으로 바꿉니다. getpixel 반환 튜플은 :

>>> print picture.getpixel((1, 1)) 
    (79, 208, 248) 

당신은 목록 ([0,128,0])에 비교합니다. 그들은 동일하지 않습니다 : 튜플의 목록이 아닌 목록의 목록

>>> (1,2,3) == [1,2,3] 
False 

변화 colors.

0

같은 화소 데이터의 형태를 유지하고 단축한다는

import Image 
filename ="name.jpg" 

picture = Image.open(filename, 'r') 
_colors = [(0, 128, 0), (128, 128, 0), (128, 128, 128), (192, 128, 0), (128, 64, 0), (0, 192, 0), (128, 64, 128), (0, 0, 0)] 
width, height = picture.size 

for x in range(0, width): 
    for y in range(0, height): 
     current_color = picture.getpixel((x,y)) 
     if current_color in _colors: 
      picture.putpixel((x,y), (0, 0, 0)) 

picture.show() 
관련 문제