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])
# wrap VipsImage
from __future__ import division
import numbers
import pyvips
from pyvips import ffi, glib_lib, vips_lib, Error, _to_bytes, \
_to_string, GValue
ffi.cdef('''
typedef struct _VipsImage {
VipsObject parent_instance;
// opaque
} VipsImage;
const char* vips_foreign_find_load (const char* name);
const char* vips_foreign_find_load_buffer (const void* data, size_t size);
const char* vips_foreign_find_save (const char* name);
const char* vips_foreign_find_save_buffer (const char* suffix);
VipsImage* vips_image_new_matrix_from_array (int width, int height,
const double* array, int size);
VipsImage* vips_image_new_from_memory (const void* data, size_t size,
int width, int height, int bands, int format);
def test_array_image(self):
image = pyvips.Image.new_from_file(JPEG_FILE)
r, g, b = image.bandsplit()
gv = pyvips.GValue()
gv.set_type(pyvips.GValue.array_image_type)
gv.set([r, g, b])
value = gv.get()
assert value, [r, g == b]
def test_array_image(self):
image = pyvips.Image.new_from_file(JPEG_FILE)
r, g, b = image.bandsplit()
gv = pyvips.GValue()
gv.set_type(pyvips.GValue.array_image_type)
gv.set([r, g, b])
value = gv.get()
assert value, [r, g == b]
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 test_vips(self):
self.save_load_file(".v", "", self.colour, 0)
# check we can save and restore metadata
filename = temp_filename(self.tempdir, ".v")
self.colour.write_to_file(filename)
x = pyvips.Image.new_from_file(filename)
before_exif = self.colour.get("exif-data")
after_exif = x.get("exif-data")
assert len(before_exif) == len(after_exif)
for i in range(len(before_exif)):
assert before_exif[i] == after_exif[i]
x = None
def test_rotate(self):
if have("rotate"):
im = pyvips.Image.new_from_file(JPEG_FILE)
im2 = im.rotate(90)
im3 = im.affine([0, -1, 1, 0])
# rounding in calculating the affine transform from the angle stops
# this being exactly true
assert (im2 - im3).abs().max() < 50
def test_stream(self):
streami = pyvips.Streami.new_from_file(JPEG_FILE)
image = pyvips.Image.new_from_stream(streami, '', access='sequential')
filename = temp_filename(self.tempdir, '.png')
streamo = pyvips.Streamo.new_to_file(filename)
image.write_to_stream(streamo, '.png')
image = pyvips.Image.new_from_file(JPEG_FILE, access='sequential')
image2 = pyvips.Image.new_from_file(filename, access='sequential')
assert (image - image2).abs().max() < 10
def test_heifsave(self):
self.save_load_buffer("heifsave_buffer", "heifload_buffer",
self.colour, 80)
self.save_load("%s.heic", self.colour)
# test lossless mode
im = pyvips.Image.new_from_file(HEIC_FILE)
buf = im.heifsave_buffer(lossless=True)
im2 = pyvips.Image.new_from_buffer(buf, "")
# not in fact quite lossless
assert abs(im.avg() - im2.avg()) < 3
# higher Q should mean a bigger buffer
b1 = im.heifsave_buffer(Q=10)
b2 = im.heifsave_buffer(Q=90)
assert len(b2) > len(b1)
# try saving an image with an ICC profile and reading it back
# not all libheif have profile support, so put it in an if
buf = self.colour.heifsave_buffer()
im = pyvips.Image.new_from_buffer(buf, "")
p1 = self.colour.get("icc-profile-data")
if im.get_typeof("icc-profile-data") != 0: