Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@pytest.mark.skipif(not pyvips.at_least_libvips(8, 5),
reason="requires libvips >= 8.5")
def test_get_fields(self):
im = pyvips.Image.black(10, 10)
fields = im.get_fields()
# we might add more fields later
assert len(fields) > 10
assert fields[0] == 'width'
def test_hist_local(self):
im = pyvips.Image.new_from_file(JPEG_FILE)
im2 = im.hist_local(10, 10)
assert im.width == im2.width
assert im.height == im2.height
assert im.avg() < im2.avg()
assert im.deviate() < im2.deviate()
if pyvips.at_least_libvips(8, 5):
im3 = im.hist_local(10, 10, max_slope=3)
assert im.width == im3.width
assert im.height == im3.height
assert im3.deviate() < im2.deviate()
def test_gifload(self):
def gif_valid(im):
a = im(10, 10)
assert_almost_equal_objects(a, [33])
assert im.width == 159
assert im.height == 203
assert im.bands == 1
self.file_loader("gifload", GIF_FILE, gif_valid)
self.buffer_loader("gifload_buffer", GIF_FILE, gif_valid)
# 'n' param added in 8.5
if pyvips.at_least_libvips(8, 5):
x1 = pyvips.Image.new_from_file(GIF_ANIM_FILE)
x2 = pyvips.Image.new_from_file(GIF_ANIM_FILE, n=2)
assert x2.height == 2 * x1.height
page_height = x2.get("page-height")
assert page_height == x1.height
x2 = pyvips.Image.new_from_file(GIF_ANIM_FILE, n=-1)
assert x2.height == 5 * x1.height
x2 = pyvips.Image.new_from_file(GIF_ANIM_FILE, page=1, n=-1)
assert x2.height == 4 * x1.height
@pytest.mark.skipif(not pyvips.at_least_libvips(8, 5),
reason="requires libvips >= 8.5")
def test_thumbnail(self):
im = pyvips.Image.thumbnail(JPEG_FILE, 100)
assert im.height == 100
assert im.bands == 3
assert im.bands == 3
# the average shouldn't move too much
im_orig = pyvips.Image.new_from_file(JPEG_FILE)
assert abs(im_orig.avg() - im.avg()) < 1
# make sure we always get the right width
for height in range(440, 1, -13):
im = pyvips.Image.thumbnail(JPEG_FILE, height)
assert im.height == height
# perhaps
# assert im.width == width * 2
# assert im.height == height * 2
# all-frames should load every frame of the animation
# (though all-frames is deprecated)
im = pyvips.Image.magickload(GIF_ANIM_FILE)
width = im.width
height = im.height
im = pyvips.Image.magickload(GIF_ANIM_FILE, all_frames=True)
assert im.width == width
assert im.height == height * 5
# page/n let you pick a range of pages
# 'n' param added in 8.5
if pyvips.at_least_libvips(8, 5):
im = pyvips.Image.magickload(GIF_ANIM_FILE)
width = im.width
height = im.height
im = pyvips.Image.magickload(GIF_ANIM_FILE, page=1, n=2)
assert im.width == width
assert im.height == height * 2
page_height = im.get("page-height")
assert page_height == height
# should work for dicom
im = pyvips.Image.magickload(DICOM_FILE)
assert im.width == 128
assert im.height == 128
# some IMs are 3 bands, some are 1, can't really test
# assert im.bands == 1
#
# * in API mode, we must have 8.6+ and use set_blob_free to
# attach the metadata to avoid leaks
# * pre-8.6, we just pass a NULL free pointer and live with the
# leak
#
# this is because in API mode you can't pass a builtin (what
# vips_lib.g_free() becomes) as a parameter to ffi.callback(), and
# vips_value_set_blob() needs a callback for arg 2
#
# additionally, you can't make a py def which calls g_free() and
# then use the py def in the callback, since libvips will trigger
# these functions during cleanup, and py will have shut down by
# then and you'll get a segv
if at_least_libvips(8, 6):
vips_lib.vips_value_set_blob_free(self.gvalue,
memory, len(value))
else:
if pyvips.API_mode:
vips_lib.vips_value_set_blob(self.gvalue,
ffi.NULL, memory, len(value))
else:
vips_lib.vips_value_set_blob(self.gvalue,
glib_lib.g_free,
memory, len(value))
else:
raise Error('unsupported gtype for set {0}, fundamental {1}'.
format(type_name(gtype), type_name(fundamental)))
exist. See :class:`GValue`.
Args:
name (str): The name of the piece of metadata to get the type of.
Returns:
The ``GType``, or 0.
Raises:
None
"""
# on libvips before 8.5, property types must be fetched separately,
# since built-in enums were reported as ints
if not at_least_libvips(8, 5):
gtype = super(Image, self).get_typeof(name)
if gtype != 0:
return gtype
return vips_lib.vips_image_get_typeof(self.pointer, _to_bytes(name))
def fetch(self, x, y, w, h):
"""Fill a region with pixel data.
Pixels are filled with data!
Returns:
Pixel data.
Raises:
:class:`.Error`
"""
if not at_least_libvips(8, 8):
raise Error('libvips too old')
psize = ffi.new('size_t *')
pointer = vips_lib.vips_region_fetch(self.pointer, x, y, w, h, psize)
if pointer == ffi.NULL:
raise Error('unable to fetch from region')
pointer = ffi.gc(pointer, glib_lib.g_free)
return ffi.buffer(pointer, psize[0])
def width(self):
"""Width of pixels held by region."""
if not at_least_libvips(8, 8):
raise Error('libvips too old')
return vips_lib.vips_region_width(self.pointer)
would fetch the image orientation.
Args:
name (str): The name of the piece of metadata to get.
Returns:
The metadata item as a Python value.
Raises:
:class:`.Error`
"""
# with old libvips, we must fetch properties (as opposed to
# metadata) via VipsObject
if not at_least_libvips(8, 5):
gtype = super(Image, self).get_typeof(name)
if gtype != 0:
return super(Image, self).get(name)
gv = GValue()
result = vips_lib.vips_image_get(self.pointer, _to_bytes(name),
gv.pointer)
if result != 0:
raise Error('unable to get {0}'.format(name))
return gv.get()