4,444
社区成员
发帖
与我相关
我的任务
分享
from PIL import Image
def adjust_img(img, threshhold, minlevel, maxlevel):
newimg = img.copy()
width, height = newimg.size
for i, px in enumerate(img.getdata()):
r,g,b = px
if all((mn < v < mx) for mn, v, mx in zip(minlevel, px, maxlevel)) and abs(r-g) < threshhold and abs(r-b)<threshhold and abs(g-b)<threshhold:
newimg.putpixel((i%width, i/width), (r/2, g/2, (b-30)/2))
return combine(img, newimg)
# put two images side by side for visual comparision
def combine(img1, img2):
w, h = img1.size
img = Image.new('RGB', (w, h*2))
img.paste(img1, (0,0))
img.paste(img2, (0, h))
return img
img2 = adjust_img(img, 40, (0,0,0), (150, 150, 200))