2019-11-07 18:35:49 +01:00
|
|
|
# vim: set fileencoding=utf-8 :
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import tempfile
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
import pyvips
|
|
|
|
from helpers import \
|
|
|
|
JPEG_FILE, PNG_FILE, TIF_FILE, \
|
|
|
|
temp_filename, assert_almost_equal_objects, have, skip_if_no
|
|
|
|
|
|
|
|
|
2019-12-30 00:08:33 +01:00
|
|
|
class TestConnection:
|
2019-11-07 18:35:49 +01:00
|
|
|
tempdir = None
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setup_class(cls):
|
|
|
|
cls.tempdir = tempfile.mkdtemp()
|
|
|
|
cls.colour = pyvips.Image.jpegload(JPEG_FILE)
|
2019-11-30 13:11:23 +01:00
|
|
|
cls.mono = cls.colour.extract_band(1).copy()
|
2019-11-07 18:35:49 +01:00
|
|
|
# we remove the ICC profile: the RGB one will no longer be appropriate
|
|
|
|
cls.mono.remove("icc-profile-data")
|
2019-11-30 13:11:23 +01:00
|
|
|
cls.rad = cls.colour.float2rad().copy()
|
2019-11-07 18:35:49 +01:00
|
|
|
cls.rad.remove("icc-profile-data")
|
|
|
|
cls.cmyk = cls.colour.bandjoin(cls.mono)
|
|
|
|
cls.cmyk = cls.cmyk.copy(interpretation=pyvips.Interpretation.CMYK)
|
|
|
|
cls.cmyk.remove("icc-profile-data")
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def teardown_class(cls):
|
|
|
|
shutil.rmtree(cls.tempdir, ignore_errors=True)
|
2019-12-21 06:13:54 +01:00
|
|
|
cls.colour = None
|
|
|
|
cls.mono = None
|
|
|
|
cls.rad = None
|
|
|
|
cls.cmyk = None
|
2019-11-07 18:35:49 +01:00
|
|
|
|
2019-12-30 00:08:33 +01:00
|
|
|
def test_source_new_from_file(self):
|
|
|
|
x = pyvips.Source.new_from_file(JPEG_FILE)
|
2019-11-07 18:35:49 +01:00
|
|
|
|
|
|
|
assert x.filename() == JPEG_FILE
|
|
|
|
|
2019-12-30 00:08:33 +01:00
|
|
|
@skip_if_no("jpegload_source")
|
|
|
|
def test_image_new_from_source_file(self):
|
|
|
|
x = pyvips.Source.new_from_file(JPEG_FILE)
|
|
|
|
y = pyvips.Image.new_from_source(x, "")
|
2019-11-07 18:35:49 +01:00
|
|
|
|
2019-11-27 12:48:50 +01:00
|
|
|
assert y.width == 290
|
|
|
|
assert y.height == 442
|
2019-11-07 18:35:49 +01:00
|
|
|
|
2019-12-30 00:08:33 +01:00
|
|
|
def test_target_new_to_file(self):
|
2019-11-07 18:35:49 +01:00
|
|
|
filename = temp_filename(self.tempdir, ".jpg")
|
2019-12-30 00:08:33 +01:00
|
|
|
x = pyvips.Target.new_to_file(filename)
|
2019-11-07 18:35:49 +01:00
|
|
|
|
|
|
|
assert x.filename() == filename
|
|
|
|
|
2019-12-30 00:08:33 +01:00
|
|
|
@skip_if_no("jpegload_source")
|
|
|
|
def test_image_write_to_target_file(self):
|
2019-11-07 18:35:49 +01:00
|
|
|
filename = temp_filename(self.tempdir, ".jpg")
|
2019-12-30 00:08:33 +01:00
|
|
|
x = pyvips.Target.new_to_file(filename)
|
|
|
|
self.colour.write_to_target(x, ".jpg")
|
2019-11-07 18:35:49 +01:00
|
|
|
with open(filename, 'rb') as f:
|
|
|
|
data = f.read()
|
|
|
|
data2 = self.colour.write_to_buffer(".jpg")
|
|
|
|
|
|
|
|
assert data == data2
|
|
|
|
|
2019-12-30 00:08:33 +01:00
|
|
|
def test_source_new_memory(self):
|
2019-11-07 18:35:49 +01:00
|
|
|
data = self.colour.write_to_buffer(".jpg")
|
2019-12-30 00:08:33 +01:00
|
|
|
x = pyvips.Source.new_from_memory(data)
|
2019-11-07 18:35:49 +01:00
|
|
|
|
|
|
|
assert x.filename() == None
|
|
|
|
|
2019-12-30 00:08:33 +01:00
|
|
|
@skip_if_no("jpegload_source")
|
|
|
|
def test_image_new_from_source_memory(self):
|
2019-11-07 18:35:49 +01:00
|
|
|
data = self.colour.write_to_buffer(".jpg")
|
2019-12-30 00:08:33 +01:00
|
|
|
x = pyvips.Source.new_from_memory(data)
|
|
|
|
y = pyvips.Image.new_from_source(x, "")
|
2019-11-07 18:35:49 +01:00
|
|
|
|
2019-11-27 12:48:50 +01:00
|
|
|
assert y.width == 290
|
|
|
|
assert y.height == 442
|
2019-11-07 18:35:49 +01:00
|
|
|
|
2019-12-30 00:08:33 +01:00
|
|
|
def test_target_new_memory(self):
|
|
|
|
x = pyvips.Target.new_to_memory()
|
2019-11-07 18:35:49 +01:00
|
|
|
|
|
|
|
assert x.filename() == None
|
|
|
|
|
2019-12-30 00:08:33 +01:00
|
|
|
@skip_if_no("jpegload_source")
|
|
|
|
def test_image_write_to_target_memory(self):
|
|
|
|
x = pyvips.Target.new_to_memory()
|
|
|
|
self.colour.write_to_target(x, ".jpg")
|
2019-11-07 18:35:49 +01:00
|
|
|
y = self.colour.write_to_buffer(".jpg")
|
|
|
|
|
|
|
|
assert x.get("blob") == y
|
|
|
|
|
2020-06-27 16:23:18 +02:00
|
|
|
@skip_if_no("matrixload_source")
|
|
|
|
@skip_if_no("matrixsave_target")
|
|
|
|
def test_connection_matrix(self):
|
|
|
|
x = pyvips.Target.new_to_memory()
|
|
|
|
self.mono.matrixsave_target(x)
|
|
|
|
y = pyvips.Source.new_from_memory(x.get("blob"))
|
|
|
|
im = pyvips.Image.matrixload_source(y)
|
|
|
|
|
|
|
|
assert (im - self.mono).abs().max() == 0
|
|
|
|
|
|
|
|
@skip_if_no("csvload_source")
|
|
|
|
@skip_if_no("csvsave_target")
|
|
|
|
def test_connection_csv(self):
|
|
|
|
x = pyvips.Target.new_to_memory()
|
|
|
|
self.mono.csvsave_target(x)
|
|
|
|
y = pyvips.Source.new_from_memory(x.get("blob"))
|
|
|
|
im = pyvips.Image.csvload_source(y)
|
|
|
|
|
|
|
|
assert (im - self.mono).abs().max() == 0
|
|
|
|
|
|
|
|
@skip_if_no("ppmload_source")
|
|
|
|
@skip_if_no("ppmsave_target")
|
|
|
|
def test_connection_ppm(self):
|
|
|
|
x = pyvips.Target.new_to_memory()
|
|
|
|
self.mono.ppmsave_target(x)
|
|
|
|
y = pyvips.Source.new_from_memory(x.get("blob"))
|
|
|
|
im = pyvips.Image.ppmload_source(y)
|
|
|
|
|
|
|
|
assert (im - self.mono).abs().max() == 0
|
|
|
|
|
2019-11-07 18:35:49 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
pytest.main()
|