Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_histfind_indexed(self):
im = pyvips.Image.black(50, 100)
test = im.insert(im + 10, 50, 0, expand=True)
index = test // 10
for x in noncomplex_formats:
for y in [pyvips.BandFormat.UCHAR, pyvips.BandFormat.USHORT]:
a = test.cast(x)
b = index.cast(y)
hist = a.hist_find_indexed(b)
assert_almost_equal_objects(hist(0, 0), [0])
assert_almost_equal_objects(hist(1, 0), [50000])
def test_premultiply(self):
for fmt in unsigned_formats + [pyvips.BandFormat.SHORT,
pyvips.BandFormat.INT] + float_formats:
mx = 255
alpha = mx / 2.0
test = self.colour.bandjoin(alpha).cast(fmt)
pixel = test(30, 30)
predict = [int(x) * alpha / mx for x in pixel[:-1]] + [alpha]
im = test.premultiply()
assert im.bands == test.bands
pixel = im(30, 30)
for x, y in zip(pixel, predict):
# we use float arithetic for int and uint, so the rounding
# differs ... don't require huge accuracy
assert abs(x - y) < 2
def test_unpremultiply(self):
for fmt in unsigned_formats + [pyvips.BandFormat.SHORT,
pyvips.BandFormat.INT] + float_formats:
mx = 255
alpha = mx / 2.0
test = self.colour.bandjoin(alpha).cast(fmt)
pixel = test(30, 30)
predict = [int(x) / (alpha / mx) for x in pixel[:-1]] + [alpha]
im = test.unpremultiply()
assert im.bands == test.bands
pixel = im(30, 30)
for x, y in zip(pixel, predict):
# we use float arithetic for int and uint, so the rounding
# differs ... don't require huge accuracy
assert abs(x - y) < 2
def run_cmplx(fn, image):
if image.format == pyvips.BandFormat.FLOAT:
new_format = pyvips.BandFormat.COMPLEX
elif image.format == pyvips.BandFormat.DOUBLE:
new_format = pyvips.BandFormat.DPCOMPLEX
else:
raise pyvips.Error("run_cmplx: not float or double")
# tag as complex, run, revert tagging
cmplx = image.copy(bands=1, format=new_format)
cmplx_result = fn(cmplx)
return cmplx_result.copy(bands=2, format=image.format)
def test_hist_plot(self):
im = pyvips.Image.identity()
im2 = im.hist_plot()
assert im2.width == 256
assert im2.height == 256
assert im2.format == pyvips.BandFormat.UCHAR
assert im2.bands == 1
assert im.height == 5
assert im.bands == 1
assert im.format == pyvips.BandFormat.DOUBLE
assert im.max() == 20
total = im.avg() * im.width * im.height
scale = im.get("scale")
assert total == scale
p = im(im.width / 2, im.height / 2)
assert p[0] == 20.0
im = pyvips.Image.gaussmat(1, 0.1,
separable=True, precision="float")
assert im.width == 5
assert im.height == 1
assert im.bands == 1
assert im.format == pyvips.BandFormat.DOUBLE
assert im.max() == 1.0
total = im.avg() * im.width * im.height
scale = im.get("scale")
assert total == scale
p = im(im.width / 2, im.height / 2)
assert p[0] == 1.0
def test_icc(self):
test = pyvips.Image.new_from_file(JPEG_FILE)
im = test.icc_import().icc_export()
self.assertLess(im.dE76(test).max(), 6)
im = test.icc_import()
im2 = im.icc_export(depth=16)
self.assertEqual(im2.format, pyvips.BandFormat.USHORT)
im3 = im2.icc_import()
self.assertLess((im - im3).abs().max(), 3)
im = test.icc_import(intent=pyvips.Intent.ABSOLUTE)
im2 = im.icc_export(intent=pyvips.Intent.ABSOLUTE)
self.assertLess(im2.dE76(test).max(), 6)
im = test.icc_import()
im2 = im.icc_export(output_profile=SRGB_FILE)
im3 = im.colourspace(pyvips.Interpretation.SRGB)
self.assertLess(im2.dE76(im3).max(), 6)
before_profile = test.get_value("icc-profile-data")
im = test.icc_transform(SRGB_FILE)
after_profile = im.get_value("icc-profile-data")
im2 = test.icc_import()
pyvips.Interpretation.SRGB,
pyvips.Interpretation.YXY]
cmyk_colourspaces = [pyvips.Interpretation.CMYK]
coded_colourspaces = [pyvips.Interpretation.LABQ]
mono_colourspaces = [pyvips.Interpretation.B_W]
sixteenbit_colourspaces = [pyvips.Interpretation.GREY16,
pyvips.Interpretation.RGB16]
all_colourspaces = colour_colourspaces + mono_colourspaces + \
coded_colourspaces + sixteenbit_colourspaces + \
cmyk_colourspaces
max_value = {pyvips.BandFormat.UCHAR: 0xff,
pyvips.BandFormat.USHORT: 0xffff,
pyvips.BandFormat.UINT: 0xffffffff,
pyvips.BandFormat.CHAR: 0x7f,
pyvips.BandFormat.SHORT: 0x7fff,
pyvips.BandFormat.INT: 0x7fffffff,
pyvips.BandFormat.FLOAT: 1.0,
pyvips.BandFormat.DOUBLE: 1.0,
pyvips.BandFormat.COMPLEX: 1.0,
pyvips.BandFormat.DPCOMPLEX: 1.0}
sizeof_format = {pyvips.BandFormat.UCHAR: 1,
pyvips.BandFormat.USHORT: 2,
pyvips.BandFormat.UINT: 4,
pyvips.BandFormat.CHAR: 1,
pyvips.BandFormat.SHORT: 2,
pyvips.BandFormat.INT: 4,
pyvips.BandFormat.FLOAT: 4,
pyvips.BandFormat.DOUBLE: 8,
pyvips.BandFormat.COMPLEX: 8,
pyvips.BandFormat.DPCOMPLEX: 16}
def test_black(self):
im = pyvips.Image.black(100, 100)
assert im.width == 100
assert im.height == 100
assert im.format == pyvips.BandFormat.UCHAR
assert im.bands == 1
for i in range(0, 100):
pixel = im(i, i)
assert len(pixel) == 1
assert pixel[0] == 0
im = pyvips.Image.black(100, 100, bands=3)
assert im.width == 100
assert im.height == 100
assert im.format == pyvips.BandFormat.UCHAR
assert im.bands == 3
for i in range(0, 100):
pixel = im(i, i)
assert len(pixel) == 3
assert_almost_equal_objects(pixel, [0, 0, 0])
def test_reduce(self):
im = pyvips.Image.new_from_file(JPEG_FILE)
# cast down to 0-127, the smallest range, so we aren't messed up by
# clipping
im = im.cast(pyvips.BandFormat.CHAR)
for fac in [1, 1.1, 1.5, 1.999]:
for fmt in all_formats:
for kernel in ["nearest", "linear",
"cubic", "lanczos2", "lanczos3"]:
x = im.cast(fmt)
r = x.reduce(fac, fac, kernel=kernel)
d = abs(r.avg() - im.avg())
self.assertLess(d, 2)
# try constant images ... should not change the constant
for const in [0, 1, 2, 254, 255]:
im = (pyvips.Image.black(10, 10) + const).cast("uchar")
for kernel in ["nearest", "linear",
"cubic", "lanczos2", "lanczos3"]:
# print "testing kernel =", kernel