2015-02-16 17:03:39 +01:00
|
|
|
#!/usr/bin/python
|
2016-08-19 10:15:34 +02:00
|
|
|
# vim: set fileencoding=utf-8 :
|
2015-02-16 17:03:39 +01:00
|
|
|
|
|
|
|
from __future__ import division
|
|
|
|
import unittest
|
|
|
|
import math
|
2015-02-17 16:04:15 +01:00
|
|
|
import os
|
2015-09-10 15:07:48 +02:00
|
|
|
import shutil
|
2016-05-24 11:15:54 +02:00
|
|
|
from tempfile import NamedTemporaryFile
|
2015-02-16 17:03:39 +01:00
|
|
|
|
|
|
|
#import logging
|
|
|
|
#logging.basicConfig(level = logging.DEBUG)
|
|
|
|
|
2016-05-17 15:08:46 +02:00
|
|
|
import gi
|
|
|
|
gi.require_version('Vips', '8.0')
|
2015-02-16 17:03:39 +01:00
|
|
|
from gi.repository import Vips
|
|
|
|
|
|
|
|
Vips.leak_set(True)
|
|
|
|
|
|
|
|
# an expanding zip ... if either of the args is not a list, duplicate it down
|
|
|
|
# the other
|
|
|
|
def zip_expand(x, y):
|
|
|
|
if isinstance(x, list) and isinstance(y, list):
|
|
|
|
return list(zip(x, y))
|
|
|
|
elif isinstance(x, list):
|
|
|
|
return [[i, y] for i in x]
|
|
|
|
elif isinstance(y, list):
|
|
|
|
return [[x, j] for j in y]
|
|
|
|
else:
|
|
|
|
return [[x, y]]
|
|
|
|
|
|
|
|
class TestForeign(unittest.TestCase):
|
|
|
|
# test a pair of things which can be lists for approx. equality
|
|
|
|
def assertAlmostEqualObjects(self, a, b, places = 4, msg = ''):
|
|
|
|
#print 'assertAlmostEqualObjects %s = %s' % (a, b)
|
|
|
|
for x, y in zip_expand(a, b):
|
|
|
|
self.assertAlmostEqual(x, y, places = places, msg = msg)
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.matlab_file = "images/sample.mat"
|
2016-08-19 10:15:34 +02:00
|
|
|
self.jpeg_file = "images/йцук.jpg"
|
2015-02-16 17:03:39 +01:00
|
|
|
self.png_file = "images/sample.png"
|
|
|
|
self.tiff_file = "images/sample.tif"
|
|
|
|
self.profile_file = "images/sRGB.icm"
|
|
|
|
self.analyze_file = "images/t00740_tr1_segm.hdr"
|
|
|
|
self.gif_file = "images/cramps.gif"
|
|
|
|
self.webp_file = "images/1.webp"
|
|
|
|
self.exr_file = "images/sample.exr"
|
|
|
|
self.fits_file = "images/WFPC2u5780205r_c0fx.fits"
|
|
|
|
self.openslide_file = "images/CMU-1-Small-Region.svs"
|
2016-02-08 21:39:39 +01:00
|
|
|
self.pdf_file = "images/ISO_12233-reschart.pdf"
|
2016-02-10 15:40:52 +01:00
|
|
|
self.cmyk_pdf_file = "images/cmyktest.pdf"
|
2016-02-09 12:01:12 +01:00
|
|
|
self.svg_file = "images/vips-profile.svg"
|
2016-06-26 23:59:58 +02:00
|
|
|
self.svgz_file = "images/vips-profile.svgz"
|
2016-08-01 15:57:33 +02:00
|
|
|
self.svg_gz_file = "images/vips-profile.svg.gz"
|
2016-08-04 16:55:06 +02:00
|
|
|
self.gif_anim_file = "images/cogs.gif"
|
2016-08-14 19:39:40 +02:00
|
|
|
self.dicom_file = "images/dicom_test_image.dcm"
|
2015-02-16 17:03:39 +01:00
|
|
|
|
|
|
|
self.colour = Vips.Image.jpegload(self.jpeg_file)
|
|
|
|
self.mono = self.colour.extract_band(1)
|
2016-10-28 17:49:02 +02:00
|
|
|
# we remove the ICC profile: the RGB one will no longer be appropriate
|
|
|
|
self.mono.remove("icc-profile-data")
|
2015-02-16 17:03:39 +01:00
|
|
|
self.rad = self.colour.float2rad()
|
2016-10-28 17:49:02 +02:00
|
|
|
self.rad.remove("icc-profile-data")
|
2016-01-04 15:18:10 +01:00
|
|
|
self.cmyk = self.colour.bandjoin(self.mono)
|
2015-02-16 17:03:39 +01:00
|
|
|
self.cmyk = self.cmyk.copy(interpretation = Vips.Interpretation.CMYK)
|
2016-10-28 17:49:02 +02:00
|
|
|
self.cmyk.remove("icc-profile-data")
|
2015-02-16 17:03:39 +01:00
|
|
|
|
2015-04-11 23:06:13 +02:00
|
|
|
im = Vips.Image.new_from_file(self.gif_file)
|
2016-07-26 18:28:54 +02:00
|
|
|
self.onebit = im > 128
|
2015-04-11 23:06:13 +02:00
|
|
|
|
2015-02-16 17:03:39 +01:00
|
|
|
# we have test files for formats which have a clear standard
|
|
|
|
def file_loader(self, loader, test_file, validate):
|
|
|
|
im = Vips.call(loader, test_file)
|
|
|
|
validate(self, im)
|
|
|
|
im = Vips.Image.new_from_file(test_file)
|
|
|
|
validate(self, im)
|
|
|
|
|
|
|
|
def buffer_loader(self, loader, test_file, validate):
|
|
|
|
with open(test_file, 'rb') as f:
|
|
|
|
buf = f.read()
|
|
|
|
|
|
|
|
im = Vips.call(loader, buf)
|
|
|
|
validate(self, im)
|
|
|
|
im = Vips.Image.new_from_buffer(buf, "")
|
|
|
|
validate(self, im)
|
|
|
|
|
|
|
|
def save_load(self, format, im):
|
|
|
|
x = Vips.Image.new_temp_file(format)
|
|
|
|
im.write(x)
|
|
|
|
|
|
|
|
self.assertEqual(im.width, x.width)
|
|
|
|
self.assertEqual(im.height, x.height)
|
|
|
|
self.assertEqual(im.bands, x.bands)
|
|
|
|
max_diff = (im - x).abs().max()
|
|
|
|
self.assertEqual(max_diff, 0)
|
|
|
|
|
2015-02-17 16:04:15 +01:00
|
|
|
def save_load_file(self, filename, options, im, thresh):
|
|
|
|
# yuk!
|
|
|
|
# but we can't set format parameters for Vips.Image.new_temp_file()
|
|
|
|
im.write_to_file(filename + options)
|
|
|
|
x = Vips.Image.new_from_file(filename)
|
|
|
|
|
|
|
|
self.assertEqual(im.width, x.width)
|
|
|
|
self.assertEqual(im.height, x.height)
|
|
|
|
self.assertEqual(im.bands, x.bands)
|
|
|
|
max_diff = (im - x).abs().max()
|
|
|
|
self.assertTrue(max_diff <= thresh)
|
|
|
|
x = None
|
|
|
|
os.unlink(filename)
|
|
|
|
|
2015-05-05 10:40:29 +02:00
|
|
|
def save_load_buffer(self, saver, loader, im, max_diff = 0):
|
|
|
|
buf = Vips.call(saver, im)
|
|
|
|
x = Vips.call(loader, buf)
|
|
|
|
|
|
|
|
self.assertEqual(im.width, x.width)
|
|
|
|
self.assertEqual(im.height, x.height)
|
|
|
|
self.assertEqual(im.bands, x.bands)
|
|
|
|
self.assertLessEqual((im - x).abs().max(), max_diff)
|
|
|
|
|
2016-05-24 11:40:40 +02:00
|
|
|
def save_buffer_tempfile(self, saver, suf, im, max_diff = 0):
|
2016-05-24 11:15:54 +02:00
|
|
|
buf = Vips.call(saver, im)
|
2016-05-24 11:40:40 +02:00
|
|
|
f = NamedTemporaryFile(suffix=suf, delete=False)
|
2016-05-24 11:15:54 +02:00
|
|
|
f.write(buf)
|
2016-05-24 11:42:23 +02:00
|
|
|
f.close()
|
2016-05-24 11:15:54 +02:00
|
|
|
x = Vips.Image.new_from_file(f.name)
|
|
|
|
|
|
|
|
self.assertEqual(im.width, x.width)
|
|
|
|
self.assertEqual(im.height, x.height)
|
|
|
|
self.assertEqual(im.bands, x.bands)
|
|
|
|
self.assertLessEqual((im - x).abs().max(), max_diff)
|
|
|
|
|
|
|
|
os.unlink(f.name)
|
|
|
|
|
2015-05-28 17:19:32 +02:00
|
|
|
def test_vips(self):
|
|
|
|
self.save_load_file("test.v", "", self.colour, 0)
|
|
|
|
|
|
|
|
# check we can save and restore metadata
|
|
|
|
self.colour.write_to_file("test.v")
|
|
|
|
x = Vips.Image.new_from_file("test.v")
|
|
|
|
before_exif = self.colour.get_value("exif-data")
|
|
|
|
after_exif = x.get_value("exif-data")
|
|
|
|
|
2015-05-31 19:17:26 +02:00
|
|
|
self.assertEqual(len(before_exif), len(after_exif))
|
2015-05-28 17:19:32 +02:00
|
|
|
for i in range(len(before_exif)):
|
|
|
|
self.assertEqual(before_exif[i], after_exif[i])
|
|
|
|
|
|
|
|
x = None
|
|
|
|
os.unlink("test.v")
|
|
|
|
|
2015-02-16 17:03:39 +01:00
|
|
|
def test_jpeg(self):
|
2015-04-24 11:51:01 +02:00
|
|
|
x = Vips.type_find("VipsForeign", "jpegload")
|
|
|
|
if not x.is_instantiatable():
|
2015-02-16 17:03:39 +01:00
|
|
|
print("no jpeg support in this vips, skipping test")
|
|
|
|
return
|
|
|
|
|
|
|
|
def jpeg_valid(self, im):
|
2015-03-01 13:15:48 +01:00
|
|
|
a = im(10, 10)
|
2015-02-16 17:03:39 +01:00
|
|
|
self.assertAlmostEqualObjects(a, [6, 5, 3])
|
|
|
|
profile = im.get_value("icc-profile-data")
|
|
|
|
self.assertEqual(len(profile), 1352)
|
|
|
|
self.assertEqual(im.width, 1024)
|
|
|
|
self.assertEqual(im.height, 768)
|
|
|
|
self.assertEqual(im.bands, 3)
|
|
|
|
|
|
|
|
self.file_loader("jpegload", self.jpeg_file, jpeg_valid)
|
|
|
|
self.buffer_loader("jpegload_buffer", self.jpeg_file, jpeg_valid)
|
2015-05-05 10:40:29 +02:00
|
|
|
self.save_load_buffer("jpegsave_buffer", "jpegload_buffer", self.colour,
|
|
|
|
60)
|
2015-02-16 17:03:39 +01:00
|
|
|
self.save_load("%s.jpg", self.mono)
|
|
|
|
self.save_load("%s.jpg", self.colour)
|
|
|
|
|
2015-07-16 16:40:11 +02:00
|
|
|
# see if we have exif parsing
|
|
|
|
have_exif = False
|
|
|
|
x = Vips.Image.new_from_file(self.jpeg_file)
|
|
|
|
try:
|
2016-05-27 17:53:25 +02:00
|
|
|
# our test image has this field
|
2015-07-16 16:40:11 +02:00
|
|
|
y = x.get_value("exif-ifd0-Orientation")
|
|
|
|
have_exif = True
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
if have_exif:
|
2015-07-17 11:59:42 +02:00
|
|
|
# we need a copy of the image to set the new metadata on
|
|
|
|
# otherwise we get caching problems
|
2016-05-27 21:18:25 +02:00
|
|
|
x = Vips.Image.new_from_file(self.jpeg_file)
|
2015-07-17 11:59:42 +02:00
|
|
|
x = x.copy()
|
2016-05-27 17:53:25 +02:00
|
|
|
x.set_value("orientation", 2)
|
2015-07-16 16:40:11 +02:00
|
|
|
x.write_to_file("test.jpg")
|
|
|
|
x = Vips.Image.new_from_file("test.jpg")
|
2016-05-27 17:53:25 +02:00
|
|
|
y = x.get_value("orientation")
|
|
|
|
self.assertEqual(y, 2)
|
2015-07-16 16:40:11 +02:00
|
|
|
os.unlink("test.jpg")
|
|
|
|
|
2016-05-27 21:18:25 +02:00
|
|
|
x = Vips.Image.new_from_file(self.jpeg_file)
|
|
|
|
x = x.copy()
|
|
|
|
x.set_value("orientation", 2)
|
|
|
|
x.write_to_file("test-12.jpg")
|
|
|
|
|
|
|
|
x = Vips.Image.new_from_file("test-12.jpg")
|
|
|
|
y = x.get_value("orientation")
|
|
|
|
self.assertEqual(y, 2)
|
|
|
|
x.remove("orientation")
|
|
|
|
x.write_to_file("test-13.jpg")
|
|
|
|
x = Vips.Image.new_from_file("test-13.jpg")
|
|
|
|
y = x.get_value("orientation")
|
|
|
|
self.assertEqual(y, 1)
|
|
|
|
os.unlink("test-12.jpg")
|
|
|
|
os.unlink("test-13.jpg")
|
|
|
|
|
2016-05-27 21:24:48 +02:00
|
|
|
x = Vips.Image.new_from_file(self.jpeg_file)
|
|
|
|
x = x.copy()
|
|
|
|
x.set_value("orientation", 6)
|
|
|
|
x.write_to_file("test-14.jpg")
|
|
|
|
|
|
|
|
x1 = Vips.Image.new_from_file("test-14.jpg")
|
|
|
|
x2 = Vips.Image.new_from_file("test-14.jpg", autorotate = True)
|
|
|
|
self.assertEqual(x1.width, x2.height)
|
|
|
|
self.assertEqual(x1.height, x2.width)
|
|
|
|
os.unlink("test-14.jpg")
|
|
|
|
|
2015-02-16 17:03:39 +01:00
|
|
|
def test_png(self):
|
2015-04-24 11:51:01 +02:00
|
|
|
x = Vips.type_find("VipsForeign", "pngload")
|
|
|
|
if not x.is_instantiatable():
|
2015-02-16 17:03:39 +01:00
|
|
|
print("no png support in this vips, skipping test")
|
|
|
|
return
|
|
|
|
|
|
|
|
def png_valid(self, im):
|
2015-03-01 13:15:48 +01:00
|
|
|
a = im(10, 10)
|
2015-02-16 17:03:39 +01:00
|
|
|
self.assertAlmostEqualObjects(a, [38671.0, 33914.0, 26762.0])
|
|
|
|
self.assertEqual(im.width, 290)
|
|
|
|
self.assertEqual(im.height, 442)
|
|
|
|
self.assertEqual(im.bands, 3)
|
|
|
|
|
|
|
|
self.file_loader("pngload", self.png_file, png_valid)
|
|
|
|
self.buffer_loader("pngload_buffer", self.png_file, png_valid)
|
2015-05-05 10:40:29 +02:00
|
|
|
self.save_load_buffer("pngsave_buffer", "pngload_buffer", self.colour)
|
2015-02-16 17:03:39 +01:00
|
|
|
self.save_load("%s.png", self.mono)
|
|
|
|
self.save_load("%s.png", self.colour)
|
|
|
|
|
|
|
|
def test_tiff(self):
|
2015-04-24 11:51:01 +02:00
|
|
|
x = Vips.type_find("VipsForeign", "tiffload")
|
|
|
|
if not x.is_instantiatable():
|
2015-02-16 17:03:39 +01:00
|
|
|
print("no tiff support in this vips, skipping test")
|
|
|
|
return
|
|
|
|
|
|
|
|
def tiff_valid(self, im):
|
2015-03-01 13:15:48 +01:00
|
|
|
a = im(10, 10)
|
2015-02-16 17:03:39 +01:00
|
|
|
self.assertAlmostEqualObjects(a, [38671.0, 33914.0, 26762.0])
|
|
|
|
self.assertEqual(im.width, 290)
|
|
|
|
self.assertEqual(im.height, 442)
|
|
|
|
self.assertEqual(im.bands, 3)
|
|
|
|
|
|
|
|
self.file_loader("tiffload", self.tiff_file, tiff_valid)
|
|
|
|
self.buffer_loader("tiffload_buffer", self.tiff_file, tiff_valid)
|
2016-10-15 13:29:14 +02:00
|
|
|
self.save_load_buffer("tiffsave_buffer", "tiffload_buffer", self.colour)
|
2015-02-16 17:03:39 +01:00
|
|
|
self.save_load("%s.tif", self.mono)
|
|
|
|
self.save_load("%s.tif", self.colour)
|
|
|
|
self.save_load("%s.tif", self.cmyk)
|
|
|
|
|
2015-04-11 23:06:13 +02:00
|
|
|
self.save_load("%s.tif", self.onebit)
|
2016-01-22 23:06:20 +01:00
|
|
|
self.save_load_file("test-1.tif", "[squash]", self.onebit, 0)
|
|
|
|
self.save_load_file("test-2.tif", "[miniswhite]", self.onebit, 0)
|
|
|
|
self.save_load_file("test-3.tif", "[squash,miniswhite]", self.onebit, 0)
|
|
|
|
|
|
|
|
self.save_load_file("test-4.tif",
|
|
|
|
"[profile=images/sRGB.icm]",
|
|
|
|
self.colour, 0)
|
|
|
|
self.save_load_file("test-5.tif", "[tile]", self.colour, 0)
|
|
|
|
self.save_load_file("test-6.tif", "[tile,pyramid]", self.colour, 0)
|
|
|
|
self.save_load_file("test-7.tif",
|
|
|
|
"[tile,pyramid,compression=jpeg]", self.colour, 60)
|
|
|
|
self.save_load_file("test-8.tif", "[bigtiff]", self.colour, 0)
|
|
|
|
self.save_load_file("test-9.tif", "[compression=jpeg]", self.colour, 60)
|
|
|
|
self.save_load_file("test-10.tif",
|
2015-02-17 16:04:15 +01:00
|
|
|
"[tile,tile-width=256]", self.colour, 10)
|
|
|
|
|
2016-05-27 17:53:25 +02:00
|
|
|
# we need a copy of the image to set the new metadata on
|
|
|
|
# otherwise we get caching problems
|
|
|
|
x = Vips.Image.new_from_file(self.tiff_file)
|
|
|
|
x = x.copy()
|
|
|
|
x.set_value("orientation", 2)
|
2016-05-27 17:59:21 +02:00
|
|
|
x.write_to_file("test-11.tif")
|
|
|
|
x = Vips.Image.new_from_file("test-11.tif")
|
2016-05-27 17:53:25 +02:00
|
|
|
y = x.get_value("orientation")
|
|
|
|
self.assertEqual(y, 2)
|
2016-05-27 17:59:21 +02:00
|
|
|
os.unlink("test-11.tif")
|
2016-05-27 17:53:25 +02:00
|
|
|
|
2016-05-27 21:18:25 +02:00
|
|
|
# we need a copy of the image to set the new metadata on
|
|
|
|
# otherwise we get caching problems
|
|
|
|
x = Vips.Image.new_from_file(self.tiff_file)
|
|
|
|
x = x.copy()
|
|
|
|
x.set_value("orientation", 2)
|
|
|
|
x.write_to_file("test-12.tif")
|
|
|
|
|
|
|
|
x = Vips.Image.new_from_file("test-12.tif")
|
|
|
|
y = x.get_value("orientation")
|
|
|
|
self.assertEqual(y, 2)
|
|
|
|
x.remove("orientation")
|
|
|
|
x.write_to_file("test-13.tif")
|
|
|
|
x = Vips.Image.new_from_file("test-13.tif")
|
|
|
|
y = x.get_value("orientation")
|
|
|
|
self.assertEqual(y, 1)
|
|
|
|
os.unlink("test-12.tif")
|
|
|
|
os.unlink("test-13.tif")
|
|
|
|
|
2016-05-27 21:24:48 +02:00
|
|
|
x = Vips.Image.new_from_file(self.tiff_file)
|
|
|
|
x = x.copy()
|
|
|
|
x.set_value("orientation", 6)
|
|
|
|
x.write_to_file("test-14.tif")
|
|
|
|
|
|
|
|
x1 = Vips.Image.new_from_file("test-14.tif")
|
|
|
|
x2 = Vips.Image.new_from_file("test-14.tif", autorotate = True)
|
|
|
|
self.assertEqual(x1.width, x2.height)
|
|
|
|
self.assertEqual(x1.height, x2.width)
|
|
|
|
os.unlink("test-14.tif")
|
|
|
|
|
2015-02-16 17:03:39 +01:00
|
|
|
def test_magickload(self):
|
2015-04-24 11:51:01 +02:00
|
|
|
x = Vips.type_find("VipsForeign", "magickload")
|
|
|
|
if not x.is_instantiatable():
|
2015-02-16 17:03:39 +01:00
|
|
|
print("no magick support in this vips, skipping test")
|
|
|
|
return
|
|
|
|
|
|
|
|
def gif_valid(self, im):
|
2016-02-13 15:27:41 +01:00
|
|
|
# some libMagick produce an RGB for this image, some a mono, some
|
2016-07-09 13:19:56 +02:00
|
|
|
# rgba, some have a valid alpha, some don't :-(
|
|
|
|
# therefore ... just test channel 0
|
|
|
|
a = im(10, 10)[0]
|
|
|
|
|
|
|
|
self.assertAlmostEqual(a, 33)
|
2015-02-16 17:03:39 +01:00
|
|
|
self.assertEqual(im.width, 159)
|
|
|
|
self.assertEqual(im.height, 203)
|
|
|
|
|
|
|
|
self.file_loader("magickload", self.gif_file, gif_valid)
|
|
|
|
self.buffer_loader("magickload_buffer", self.gif_file, gif_valid)
|
|
|
|
|
2016-08-04 16:55:06 +02:00
|
|
|
# we should have rgba for svg files
|
|
|
|
im = Vips.Image.magickload(self.svg_file)
|
2016-08-14 19:39:40 +02:00
|
|
|
self.assertEqual(im.bands, 4)
|
|
|
|
|
|
|
|
# density should change size of generated svg
|
|
|
|
im = Vips.Image.magickload(self.svg_file, density = 100)
|
|
|
|
width = im.width
|
|
|
|
height = im.height
|
|
|
|
im = Vips.Image.magickload(self.svg_file, density = 200)
|
2016-08-14 22:33:33 +02:00
|
|
|
# This seems to fail on travis, no idea why, some problem in their IM
|
|
|
|
# perhaps
|
|
|
|
#self.assertEqual(im.width, width * 2)
|
|
|
|
#self.assertEqual(im.height, height * 2)
|
2016-08-04 16:55:06 +02:00
|
|
|
|
|
|
|
# all-frames should load every frame of the animation
|
|
|
|
im = Vips.Image.magickload(self.gif_anim_file)
|
2016-08-14 19:39:40 +02:00
|
|
|
width = im.width
|
|
|
|
height = im.height
|
2016-08-04 16:55:06 +02:00
|
|
|
im = Vips.Image.magickload(self.gif_anim_file, all_frames = True)
|
2016-08-14 19:39:40 +02:00
|
|
|
self.assertEqual(im.width, width)
|
|
|
|
self.assertEqual(im.height, height * 5)
|
|
|
|
|
|
|
|
# should work for dicom
|
|
|
|
im = Vips.Image.magickload(self.dicom_file)
|
|
|
|
self.assertEqual(im.width, 128)
|
|
|
|
self.assertEqual(im.height, 128)
|
2016-08-14 22:44:55 +02:00
|
|
|
# some IMs are 3 bands, some are 1, can't really test
|
|
|
|
#self.assertEqual(im.bands, 1)
|
2016-08-04 16:55:06 +02:00
|
|
|
|
2015-02-16 17:03:39 +01:00
|
|
|
def test_webp(self):
|
2015-04-24 11:51:01 +02:00
|
|
|
x = Vips.type_find("VipsForeign", "webpload")
|
|
|
|
if not x.is_instantiatable():
|
2015-02-16 17:03:39 +01:00
|
|
|
print("no webp support in this vips, skipping test")
|
|
|
|
return
|
|
|
|
|
|
|
|
def webp_valid(self, im):
|
2015-03-01 13:15:48 +01:00
|
|
|
a = im(10, 10)
|
2015-02-16 17:03:39 +01:00
|
|
|
self.assertAlmostEqualObjects(a, [71, 166, 236])
|
|
|
|
self.assertEqual(im.width, 550)
|
|
|
|
self.assertEqual(im.height, 368)
|
|
|
|
self.assertEqual(im.bands, 3)
|
|
|
|
|
|
|
|
self.file_loader("webpload", self.webp_file, webp_valid)
|
|
|
|
self.buffer_loader("webpload_buffer", self.webp_file, webp_valid)
|
2015-05-05 10:40:29 +02:00
|
|
|
self.save_load_buffer("webpsave_buffer", "webpload_buffer", self.colour,
|
|
|
|
50)
|
2015-02-16 17:03:39 +01:00
|
|
|
self.save_load("%s.webp", self.colour)
|
|
|
|
|
2016-05-17 15:08:46 +02:00
|
|
|
# test lossless mode
|
|
|
|
im = Vips.Image.new_from_file(self.webp_file)
|
|
|
|
buf = im.webpsave_buffer(lossless = True)
|
|
|
|
im2 = Vips.Image.new_from_buffer(buf, "")
|
|
|
|
self.assertEqual(im.avg(), im2.avg())
|
|
|
|
|
|
|
|
# higher Q should mean a bigger buffer
|
|
|
|
b1 = im.webpsave_buffer(Q = 10)
|
|
|
|
b2 = im.webpsave_buffer(Q = 90)
|
|
|
|
self.assertGreater(len(b2), len(b1))
|
|
|
|
|
2015-02-16 17:03:39 +01:00
|
|
|
def test_analyzeload(self):
|
2016-03-12 17:48:27 +01:00
|
|
|
x = Vips.type_find("VipsForeign", "analyzeload")
|
|
|
|
if not x.is_instantiatable():
|
|
|
|
print("no analyze support in this vips, skipping test")
|
|
|
|
return
|
|
|
|
|
2015-02-16 17:03:39 +01:00
|
|
|
def analyze_valid(self, im):
|
2015-03-01 13:15:48 +01:00
|
|
|
a = im(10, 10)
|
2015-02-16 17:03:39 +01:00
|
|
|
self.assertAlmostEqual(a[0], 3335)
|
|
|
|
self.assertEqual(im.width, 128)
|
|
|
|
self.assertEqual(im.height, 8064)
|
|
|
|
self.assertEqual(im.bands, 1)
|
|
|
|
|
|
|
|
self.file_loader("analyzeload", self.analyze_file, analyze_valid)
|
|
|
|
|
|
|
|
def test_matload(self):
|
2015-04-24 11:51:01 +02:00
|
|
|
x = Vips.type_find("VipsForeign", "matload")
|
|
|
|
if not x.is_instantiatable():
|
2015-02-16 17:03:39 +01:00
|
|
|
print("no matlab support in this vips, skipping test")
|
|
|
|
return
|
|
|
|
|
|
|
|
def matlab_valid(self, im):
|
2015-03-01 13:15:48 +01:00
|
|
|
a = im(10, 10)
|
2015-02-16 17:03:39 +01:00
|
|
|
self.assertAlmostEqualObjects(a, [38671.0, 33914.0, 26762.0])
|
|
|
|
self.assertEqual(im.width, 290)
|
|
|
|
self.assertEqual(im.height, 442)
|
|
|
|
self.assertEqual(im.bands, 3)
|
|
|
|
|
|
|
|
self.file_loader("matload", self.matlab_file, matlab_valid)
|
|
|
|
|
|
|
|
def test_openexrload(self):
|
2015-04-24 11:51:01 +02:00
|
|
|
x = Vips.type_find("VipsForeign", "openexrload")
|
|
|
|
if not x.is_instantiatable():
|
2015-02-16 17:03:39 +01:00
|
|
|
print("no openexr support in this vips, skipping test")
|
|
|
|
return
|
|
|
|
|
|
|
|
def exr_valid(self, im):
|
2015-03-01 13:15:48 +01:00
|
|
|
a = im(10, 10)
|
2015-02-16 17:03:39 +01:00
|
|
|
self.assertAlmostEqualObjects(a, [0.124512, 0.159668,
|
|
|
|
0.040375, 1.0],
|
|
|
|
places = 5)
|
|
|
|
self.assertEqual(im.width, 610)
|
|
|
|
self.assertEqual(im.height, 406)
|
|
|
|
self.assertEqual(im.bands, 4)
|
|
|
|
|
|
|
|
self.file_loader("openexrload", self.exr_file, exr_valid)
|
|
|
|
|
|
|
|
def test_fitsload(self):
|
2015-04-24 11:51:01 +02:00
|
|
|
x = Vips.type_find("VipsForeign", "fitsload")
|
|
|
|
if not x.is_instantiatable():
|
2015-02-16 17:03:39 +01:00
|
|
|
print("no fits support in this vips, skipping test")
|
|
|
|
return
|
|
|
|
|
|
|
|
def fits_valid(self, im):
|
2015-03-01 13:15:48 +01:00
|
|
|
a = im(10, 10)
|
2015-02-16 17:03:39 +01:00
|
|
|
self.assertAlmostEqualObjects(a, [-0.165013, -0.148553, 1.09122,
|
|
|
|
-0.942242],
|
|
|
|
places = 5)
|
|
|
|
self.assertEqual(im.width, 200)
|
|
|
|
self.assertEqual(im.height, 200)
|
|
|
|
self.assertEqual(im.bands, 4)
|
|
|
|
|
|
|
|
self.file_loader("fitsload", self.fits_file, fits_valid)
|
|
|
|
self.save_load("%s.fits", self.mono)
|
|
|
|
|
|
|
|
def test_openslideload(self):
|
2015-04-24 11:51:01 +02:00
|
|
|
x = Vips.type_find("VipsForeign", "openslideload")
|
|
|
|
if not x.is_instantiatable():
|
2015-02-16 17:03:39 +01:00
|
|
|
print("no openslide support in this vips, skipping test")
|
|
|
|
return
|
|
|
|
|
|
|
|
def openslide_valid(self, im):
|
2015-03-01 13:15:48 +01:00
|
|
|
a = im(10, 10)
|
2015-02-16 17:03:39 +01:00
|
|
|
self.assertAlmostEqualObjects(a, [244, 250, 243, 255])
|
|
|
|
self.assertEqual(im.width, 2220)
|
|
|
|
self.assertEqual(im.height, 2967)
|
|
|
|
self.assertEqual(im.bands, 4)
|
|
|
|
|
|
|
|
self.file_loader("openslideload", self.openslide_file, openslide_valid)
|
|
|
|
|
2016-02-08 21:39:39 +01:00
|
|
|
def test_pdfload(self):
|
|
|
|
x = Vips.type_find("VipsForeign", "pdfload")
|
|
|
|
if not x.is_instantiatable():
|
|
|
|
print("no pdf support in this vips, skipping test")
|
|
|
|
return
|
|
|
|
|
|
|
|
def pdf_valid(self, im):
|
|
|
|
a = im(10, 10)
|
|
|
|
self.assertAlmostEqualObjects(a, [35, 31, 32, 255])
|
|
|
|
self.assertEqual(im.width, 1133)
|
|
|
|
self.assertEqual(im.height, 680)
|
|
|
|
self.assertEqual(im.bands, 4)
|
|
|
|
|
|
|
|
self.file_loader("pdfload", self.pdf_file, pdf_valid)
|
|
|
|
self.buffer_loader("pdfload_buffer", self.pdf_file, pdf_valid)
|
|
|
|
|
|
|
|
im = Vips.Image.new_from_file(self.pdf_file)
|
|
|
|
x = Vips.Image.new_from_file(self.pdf_file, scale = 2)
|
|
|
|
self.assertLess(abs(im.width * 2 - x.width), 2)
|
|
|
|
self.assertLess(abs(im.height * 2 - x.height), 2)
|
|
|
|
|
|
|
|
im = Vips.Image.new_from_file(self.pdf_file)
|
|
|
|
x = Vips.Image.new_from_file(self.pdf_file, dpi = 144)
|
|
|
|
self.assertLess(abs(im.width * 2 - x.width), 2)
|
|
|
|
self.assertLess(abs(im.height * 2 - x.height), 2)
|
2016-02-09 12:01:12 +01:00
|
|
|
|
2016-02-12 21:04:46 +01:00
|
|
|
def test_gifload(self):
|
|
|
|
x = Vips.type_find("VipsForeign", "gifload")
|
|
|
|
if not x.is_instantiatable():
|
|
|
|
print("no gif support in this vips, skipping test")
|
|
|
|
return
|
|
|
|
|
|
|
|
def gif_valid(self, im):
|
|
|
|
a = im(10, 10)
|
2016-07-26 18:28:54 +02:00
|
|
|
self.assertAlmostEqualObjects(a, [33])
|
2016-02-12 21:04:46 +01:00
|
|
|
self.assertEqual(im.width, 159)
|
|
|
|
self.assertEqual(im.height, 203)
|
2016-07-26 18:28:54 +02:00
|
|
|
self.assertEqual(im.bands, 1)
|
2016-02-12 21:04:46 +01:00
|
|
|
|
|
|
|
self.file_loader("gifload", self.gif_file, gif_valid)
|
|
|
|
self.buffer_loader("gifload_buffer", self.gif_file, gif_valid)
|
|
|
|
|
2016-02-09 12:01:12 +01:00
|
|
|
def test_svgload(self):
|
|
|
|
x = Vips.type_find("VipsForeign", "svgload")
|
|
|
|
if not x.is_instantiatable():
|
|
|
|
print("no svg support in this vips, skipping test")
|
|
|
|
return
|
|
|
|
|
|
|
|
def svg_valid(self, im):
|
|
|
|
a = im(10, 10)
|
|
|
|
self.assertAlmostEqualObjects(a, [0, 0, 77, 255])
|
|
|
|
self.assertEqual(im.width, 360)
|
|
|
|
self.assertEqual(im.height, 588)
|
|
|
|
self.assertEqual(im.bands, 4)
|
|
|
|
|
|
|
|
self.file_loader("svgload", self.svg_file, svg_valid)
|
|
|
|
self.buffer_loader("svgload_buffer", self.svg_file, svg_valid)
|
|
|
|
|
2016-06-26 23:59:58 +02:00
|
|
|
self.file_loader("svgload", self.svgz_file, svg_valid)
|
|
|
|
self.buffer_loader("svgload_buffer", self.svgz_file, svg_valid)
|
|
|
|
|
2016-08-01 15:57:33 +02:00
|
|
|
self.file_loader("svgload", self.svg_gz_file, svg_valid)
|
|
|
|
|
2016-02-09 12:01:12 +01:00
|
|
|
im = Vips.Image.new_from_file(self.svg_file)
|
|
|
|
x = Vips.Image.new_from_file(self.svg_file, scale = 2)
|
|
|
|
self.assertLess(abs(im.width * 2 - x.width), 2)
|
|
|
|
self.assertLess(abs(im.height * 2 - x.height), 2)
|
|
|
|
|
|
|
|
im = Vips.Image.new_from_file(self.svg_file)
|
|
|
|
x = Vips.Image.new_from_file(self.svg_file, dpi = 144)
|
|
|
|
self.assertLess(abs(im.width * 2 - x.width), 2)
|
|
|
|
self.assertLess(abs(im.height * 2 - x.height), 2)
|
2016-02-08 21:39:39 +01:00
|
|
|
|
2015-02-16 17:03:39 +01:00
|
|
|
def test_csv(self):
|
|
|
|
self.save_load("%s.csv", self.mono)
|
|
|
|
|
|
|
|
def test_matrix(self):
|
|
|
|
self.save_load("%s.mat", self.mono)
|
|
|
|
|
|
|
|
def test_ppm(self):
|
2016-03-12 17:48:27 +01:00
|
|
|
x = Vips.type_find("VipsForeign", "ppmload")
|
|
|
|
if not x.is_instantiatable():
|
|
|
|
print("no PPM support in this vips, skipping test")
|
|
|
|
return
|
|
|
|
|
2015-02-16 17:03:39 +01:00
|
|
|
self.save_load("%s.ppm", self.mono)
|
|
|
|
self.save_load("%s.ppm", self.colour)
|
|
|
|
|
|
|
|
def test_rad(self):
|
2016-03-12 17:48:27 +01:00
|
|
|
x = Vips.type_find("VipsForeign", "radload")
|
|
|
|
if not x.is_instantiatable():
|
|
|
|
print("no Radiance support in this vips, skipping test")
|
|
|
|
return
|
|
|
|
|
2015-02-16 17:03:39 +01:00
|
|
|
self.save_load("%s.hdr", self.colour)
|
2016-05-24 11:40:40 +02:00
|
|
|
self.save_buffer_tempfile("radsave_buffer", ".hdr", self.rad, max_diff = 0)
|
2015-02-16 17:03:39 +01:00
|
|
|
|
2015-09-10 15:07:48 +02:00
|
|
|
def test_dzsave(self):
|
|
|
|
x = Vips.type_find("VipsForeign", "dzsave")
|
|
|
|
if not x.is_instantiatable():
|
|
|
|
print("no dzsave support in this vips, skipping test")
|
|
|
|
return
|
|
|
|
|
|
|
|
# dzsave is hard to test, there are so many options
|
|
|
|
# test each option separately and hope they all function together
|
|
|
|
# correctly
|
|
|
|
|
2015-12-17 12:54:38 +01:00
|
|
|
# default deepzoom layout ... we must use png here, since we want to
|
|
|
|
# test the overlap for equality
|
|
|
|
self.colour.dzsave("test", suffix = ".png")
|
2015-09-10 15:07:48 +02:00
|
|
|
|
2016-09-08 11:41:01 +02:00
|
|
|
# test horizontal overlap ... expect 256 step, overlap 1
|
2015-12-30 12:23:33 +01:00
|
|
|
x = Vips.Image.new_from_file("test_files/10/0_0.png")
|
|
|
|
self.assertEqual(x.width, 255)
|
|
|
|
y = Vips.Image.new_from_file("test_files/10/1_0.png")
|
|
|
|
self.assertEqual(y.width, 256)
|
2015-12-17 12:54:38 +01:00
|
|
|
|
|
|
|
# the right two columns of x should equal the left two columns of y
|
|
|
|
left = x.crop(x.width - 2, 0, 2, x.height)
|
|
|
|
right = y.crop(0, 0, 2, y.height)
|
|
|
|
self.assertEqual((left - right).abs().max(), 0)
|
2015-09-10 15:07:48 +02:00
|
|
|
|
2015-12-30 12:23:33 +01:00
|
|
|
# test vertical overlap
|
|
|
|
self.assertEqual(x.height, 255)
|
|
|
|
y = Vips.Image.new_from_file("test_files/10/0_1.png")
|
|
|
|
self.assertEqual(y.height, 256)
|
|
|
|
|
|
|
|
# the bottom two rows of x should equal the top two rows of y
|
|
|
|
top = x.crop(0, x.height - 2, x.width, 2)
|
|
|
|
bottom = y.crop(0, 0, y.width, 2)
|
|
|
|
self.assertEqual((top - bottom).abs().max(), 0)
|
2015-09-10 15:07:48 +02:00
|
|
|
|
|
|
|
# there should be a bottom layer
|
2015-12-17 12:54:38 +01:00
|
|
|
x = Vips.Image.new_from_file("test_files/0/0_0.png")
|
2015-09-10 15:07:48 +02:00
|
|
|
self.assertEqual(x.width, 1)
|
|
|
|
self.assertEqual(x.height, 1)
|
|
|
|
|
|
|
|
# 10 should be the final layer
|
|
|
|
self.assertFalse(os.path.isdir("test_files/11"))
|
|
|
|
|
|
|
|
shutil.rmtree("test_files")
|
|
|
|
os.unlink("test.dzi")
|
|
|
|
|
|
|
|
# default google layout
|
|
|
|
self.colour.dzsave("test", layout = "google")
|
|
|
|
|
|
|
|
# test bottom-right tile ... default is 256x256 tiles, overlap 0
|
|
|
|
x = Vips.Image.new_from_file("test/2/2/3.jpg")
|
|
|
|
self.assertEqual(x.width, 256)
|
|
|
|
self.assertEqual(x.height, 256)
|
|
|
|
self.assertFalse(os.path.exists("test/2/2/4.jpg"))
|
|
|
|
self.assertFalse(os.path.exists("test/3"))
|
|
|
|
x = Vips.Image.new_from_file("test/blank.png")
|
|
|
|
self.assertEqual(x.width, 256)
|
|
|
|
self.assertEqual(x.height, 256)
|
|
|
|
|
|
|
|
shutil.rmtree("test")
|
|
|
|
|
2016-09-08 11:41:01 +02:00
|
|
|
# google layout with overlap ... verify that we clip correctly
|
|
|
|
# with overlap 192 tile size 256, we should step by 64 pixels each time
|
|
|
|
# so 3x3 tiles exactly
|
|
|
|
self.colour.crop(0, 0, 384, 384).dzsave("test2", layout = "google",
|
|
|
|
overlap = 192, depth = "one")
|
|
|
|
|
|
|
|
# test bottom-right tile ... default is 256x256 tiles, overlap 0
|
|
|
|
x = Vips.Image.new_from_file("test2/0/2/2.jpg")
|
|
|
|
self.assertEqual(x.width, 256)
|
|
|
|
self.assertEqual(x.height, 256)
|
|
|
|
self.assertFalse(os.path.exists("test2/0/3/3.jpg"))
|
|
|
|
|
|
|
|
shutil.rmtree("test2")
|
|
|
|
|
|
|
|
self.colour.crop(0, 0, 385, 385).dzsave("test3", layout = "google",
|
|
|
|
overlap = 192, depth = "one")
|
|
|
|
|
|
|
|
# test bottom-right tile ... default is 256x256 tiles, overlap 0
|
|
|
|
x = Vips.Image.new_from_file("test3/0/3/3.jpg")
|
|
|
|
self.assertEqual(x.width, 256)
|
|
|
|
self.assertEqual(x.height, 256)
|
|
|
|
self.assertFalse(os.path.exists("test3/0/4/4.jpg"))
|
|
|
|
|
|
|
|
shutil.rmtree("test3")
|
|
|
|
|
2015-09-10 15:07:48 +02:00
|
|
|
# default zoomify layout
|
|
|
|
self.colour.dzsave("test", layout = "zoomify")
|
|
|
|
|
|
|
|
# 256x256 tiles, no overlap
|
|
|
|
self.assertTrue(os.path.exists("test/ImageProperties.xml"))
|
|
|
|
x = Vips.Image.new_from_file("test/TileGroup0/2-3-2.jpg")
|
|
|
|
self.assertEqual(x.width, 256)
|
|
|
|
self.assertEqual(x.height, 256)
|
|
|
|
|
|
|
|
shutil.rmtree("test")
|
|
|
|
|
|
|
|
# test zip output
|
|
|
|
self.colour.dzsave("test.zip")
|
|
|
|
self.assertFalse(os.path.exists("test_files"))
|
|
|
|
self.assertFalse(os.path.exists("test.dzi"))
|
2016-06-04 02:14:46 +02:00
|
|
|
|
|
|
|
# test compressed zip output
|
|
|
|
self.colour.dzsave("test_compressed.zip", compression = -1)
|
|
|
|
self.assertLess(os.path.getsize("test_compressed.zip"),
|
|
|
|
os.path.getsize("test.zip"))
|
2015-09-10 15:07:48 +02:00
|
|
|
os.unlink("test.zip")
|
2016-06-04 02:14:46 +02:00
|
|
|
os.unlink("test_compressed.zip")
|
2015-09-10 15:07:48 +02:00
|
|
|
|
|
|
|
# test suffix
|
|
|
|
self.colour.dzsave("test", suffix = ".png")
|
|
|
|
|
2015-12-17 12:54:38 +01:00
|
|
|
x = Vips.Image.new_from_file("test_files/10/0_0.png")
|
2015-12-30 12:23:33 +01:00
|
|
|
self.assertEqual(x.width, 255)
|
2015-09-10 15:07:48 +02:00
|
|
|
|
|
|
|
shutil.rmtree("test_files")
|
|
|
|
os.unlink("test.dzi")
|
|
|
|
|
|
|
|
# test overlap
|
|
|
|
self.colour.dzsave("test", overlap = 200)
|
|
|
|
|
2015-12-17 12:54:38 +01:00
|
|
|
y = Vips.Image.new_from_file("test_files/10/1_1.jpeg")
|
2015-12-30 12:23:33 +01:00
|
|
|
self.assertEqual(y.width, 654)
|
2015-09-10 15:07:48 +02:00
|
|
|
|
|
|
|
shutil.rmtree("test_files")
|
|
|
|
os.unlink("test.dzi")
|
|
|
|
|
|
|
|
# test tile-size
|
|
|
|
self.colour.dzsave("test", tile_size = 512)
|
|
|
|
|
2015-12-17 12:54:38 +01:00
|
|
|
y = Vips.Image.new_from_file("test_files/10/0_0.jpeg")
|
|
|
|
self.assertEqual(y.width, 513)
|
|
|
|
self.assertEqual(y.height, 513)
|
2015-09-10 15:07:48 +02:00
|
|
|
|
|
|
|
shutil.rmtree("test_files")
|
|
|
|
os.unlink("test.dzi")
|
|
|
|
|
2016-10-15 20:01:34 +02:00
|
|
|
# test save to memory buffer
|
|
|
|
self.colour.dzsave("test-10.zip")
|
|
|
|
with open("test-10.zip", 'rb') as f:
|
|
|
|
buf1 = f.read()
|
|
|
|
os.unlink("test-10.zip")
|
|
|
|
buf2 = self.colour.dzsave_buffer(basename = "test-10")
|
|
|
|
self.assertEqual(len(buf1), len(buf2))
|
|
|
|
|
|
|
|
# we can't test the bytes are exactly equal, the timestamps will be
|
|
|
|
# different
|
|
|
|
|
2015-02-16 17:03:39 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|
|
|
|
|