Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
p1 = self.colour.get("icc-profile-data")
p2 = im.get("icc-profile-data")
assert p1 == p2
# add tests for exif, xmp, ipct
# the exif test will need us to be able to walk the header,
# we can't just check exif-data
# we can test that exif changes change the output of webpsave
# first make sure we have exif support
z = pyvips.Image.new_from_file(JPEG_FILE)
if z.get_typeof("exif-ifd0-Orientation") != 0:
x = self.colour.copy()
x.set("orientation", 6)
buf = x.webpsave_buffer()
y = pyvips.Image.new_from_buffer(buf, "")
assert y.get("orientation") == 6
def test_load_buffer(self):
im = pyvips.Image.black(10, 20)
buf = im.write_to_buffer('.jpg')
x = pyvips.Image.new_from_buffer(buf, '')
assert x.width == 10
assert x.height == 20
assert x.bands == 1
# first make sure we have exif support
z = pyvips.Image.new_from_file(JPEG_FILE)
if z.get_typeof("exif-ifd0-Orientation") != 0:
x = self.colour.copy()
x.set("orientation", 6)
buf = x.webpsave_buffer()
y = pyvips.Image.new_from_buffer(buf, "")
assert y.get("orientation") == 6
# try converting an animated gif to webp ... can't do back to gif
# again without IM support
if have("gifload"):
x1 = pyvips.Image.new_from_file(GIF_ANIM_FILE, n=-1)
w1 = x1.webpsave_buffer(Q=10)
x2 = pyvips.Image.new_from_buffer(w1, "", n=-1)
assert x1.width == x2.width
assert x1.height == x2.height
assert x1.get("delay") == x2.get("delay")
assert x1.get("page-height") == x2.get("page-height")
assert x1.get("gif-loop") == x2.get("gif-loop")
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:
p2 = im.get("icc-profile-data")
assert p1 == p2
# to small variations
assert_almost_equal_objects(a, [71, 166, 236], threshold=2)
assert im.width == 550
assert im.height == 368
assert im.bands == 3
self.file_loader("webpload", WEBP_FILE, webp_valid)
self.buffer_loader("webpload_buffer", WEBP_FILE, webp_valid)
self.save_load_buffer("webpsave_buffer", "webpload_buffer",
self.colour, 60)
self.save_load("%s.webp", self.colour)
# test lossless mode
im = pyvips.Image.new_from_file(WEBP_FILE)
buf = im.webpsave_buffer(lossless=True)
im2 = pyvips.Image.new_from_buffer(buf, "")
assert abs(im.avg() - im2.avg()) < 1
# higher Q should mean a bigger buffer
b1 = im.webpsave_buffer(Q=10)
b2 = im.webpsave_buffer(Q=90)
assert len(b2) > len(b1)
# try saving an image with an ICC profile and reading it back ... if we
# can do it, our webp supports metadata load/save
buf = self.colour.webpsave_buffer()
im = pyvips.Image.new_from_buffer(buf, "")
if im.get_typeof("icc-profile-data") != 0:
# verify that the profile comes back unharmed
p1 = self.colour.get("icc-profile-data")
p2 = im.get("icc-profile-data")
assert p1 == p2
# 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:
p2 = im.get("icc-profile-data")
assert p1 == p2
assert x.height == page_height * 15
assert x(0, 166)[0] == 96
assert x(0, 167)[0] == 0
assert x(0, 168)[0] == 1
# pyr save to buffer added in 8.6
x = pyvips.Image.new_from_file(TIF_FILE)
buf = x.tiffsave_buffer(tile=True, pyramid=True)
filename = temp_filename(self.tempdir, '.tif')
x.tiffsave(filename, tile=True, pyramid=True)
with open(filename, 'rb') as f:
buf2 = f.read()
assert len(buf) == len(buf2)
a = pyvips.Image.new_from_buffer(buf, "", page=2)
b = pyvips.Image.new_from_buffer(buf2, "", page=2)
assert a.width == b.width
assert a.height == b.height
assert a.avg() == b.avg()
# region-shrink added in 8.7
x = pyvips.Image.new_from_file(TIF_FILE)
buf = x.tiffsave_buffer(tile=True, pyramid=True, region_shrink="mean")
buf = x.tiffsave_buffer(tile=True, pyramid=True, region_shrink="mode")
buf = x.tiffsave_buffer(tile=True, pyramid=True, region_shrink="median")
x = pyvips.Image.magickload(filename)
assert self.colour.width == x.width
assert self.colour.height == x.height
assert self.colour.bands == x.bands
max_diff = (self.colour - x).abs().max()
assert max_diff < 60
self.save_load_buffer("magicksave_buffer", "magickload_buffer",
self.colour, 60, format="JPG")
# try an animation
if have("gifload"):
x1 = pyvips.Image.new_from_file(GIF_ANIM_FILE, n=-1)
w1 = x1.magicksave_buffer(format="GIF")
x2 = pyvips.Image.new_from_buffer(w1, "", n=-1)
assert x1.get("delay") == x2.get("delay")
assert x1.get("page-height") == x2.get("page-height")
# magicks vary in how they handle this ... just pray we are close
assert abs(x1.get("gif-loop") - x2.get("gif-loop")) < 5
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
# libvips has its own sniffer for ICO, test that
with open(ICO_FILE, 'rb') as f:
buf = f.read()
im = pyvips.Image.new_from_buffer(buf, "")
assert im.width == 16
assert im.height == 16
assert x.width == 439
assert x.height == page_height * 15
assert x(0, 166)[0] == 96
assert x(0, 167)[0] == 0
assert x(0, 168)[0] == 1
# pyr save to buffer added in 8.6
x = pyvips.Image.new_from_file(TIF_FILE)
buf = x.tiffsave_buffer(tile=True, pyramid=True)
filename = temp_filename(self.tempdir, '.tif')
x.tiffsave(filename, tile=True, pyramid=True)
with open(filename, 'rb') as f:
buf2 = f.read()
assert len(buf) == len(buf2)
a = pyvips.Image.new_from_buffer(buf, "", page=2)
b = pyvips.Image.new_from_buffer(buf2, "", page=2)
assert a.width == b.width
assert a.height == b.height
assert a.avg() == b.avg()
# region-shrink added in 8.7
x = pyvips.Image.new_from_file(TIF_FILE)
buf = x.tiffsave_buffer(tile=True, pyramid=True, region_shrink="mean")
buf = x.tiffsave_buffer(tile=True, pyramid=True, region_shrink="mode")
buf = x.tiffsave_buffer(tile=True, pyramid=True, region_shrink="median")