Mark The Difference Between 2 Pictures In Python
Solution 1:
From PIL documentation:
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.
This means that you should pass a sequence, and from the documentation as well:
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.
So the problem is that you pass a 4 tuple to a function that expects a sequence of either [(x0, y0), (x1, y1)]
or [x0, y0, x1, y1]
You can wrap your 4 tuple with list()
literal to get the second option of what the function expects:
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')
Or if you want to replace the input to the first type rectangle
expects you can do the following:
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')
Post a Comment for "Mark The Difference Between 2 Pictures In Python"