本文整理汇总了Python中matplotlib._png.read_png_int函数的典型用法代码示例。如果您正苦于以下问题:Python read_png_int函数的具体用法?Python read_png_int怎么用?Python read_png_int使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了read_png_int函数的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: compare_pngs
def compare_pngs(actual, expected):
""" Compare two images using RMS approach.
Actual and expected are paths to PNG files.
Returns True for mismatch, False otherwise.
"""
# open the image files and remove the alpha channel (if it exists)
expectedImage = _png.read_png_int(expected)
actualImage = _png.read_png_int(actual)
expectedImage = expectedImage[:, :, :3]
actualImage = actualImage[:, :, :3]
# convert to signed integers, so that the images can be subtracted without
# overflow
expectedImage = expectedImage.astype(np.int16)
actualImage = actualImage.astype(np.int16)
num_values = np.prod(expectedImage.shape)
abs_diff_image = abs(expectedImage - actualImage)
histogram = np.bincount(abs_diff_image.ravel(), minlength=256)
sum_of_squares = np.sum(histogram * np.arange(len(histogram)) ** 2)
rms = np.sqrt(float(sum_of_squares) / num_values)
return rms
示例2: test_imread_png_uint16
def test_imread_png_uint16():
from matplotlib import _png
img = _png.read_png_int(os.path.join(os.path.dirname(__file__),
'baseline_images/test_png/uint16.png'))
assert (img.dtype == np.uint16)
assert np.sum(img.flatten()) == 134184960
示例3: compare_images
def compare_images(expected, actual, tol, in_decorator=False):
"""
Compare two "image" files checking differences within a tolerance.
The two given filenames may point to files which are convertible to
PNG via the `.converter` dictionary. The underlying RMS is calculated
with the `.calculate_rms` function.
Parameters
----------
expected : str
The filename of the expected image.
actual :str
The filename of the actual image.
tol : float
The tolerance (a color value difference, where 255 is the
maximal difference). The test fails if the average pixel
difference is greater than this value.
in_decorator : bool
If called from image_comparison decorator, this should be
True. (default=False)
Example
-------
img1 = "./baseline/plot.png"
img2 = "./output/plot.png"
compare_images( img1, img2, 0.001 ):
"""
if not os.path.exists(actual):
msg = "Output image %s does not exist." % actual
raise Exception(msg)
if os.stat(actual).st_size == 0:
msg = "Output image file %s is empty." % actual
raise Exception(msg)
verify(actual)
# Convert the image to png
extension = expected.split('.')[-1]
if not os.path.exists(expected):
raise IOError('Baseline image %r does not exist.' % expected)
if extension != 'png':
actual = convert(actual, False)
expected = convert(expected, True)
# open the image files and remove the alpha channel (if it exists)
expectedImage = _png.read_png_int(expected)
actualImage = _png.read_png_int(actual)
expectedImage = expectedImage[:, :, :3]
actualImage = actualImage[:, :, :3]
actualImage, expectedImage = crop_to_same(
actual, actualImage, expected, expectedImage)
# convert to signed integers, so that the images can be subtracted without
# overflow
expectedImage = expectedImage.astype(np.int16)
actualImage = actualImage.astype(np.int16)
rms = calculate_rms(expectedImage, actualImage)
diff_image = make_test_filename(actual, 'failed-diff')
if rms <= tol:
if os.path.exists(diff_image):
os.unlink(diff_image)
return None
save_diff_image(expected, actual, diff_image)
results = dict(rms=rms, expected=str(expected),
actual=str(actual), diff=str(diff_image))
if not in_decorator:
# Then the results should be a string suitable for stdout.
template = ['Error: Image files did not match.',
'RMS Value: {rms}',
'Expected: \n {expected}',
'Actual: \n {actual}',
'Difference:\n {diff}',
'Tolerance: \n {tol}', ]
results = '\n '.join([line.format(**results) for line in template])
return results
示例4: compare_images
def compare_images( expected, actual, tol, in_decorator=False ):
'''Compare two image files - not the greatest, but fast and good enough.
= EXAMPLE
# img1 = "./baseline/plot.png"
# img2 = "./output/plot.png"
#
# compare_images( img1, img2, 0.001 ):
= INPUT VARIABLES
- expected The filename of the expected image.
- actual The filename of the actual image.
- tol The tolerance (a color value difference, where 255 is the
maximal difference). The test fails if the average pixel
difference is greater than this value.
- in_decorator If called from image_comparison decorator, this should be
True. (default=False)
'''
verify(actual)
# Convert the image to png
extension = expected.split('.')[-1]
if not os.path.exists(expected):
raise IOError('Baseline image %r does not exist.' % expected)
if extension != 'png':
actual = convert(actual, False)
expected = convert(expected, True)
# open the image files and remove the alpha channel (if it exists)
expectedImage = _png.read_png_int( expected )
actualImage = _png.read_png_int( actual )
expectedImage = expectedImage[:, :, :3]
actualImage = actualImage[:, :, :3]
actualImage, expectedImage = crop_to_same(actual, actualImage, expected, expectedImage)
# convert to signed integers, so that the images can be subtracted without
# overflow
expectedImage = expectedImage.astype(np.int16)
actualImage = actualImage.astype(np.int16)
rms = calculate_rms(expectedImage, actualImage)
diff_image = make_test_filename(actual, 'failed-diff')
if rms <= tol:
if os.path.exists(diff_image):
os.unlink(diff_image)
return None
save_diff_image( expected, actual, diff_image )
if in_decorator:
results = dict(
rms = rms,
expected = str(expected),
actual = str(actual),
diff = str(diff_image),
)
return results
else:
# old-style call from mplTest directory
msg = " Error: Image files did not match.\n" \
" RMS Value: " + str( rms ) + "\n" \
" Expected:\n " + str( expected ) + "\n" \
" Actual:\n " + str( actual ) + "\n" \
" Difference:\n " + str( diff_image ) + "\n" \
" Tolerance: " + str( tol ) + "\n"
return msg
示例5: compare_images
def compare_images( expected, actual, tol, in_decorator=False ):
'''Compare two image files - not the greatest, but fast and good enough.
= EXAMPLE
# img1 = "./baseline/plot.png"
# img2 = "./output/plot.png"
#
# compare_images( img1, img2, 0.001 ):
= INPUT VARIABLES
- expected The filename of the expected image.
- actual The filename of the actual image.
- tol The tolerance (a color value difference, where 255 is the
maximal difference). The test fails if the average pixel
difference is greater than this value.
- in_decorator If called from image_comparison decorator, this should be
True. (default=False)
'''
verify(actual)
# Convert the image to png
extension = expected.split('.')[-1]
if not os.path.exists(expected):
raise IOError('Baseline image %r does not exist.' % expected)
if extension != 'png':
actual = convert(actual, False)
expected = convert(expected, True)
# open the image files and remove the alpha channel (if it exists)
expectedImage = _png.read_png_int( expected )
actualImage = _png.read_png_int( actual )
expectedImage = expectedImage[:, :, :3]
actualImage = actualImage[:, :, :3]
actualImage, expectedImage = crop_to_same(actual, actualImage, expected, expectedImage)
# convert to signed integers, so that the images can be subtracted without
# overflow
expectedImage = expectedImage.astype(np.int16)
actualImage = actualImage.astype(np.int16)
# compare the resulting image histogram functions
expected_version = version.LooseVersion("1.6")
found_version = version.LooseVersion(np.__version__)
# On Numpy 1.6, we can use bincount with minlength, which is much faster than
# using histogram
if found_version >= expected_version:
rms = 0
for i in xrange(0, 3):
h1p = expectedImage[:,:,i]
h2p = actualImage[:,:,i]
h1h = np.bincount(h1p.ravel(), minlength=256)
h2h = np.bincount(h2p.ravel(), minlength=256)
rms += np.sum(np.power((h1h-h2h), 2))
else:
rms = 0
ns = np.arange(257)
for i in xrange(0, 3):
h1p = expectedImage[:,:,i]
h2p = actualImage[:,:,i]
h1h = np.histogram(h1p, bins=ns)[0]
h2h = np.histogram(h2p, bins=ns)[0]
rms += np.sum(np.power((h1h-h2h), 2))
rms = calculate_rms(expectedImage, actualImage)
diff_image = make_test_filename(actual, 'failed-diff')
if rms <= tol:
if os.path.exists(diff_image):
os.unlink(diff_image)
return None
save_diff_image( expected, actual, diff_image )
if in_decorator:
results = dict(
rms = rms,
expected = str(expected),
actual = str(actual),
diff = str(diff_image),
)
return results
else:
# old-style call from mplTest directory
msg = " Error: Image files did not match.\n" \
" RMS Value: " + str( rms ) + "\n" \
" Expected:\n " + str( expected ) + "\n" \
" Actual:\n " + str( actual ) + "\n" \
#.........这里部分代码省略.........
示例6: compare_images
def compare_images( expected, actual, tol, in_decorator=False ):
'''Compare two image files - not the greatest, but fast and good enough.
= EXAMPLE
# img1 = "./baseline/plot.png"
# img2 = "./output/plot.png"
#
# compare_images( img1, img2, 0.001 ):
= INPUT VARIABLES
- expected The filename of the expected image.
- actual The filename of the actual image.
- tol The tolerance (a unitless float). This is used to
determine the 'fuzziness' to use when comparing images.
- in_decorator If called from image_comparison decorator, this should be
True. (default=False)
'''
verify(actual)
# Convert the image to png
extension = expected.split('.')[-1]
if extension != 'png':
actual = convert(actual, False)
expected = convert(expected, True)
# open the image files and remove the alpha channel (if it exists)
expectedImage = _png.read_png_int( expected )
actualImage = _png.read_png_int( actual )
actualImage, expectedImage = crop_to_same(actual, actualImage, expected, expectedImage)
# compare the resulting image histogram functions
expected_version = version.LooseVersion("1.6")
found_version = version.LooseVersion(np.__version__)
# On Numpy 1.6, we can use bincount with minlength, which is much faster than
# using histogram
if found_version >= expected_version:
rms = 0
for i in xrange(0, 3):
h1p = expectedImage[:,:,i]
h2p = actualImage[:,:,i]
h1h = np.bincount(h1p.ravel(), minlength=256)
h2h = np.bincount(h2p.ravel(), minlength=256)
rms += np.sum(np.power((h1h-h2h), 2))
else:
rms = 0
bins = np.arange(257)
for i in xrange(0, 3):
h1p = expectedImage[:,:,i]
h2p = actualImage[:,:,i]
h1h = np.histogram(h1p, bins=bins)[0]
h2h = np.histogram(h2p, bins=bins)[0]
rms += np.sum(np.power((h1h-h2h), 2))
rms = np.sqrt(rms / (256 * 3))
diff_image = make_test_filename(actual, 'failed-diff')
if ( (rms / 10000.0) <= tol ):
if os.path.exists(diff_image):
os.unlink(diff_image)
return None
save_diff_image( expected, actual, diff_image )
if in_decorator:
results = dict(
rms = rms,
expected = str(expected),
actual = str(actual),
diff = str(diff_image),
)
return results
else:
# old-style call from mplTest directory
msg = " Error: Image files did not match.\n" \
" RMS Value: " + str( rms / 10000.0 ) + "\n" \
" Expected:\n " + str( expected ) + "\n" \
" Actual:\n " + str( actual ) + "\n" \
" Difference:\n " + str( diff_image ) + "\n" \
" Tolerance: " + str( tol ) + "\n"
return msg
示例7: compare_images
def compare_images(expected, actual, tol, in_decorator=False):
"""
Compare two "image" files checking differences within a tolerance.
The two given filenames may point to files which are convertible to
PNG via the `.converter` dictionary. The underlying RMS is calculated
with the `.calculate_rms` function.
Parameters
----------
expected : str
The filename of the expected image.
actual : str
The filename of the actual image.
tol : float
The tolerance (a color value difference, where 255 is the
maximal difference). The test fails if the average pixel
difference is greater than this value.
in_decorator : bool
Determines the output format. If called from image_comparison
decorator, this should be True. (default=False)
Returns
-------
comparison_result : None or dict or str
Return *None* if the images are equal within the given tolerance.
If the images differ, the return value depends on *in_decorator*.
If *in_decorator* is true, a dict with the following entries is
returned:
- *rms*: The RMS of the image difference.
- *expected*: The filename of the expected image.
- *actual*: The filename of the actual image.
- *diff_image*: The filename of the difference image.
- *tol*: The comparison tolerance.
Otherwise, a human-readable multi-line string representation of this
information is returned.
Examples
--------
::
img1 = "./baseline/plot.png"
img2 = "./output/plot.png"
compare_images(img1, img2, 0.001)
"""
from matplotlib import _png
if not os.path.exists(actual):
raise Exception("Output image %s does not exist." % actual)
if os.stat(actual).st_size == 0:
raise Exception("Output image file %s is empty." % actual)
# Convert the image to png
extension = expected.split('.')[-1]
if not os.path.exists(expected):
raise IOError('Baseline image %r does not exist.' % expected)
if extension != 'png':
actual = convert(actual, False)
expected = convert(expected, True)
# open the image files and remove the alpha channel (if it exists)
expected_image = _png.read_png_int(expected)
actual_image = _png.read_png_int(actual)
expected_image = expected_image[:, :, :3]
actual_image = actual_image[:, :, :3]
actual_image, expected_image = crop_to_same(
actual, actual_image, expected, expected_image)
diff_image = make_test_filename(actual, 'failed-diff')
if tol <= 0:
if np.array_equal(expected_image, actual_image):
return None
# convert to signed integers, so that the images can be subtracted without
# overflow
expected_image = expected_image.astype(np.int16)
actual_image = actual_image.astype(np.int16)
rms = calculate_rms(expected_image, actual_image)
if rms <= tol:
return None
save_diff_image(expected, actual, diff_image)
results = dict(rms=rms, expected=str(expected),
actual=str(actual), diff=str(diff_image), tol=tol)
if not in_decorator:
# Then the results should be a string suitable for stdout.
template = ['Error: Image files did not match.',
#.........这里部分代码省略.........
示例8: compare_images
def compare_images( expected, actual, tol, in_decorator=False ):
'''Compare two image files - not the greatest, but fast and good enough.
= EXAMPLE
# img1 = "./baseline/plot.png"
# img2 = "./output/plot.png"
#
# compare_images( img1, img2, 0.001 ):
= INPUT VARIABLES
- expected The filename of the expected image.
- actual The filename of the actual image.
- tol The tolerance (a unitless float). This is used to
determine the 'fuzziness' to use when comparing images.
- in_decorator If called from image_comparison decorator, this should be
True. (default=False)
'''
verify(actual)
# Convert the image to png
extension = expected.split('.')[-1]
if extension != 'png':
actual = convert(actual, False)
expected = convert(expected, True)
# open the image files and remove the alpha channel (if it exists)
expectedImage = _png.read_png_int( expected )
actualImage = _png.read_png_int( actual )
actualImage, expectedImage = crop_to_same(actual, actualImage, expected, expectedImage)
# compare the resulting image histogram functions
expected_version = version.LooseVersion("1.6")
found_version = version.LooseVersion(np.__version__)
rms = calculate_rms(expectedImage, actualImage)
diff_image = make_test_filename(actual, 'failed-diff')
if ( (rms / 10000.0) <= tol ):
if os.path.exists(diff_image):
os.unlink(diff_image)
return None
# For Agg-rendered images, we can retry by ignoring pixels with
# differences of only 1
if extension == 'png':
# Remove differences of only 1
diffImage = np.abs(np.asarray(actualImage, dtype=np.int) -
np.asarray(expectedImage, dtype=np.int))
actualImage = np.where(diffImage <= 1, expectedImage, actualImage)
rms = calculate_rms(expectedImage, actualImage)
if ( (rms / 10000.0) <= tol ):
if os.path.exists(diff_image):
os.unlink(diff_image)
return None
save_diff_image( expected, actual, diff_image )
if in_decorator:
results = dict(
rms = rms,
expected = str(expected),
actual = str(actual),
diff = str(diff_image),
)
return results
else:
# old-style call from mplTest directory
msg = " Error: Image files did not match.\n" \
" RMS Value: " + str( rms / 10000.0 ) + "\n" \
" Expected:\n " + str( expected ) + "\n" \
" Actual:\n " + str( actual ) + "\n" \
" Difference:\n " + str( diff_image ) + "\n" \
" Tolerance: " + str( tol ) + "\n"
return msg