一寸照白底改紅底

使用 python3 的 PIL 庫,使用其中 Image.open 方法打開,然後使用 getpixel() 方法獲取像素信息,若爲白色則使用 putpixel() 改爲紅色。
但選區不會選,只能大致改一下,若有解決方法請指教 ^_^

from PIL import Image as image
img = image.open("d:\\2.jpg")
p = img.getpixel( tuple(map(lambda x:x//10, img.size)) )
w,h = img.size
for x in range(w-1):
    for y in range(h-1):
        try:
            if img.getpixel( (x,y) ) > (240,240,240) and img.getpixel( (x,y+1) ) > (240,240,240) and img.getpixel( (x,y-1) ) > (240,240,240) and img.getpixel( (x+1,y) ) > (240,240,240) and img.getpixel( (x-1,y) ) > (240,240,240):
                img.putpixel( (x,y), (255,0,0) )
        except:
            img.putpixel( (x,y), (255,0,0) )
img.save("3.jpg")
img.show()
print("successful convernt!")

(本文會繼續更新)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章