은 :
from PIL import Image
im = Image.open("g.bmp") # The input image. Should be greyscale
out = open("out.txt", "wb") # The output.
data = im.getdata() # This will create a generator that yields
# the value of the rbg values consecutively. If
# g.bmp is a 2x2 image of four rgb(12, 12, 12) pixels,
# list(data) should be
# [(12,12,12), (12,12,12), (12,12,12), (12,12,12)]
for i in data: # Here we iterate through the pixels.
if i[0] < 10: # If r==b==g, we only really
# need one pixel (i[0] or "r")
out.write(str(i[0])+" ") # if the pixel is valid, we'll write the value. So for
# rgb(4, 4, 4), we'll output the string "4"
else:
out.write("X ") # Otherwise, it does not meet the requirements, so
# we'll output "X"
필요에 따라 조건을 조정, 어떤 이유로 r==g==b
보장되지 않습니다. 당신은 10의 평균하려는 경우, 예를 들어, 당신은 im.getdata()
것 (컬러 파일 형식으로 그레이 스케일 이미지에 반대되는) 또한 그레이 스케일 형식의 파일을 점에 유의
if sum(i) <= 30: # Equivalent to sum(i)/float(len(i)) <= 10 if we know the length is 3
처럼 뭔가 상태를 변경할 수 있습니다 단순히 그레이 레벨을 단일 값으로 반환합니다. rgb(15, 15, 15)
의 2 × 2 이미지, list(data)
출력됩니다 대신 [(4, 4, 4), (4, 4, 4), (4, 4, 4), (4, 4, 4)]
의 [4, 4, 4, 4]
에 대한 그래서. 분석 할 때이 경우, 단지 i
대신 i[0]
를 참조하면 픽셀 값을 인쇄 하시겠습니까? 너 뭐 해봤 니? – Matt
rgb 값을 얻으려면 이미지를 "RGB"로 변환하고'getpixel'을 사용하십시오. '(x, y, z) <(10,10,10)'이''(x, rgb.getpixel (i, j) c) '의' Bakuriu
@Bakuriu :. 제 뇌 일시적) (합계하여 제안 조사 틀림 그러나 확실히'getpixel()'에는 튜플 인수가 필요합니다. – eryksun