本文整理汇总了Python中PIL.ImageChops.difference方法的典型用法代码示例。如果您正苦于以下问题:Python ImageChops.difference方法的具体用法?Python ImageChops.difference怎么用?Python ImageChops.difference使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PIL.ImageChops
的用法示例。
在下文中一共展示了ImageChops.difference方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: compare_images
# 需要导入模块: from PIL import ImageChops [as 别名]
# 或者: from PIL.ImageChops import difference [as 别名]
def compare_images(img1, img2):
"""Calculate the difference between two images of the same size
by comparing channel values at the pixel level.
`delete_diff_file`: removes the diff image after ratio found
`diff_img_file`: filename to store diff image
Adapted from Nicolas Hahn:
https://github.com/nicolashahn/diffimg/blob/master/diffimg/__init__.py
"""
# Don't compare if images are of different modes or different sizes.
if (img1.mode != img2.mode) \
or (img1.size != img2.size) \
or (img1.getbands() != img2.getbands()):
return None
# Generate diff image in memory.
diff_img = ImageChops.difference(img1, img2)
# Calculate difference as a ratio.
stat = ImageStat.Stat(diff_img)
diff_ratio = sum(stat.mean) / (len(stat.mean) * 255)
return diff_ratio * 100
示例2: test_draw_bell_state
# 需要导入模块: from PIL import ImageChops [as 别名]
# 或者: from PIL.ImageChops import difference [as 别名]
def test_draw_bell_state():
return circuit.H @ circuit.Id(1) >> circuit.CX
# def test_Diagram_to_gif():
# file = 'EckmannHilton.gif'
# path_ref = os.path.join(IMG_FOLDER, file)
# path_test = os.path.join(IMG_FOLDER, '.' + file)
#
# step0 = Box('s0', Ty(), Ty()) @ Box('s1', Ty(), Ty())
# step1 = next(step0.normalize())
# Diagram.to_gif(
# step0, step1,
# loop=True, margins=(0.1, 0.1), figsize=(3, 3),
# path=path_test)
#
# img_ref = Image.open(path_ref).convert('RGB')
# img_test = Image.open(path_test).convert('RGB')
# assert ImageChops.difference(img_ref, img_test).getbbox() is None
# os.remove(path_test)
示例3: analyze_difference_smartly
# 需要导入模块: from PIL import ImageChops [as 别名]
# 或者: from PIL.ImageChops import difference [as 别名]
def analyze_difference_smartly(img):
"Make an evaluation of a difference image"
result_flag = False
if not os.path.exists(img):
print('Could not locate the image to analyze the difference smartly: %s'%img)
else:
my_image = Image.open(img)
#Not an ideal line, but we dont have any enormous images
pixels = list(my_image.getdata())
pixels = [1 for x in pixels if x!=0]
num_different_pixels = sum(pixels)
print('Number of different pixels in the result image: %d'%num_different_pixels)
#Rule 1: If the number of different pixels is <10, then pass the image
#This is relatively safe since all changes to objects will be more than 10 different pixels
if num_different_pixels < 10:
result_flag = True
return result_flag
示例4: _compute_diff_box
# 需要导入模块: from PIL import ImageChops [as 别名]
# 或者: from PIL.ImageChops import difference [as 别名]
def _compute_diff_box(cls, a, b, round_to=2):
'''
Find the four coordinates giving the bounding box of differences between a and b
making sure they are divisible by round_to
Parameters
----------
a : PIL.Image
The first image
b : PIL.Image
The second image
round_to : int
The multiple to align the bbox to
'''
box = ImageChops.difference(a, b).getbbox()
if box is None:
return None
return cls._round_bbox(box, round_to)
示例5: test_transforms
# 需要导入模块: from PIL import ImageChops [as 别名]
# 或者: from PIL.ImageChops import difference [as 别名]
def test_transforms(self):
settings = get_settings(IMAGE_PROCESS=self.transforms)
def test_transform(d, i, tmpdir):
path, name = os.path.split(i)
destination = os.path.join(tmpdir, d, name)
image = (i, destination, settings["IMAGE_PROCESS"][d])
process_image(image, settings)
transformed = Image.open(destination)
expected_path = os.path.join(path, "results", d, name)
expected = Image.open(expected_path)
img_diff = ImageChops.difference(transformed, expected).getbbox()
self.assertEqual(img_diff, None)
with temporary_folder() as tmpdir:
[
test_transform(d, i, tmpdir)
for d in self.transforms
for i in TEST_IMAGES
]
示例6: analyze_difference_smartly
# 需要导入模块: from PIL import ImageChops [as 别名]
# 或者: from PIL.ImageChops import difference [as 别名]
def analyze_difference_smartly(img):
"Make an evaluation of a difference image"
result_flag = False
if not os.path.exists(img):
print('Could not locate the image to analyze the difference smartly: %s'%img)
else:
my_image = Image.open(img)
#Not an ideal line, but we dont have any enormous images
pixels = list(my_image.getdata())
pixels = [1 for x in pixels if x!=0]
num_different_pixels = sum(pixels)
print('Number of different pixels in the result image: %d'%num_different_pixels)
#Rule 1: If the number of different pixels is <10, then pass the image
#This is relatively safe since all changes to objects will be more than 10 different pixels
if num_different_pixels < 10:
result_flag = True
return result_flag
示例7: ela
# 需要导入模块: from PIL import ImageChops [as 别名]
# 或者: from PIL.ImageChops import difference [as 别名]
def ela(filename, output_path):
print "****ELA is in BETA****"
if magic.from_file(filename, mime=True) == "image/jpeg":
quality_level = 85
tmp_img = os.path.join(output_path,os.path.basename(filename)+".tmp.jpg")
ela = os.path.join(output_path,os.path.basename(filename)+".ela.jpg")
image = Image.open(filename)
image.save(tmp_img, 'JPEG', quality=quality_level)
tmp_img_file = Image.open(tmp_img)
ela_image = ImageChops.difference(image, tmp_img_file)
extrema = ela_image.getextrema()
max_diff = max([ex[1] for ex in extrema])
scale = 255.0/max_diff
ela_image = ImageEnhance.Brightness(ela_image).enhance(scale)
ela_image.save(ela)
os.remove(tmp_img)
else:
print "ELA works only with JPEG"
#Modified version of a gist by: https://github.com/erans
示例8: assertImagesAreEqual
# 需要导入模块: from PIL import ImageChops [as 别名]
# 或者: from PIL.ImageChops import difference [as 别名]
def assertImagesAreEqual(self, current, expected, diff_tolerance=0.001):
"""Checks if both images are similar enough to be considered equal.
Similarity is controlled by the ```diff_tolerance``` argument."""
from PIL import Image, ImageChops
if isinstance(current, str):
current = Image.open(current)
if isinstance(expected, str):
expected = Image.open(expected)
diff = ImageChops.difference(expected, current)
black_pixels = _get_black_pixels(diff)
total_pixels = diff.size[0] * diff.size[1]
similarity_ratio = black_pixels / total_pixels
self.assertTrue(
1 - similarity_ratio < diff_tolerance,
'The images are different by {}%'.format((1 - similarity_ratio) * 100) +
' which is more than the allowed {}%'.format(diff_tolerance * 100))
示例9: get_diff_at_quality
# 需要导入模块: from PIL import ImageChops [as 别名]
# 或者: from PIL.ImageChops import difference [as 别名]
def get_diff_at_quality(photo, quality):
"""Return a difference score for this JPEG image saved at the specified quality
A SSIM score would be much better, but currently there is no pure Python
implementation available.
"""
diff_photo = BytesIO()
# optimize is omitted here as it doesn't affect
# quality but requires additional memory and cpu
photo.save(diff_photo, format="JPEG", quality=quality, progressive=True)
diff_photo.seek(0)
diff_score = compare_images(photo, Image.open(diff_photo))
# print("================> DIFF1 == DIFF2? ", diff_score==diff_score2)
if diff_score < 0:
return -1 + diff_score / 100
else:
return 1 - diff_score / 100
示例10: test_size
# 需要导入模块: from PIL import ImageChops [as 别名]
# 或者: from PIL.ImageChops import difference [as 别名]
def test_size(font_awesome, image, size):
"""Test size option"""
original_file = os.path.join(BASE_DIR, 'files', image)
font_awesome.export_icon(icon='rocket', size=size)
exported_file = os.path.join('exported', 'rocket.png')
img1 = Image.open(original_file)
img2 = Image.open(exported_file)
# Check dimensions
assert img1.size == (size, size)
assert img2.size == (size, size)
# Check if the images are equal
assert ImageChops.difference(img1, img2).getbbox() is None
示例11: pixel_diff
# 需要导入模块: from PIL import ImageChops [as 别名]
# 或者: from PIL.ImageChops import difference [as 别名]
def pixel_diff(image_a, image_b):
"""
Calculates a black/white image containing all differences between the two input images.
:param image_a: input image A
:param image_b: input image B
:return: a black/white image containing the differences between A and B
"""
if image_a.size != image_b.size:
raise ImageCompareException(
"different image sizes, can only compare same size images: A=" + str(image_a.size) + " B=" + str(
image_b.size))
if image_a.mode != image_b.mode:
raise ImageCompareException(
"different image mode, can only compare same mode images: A=" + str(image_a.mode) + " B=" + str(
image_b.mode))
diff = ImageChops.difference(image_a, image_b)
diff = diff.convert('L')
return diff
示例12: verify_data
# 需要导入模块: from PIL import ImageChops [as 别名]
# 或者: from PIL.ImageChops import difference [as 别名]
def verify_data(client, data, vis, prefix=''):
res_path = './tests/resources/'
assert data['input_file_name']
assert data['predict_probs']
if data['has_output']:
assert data['output_file_names']
i = 1
for filename in data['output_file_names']:
actual_image = client.get(url_for('api.download_outputs', filename=filename)).data
actual_processed_input = Image.open(io.BytesIO(actual_image))
expected_processed_input = Image.open(res_path + vis.__name__ + '/' + prefix + 'output/' + str(i) + '.png')
assert ImageChops.difference(actual_processed_input, expected_processed_input).getbbox() is None
i += 1
if data['has_processed_input']:
assert data['processed_input_file_name']
filename = data['processed_input_file_name']
actual_image = client.get(url_for('api.download_outputs', filename=filename)).data
actual_processed_input = Image.open(io.BytesIO(actual_image))
expected_processed_input = Image.open(res_path + vis.__name__ + '/' + prefix + 'pre/default.png')
assert ImageChops.difference(actual_processed_input, expected_processed_input).getbbox() is None
示例13: image
# 需要导入模块: from PIL import ImageChops [as 别名]
# 或者: from PIL.ImageChops import difference [as 别名]
def image(self, im):
for p in range(8):
pr = self.im.crop((0,8*p,128,8*p+8)).transpose(Image.ROTATE_270)
bb = im.crop((0,8*p,128,8*p+8)).transpose(Image.ROTATE_270)
diff = ImageChops.difference(pr, bb)
di = diff.getbbox()
if di is not None:
(x0, y0, x1, y1) = di
self.command(COLUMNADDR)
self.command(y0)
self.command(y1 - 1)
self.command(PAGEADDR)
self.command(p)
self.command(p + 1)
self.i2.start(self.a, 0)
self.i2.write([0x40])
self.i2.write(bb.tobytes()[y0:y1])
self.i2.stop()
self.im = im
self.command(DISPLAYON)
示例14: get_mask
# 需要导入模块: from PIL import ImageChops [as 别名]
# 或者: from PIL.ImageChops import difference [as 别名]
def get_mask(raw_pil, clean_pil):
raw = raw_pil.convert("L")
clean = clean_pil.convert("L")
mask = ImageChops.difference(raw, clean)
return mask
示例15: trim
# 需要导入模块: from PIL import ImageChops [as 别名]
# 或者: from PIL.ImageChops import difference [as 别名]
def trim(im, bg=None):
if bg is None:
bg = get_bg(im)
diff = ImageChops.difference(im, bg)
bbox = diff.getbbox()
if bbox:
return im.crop(bbox), bbox