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_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_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:
def test_hist_entropy(self):
im = pyvips.Image.new_from_file(JPEG_FILE).extract_band(1)
ent = im.hist_find().hist_entropy()
self.assertAlmostEqual(ent, 4.37, places=2)
# test compressed zip output
filename2 = temp_filename(self.tempdir, '.zip')
self.colour.dzsave(filename2, compression=-1)
# before 8.5.8, you needed a gc on pypy to flush small zip output to
# disc
if not pyvips.base.at_least_libvips(8, 6):
gc.collect()
assert os.path.exists(filename2)
assert os.path.getsize(filename2) < os.path.getsize(filename)
# test suffix
filename = temp_filename(self.tempdir, '')
self.colour.dzsave(filename, suffix=".png")
x = pyvips.Image.new_from_file(filename + "_files/10/0_0.png")
assert x.width == 255
# test overlap
filename = temp_filename(self.tempdir, '')
self.colour.dzsave(filename, overlap=200)
y = pyvips.Image.new_from_file(filename + "_files/10/1_1.jpeg")
assert y.width == 654
# test tile-size
filename = temp_filename(self.tempdir, '')
self.colour.dzsave(filename, tile_size=512)
y = pyvips.Image.new_from_file(filename + "_files/10/0_0.jpeg")
assert y.width == 513
assert y.height == 513
self.colour, 0)
self.save_load_file(".tif", "[tile]", self.colour, 0)
self.save_load_file(".tif", "[tile,pyramid]", self.colour, 0)
self.save_load_file(".tif",
"[tile,pyramid,compression=jpeg]", self.colour, 80)
self.save_load_file(".tif", "[bigtiff]", self.colour, 0)
self.save_load_file(".tif", "[compression=jpeg]", self.colour, 80)
self.save_load_file(".tif",
"[tile,tile-width=256]", self.colour, 10)
filename = temp_filename(self.tempdir, '.tif')
x = pyvips.Image.new_from_file(TIF_FILE)
x = x.copy()
x.set("orientation", 2)
x.write_to_file(filename)
x = pyvips.Image.new_from_file(filename)
y = x.get("orientation")
assert y == 2
filename = temp_filename(self.tempdir, '.tif')
x = pyvips.Image.new_from_file(TIF_FILE)
x = x.copy()
x.set("orientation", 2)
x.write_to_file(filename)
x = pyvips.Image.new_from_file(filename)
y = x.get("orientation")
assert y == 2
x.remove("orientation")
filename = temp_filename(self.tempdir, '.tif')
x.write_to_file(filename)
x = pyvips.Image.new_from_file(filename)
def from_gdal_dataset(cls, dataset, band):
"""
Creates a new 1-band pyvips.Image from `dataset` at `band`
dataset: GDAL Dataset
band: Number of the band, starting from 1
"""
with LibVips.disable_warnings():
filename = dataset.GetFileList()[0]
image1 = Image.new_from_file(filename)
# Extract the band
image2 = image1.extract_band((band - 1), n=1)
# Cast to the right datatype, if necessary
datatype = dataset.GetRasterBand(band).NumPyDataType
if VImageAdapter(image2).NumPyType() == datatype:
return image2
types = dict((v, k) for k, v in cls.NUMPY_TYPES.items())
image3 = Image.new_from_memory(image2.write_to_memory(),
width=image2.width,
height=image2.height,
bands=1, format=types[datatype])
image3._buf = image2
return image3
"""
# xy image, vertical scaled to 360 degrees
xy = pyvips.Image.xyz(image.width, image.height)
xy *= [1, 360.0 / image.height]
index = xy.rect()
# scale to image rect
scale = min(image.width, image.height) / float(image.width)
index *= scale / 2.0
index += [image.width / 2.0, image.height / 2.0]
return image.mapim(index)
a = pyvips.Image.new_from_file(sys.argv[1])
a = to_polar(a)
a = to_rectangular(a)
a.write_to_file(sys.argv[2])