2017-06-12 16 views
0

最近ピローと遊び始めました。私はPython 3.3で2枚の画像を比較しており、その違いを画像として保存したいと考えています。Pythonの2枚の画像の違いをマーク

from PIL import Image 
from PIL import ImageChops 
from PIL import ImageDraw 

file1 = '300.jpg' 
file2 = '300.jpg' 

im1 = Image.open(file1) 
im2 = Image.open(file2) 

diff = ImageChops.difference(im1, im2).getbbox() 
draw = ImageDraw.Draw(im2) 
draw.rectangle(diff) 
im2.convert('RGB').save('file3.jpg') 

しかし、私はいつもTypeError例外を取得:引数は、私はこれがdraw.rectangle(差分)私はエラーを取り除くことができますどのように で起こると考えているシーケンス

でなければなりませんか?

ありがとうございます。 PIL documentationから

+0

をお示しすることはできます完全なスタックトレースはどこにエラーがあるのか​​正確に見ることができますか?当然の –

+0

@AvihooMamka: ファイル "test.py"、13行目、 draw.rectangle(差分) ファイルで 「C:\ユーザーはJohn.Smithj \のAppData \ローカル\プログラム\ Pythonの\を\長方形でPIL \ ImageDraw.py」、行192 \ Python36 \ libには\サイト - パッケージは、 self.draw.draw_rectangle(XY、インク、0) 例外TypeError:引数には、あなたは以下の私の答えを試すことができますシーケンス –

+0

でなければなりません。 –

答えて

1

PIL.ImageDraw.Draw.rectangle(xy, fill=None, outline=None) Draws a rectangle.

Parameters:
xy – Four points to define the bounding box.

Sequence of either [(x0, y0), (x1, y1)] or [x0, y0, x1, y1] . The second point is just outside the drawn rectangle.

outline – Color to use for the outline.

fill – Color to use for the fill.

これはあなたが順序を渡す必要があることを意味し、documentationから同様:だから問題はあなたが4タプルを渡すことです

Image.getbbox()

Calculates the bounding box of the non-zero regions in the image.

Returns: The bounding box is returned as a 4-tuple defining the left, upper, right, and lower pixel coordinate. If the image is completely empty, this method returns None.

を関数は、いずれかのシーケンスを期待します[(x0, y0), (x1, y1)]または[x0, y0, x1, y1]

あなたは4タプルwi list()番目の関数が期待するものの第二の選択肢取得するリテラル:

from PIL import Image 
from PIL import ImageChops 
from PIL import ImageDraw 

file1 = '300.jpg' 
file2 = '300.jpg' 

im1 = Image.open(file1) 
im2 = Image.open(file2) 

diff = ImageChops.difference(im1, im2).getbbox() 
draw = ImageDraw.Draw(im2) 
diff_list = list(diff) if diff else [] 
draw.rectangle(diff_list) 
im2.convert('RGB').save('file3.jpg') 

それとも、最初のタイプrectangleへの入力を交換したい場合は、次の操作を行うことができます見込ん:

from PIL import Image 
from PIL import ImageChops 
from PIL import ImageDraw 

file1 = '300.jpg' 
file2 = '300.jpg' 

im1 = Image.open(file1) 
im2 = Image.open(file2) 

diff = ImageChops.difference(im1, im2).getbbox() 
draw = ImageDraw.Draw(im2) 
diff_list_tuples = >>> [diff[0:2], diff[2:]] if diff else [(None, None), (None, None)] 
draw.rectangle(diff_list) 
im2.convert('RGB').save('file3.jpg') 
+0

それは働いた。ありがとうございました! –

+1

@JohnSmith私はあなたの質問をd​​ownvoteしませんでした。私は今それを投票した。 –

関連する問題