0
我想比較兩張圖像並保存差異圖像,其中差異標記爲紅色。 不幸的是我得到以下錯誤:Python圖像庫:無法訪問像素值
Traceback (most recent call last): File "pythontest.py", line 216, in <module> nDiff = compare(sPathCur, sPathRef, sPathDif) File "pythontest.py", line 88, in compare pix_diff[y, x] = (255, 0, 0) TypeError: function takes exactly 1 argument (3 given)
def compare(sPathCur, sPathRef, sPathDif):
im_cur = Image.open(sPathCur)
im_ref = Image.open(sPathRef)
im_dif = im_cur.convert('L') # convert image to grey scale
delta = ImageChops.difference(im_cur, im_ref)
width, height = delta.size
pix_delta = delta.load()
pix_diff = im_dif.load()
for y in range(width):
for x in range(height):
r, g, b = pix_delta[y, x]
if (r > 0 or g > 0 or b > 0):
pix_diff[y, x] = (255, 0, 0)
im_dif.save(sPathDif)
「putpixel」方法的[docs](http://www.effbot.org/imagingbook/image.htm#tag-Image.Image.putpixel)表示:「顏色以單個數值給出,帶圖像,以及用於多波段圖像的元組「。難道你需要將元組轉換爲單個值嗎? – phd