1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
# vim: set fileencoding=utf-8 :
import sys
import os
import shutil
import tempfile
import pytest
import pyvips
from helpers import *
class TestConnection:
tempdir = None
@classmethod
def setup_class(cls):
cls.tempdir = tempfile.mkdtemp()
cls.colour = pyvips.Image.jpegload(JPEG_FILE)
cls.mono = cls.colour.extract_band(1).copy()
# we remove the ICC profile: the RGB one will no longer be appropriate
cls.mono.remove("icc-profile-data")
cls.rad = cls.colour.float2rad().copy()
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)
cls.colour = None
cls.mono = None
cls.rad = None
cls.cmyk = None
def test_source_new_from_file(self):
x = pyvips.Source.new_from_file(JPEG_FILE)
assert x.filename() == JPEG_FILE
@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, "")
assert y.width == 290
assert y.height == 442
def test_target_new_to_file(self):
filename = temp_filename(self.tempdir, ".jpg")
x = pyvips.Target.new_to_file(filename)
assert x.filename() == filename
@skip_if_no("jpegload_source")
def test_image_write_to_target_file(self):
filename = temp_filename(self.tempdir, ".jpg")
x = pyvips.Target.new_to_file(filename)
self.colour.write_to_target(x, ".jpg")
with open(filename, 'rb') as f:
data = f.read()
data2 = self.colour.write_to_buffer(".jpg")
assert data == data2
def test_source_new_memory(self):
data = self.colour.write_to_buffer(".jpg")
x = pyvips.Source.new_from_memory(data)
assert x.filename() == None
@skip_if_no("jpegload_source")
def test_image_new_from_source_memory(self):
data = self.colour.write_to_buffer(".jpg")
x = pyvips.Source.new_from_memory(data)
y = pyvips.Image.new_from_source(x, "")
assert y.width == 290
assert y.height == 442
def test_target_new_memory(self):
x = pyvips.Target.new_to_memory()
assert x.filename() == None
@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")
y = self.colour.write_to_buffer(".jpg")
assert x.get("blob") == y
@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("svgload_source")
def test_connection_svg(self):
svg = b'<svg xmlns="http://www.w3.org/2000/svg" width="1" height="1" />'
x = pyvips.Source.new_from_memory(svg)
im = pyvips.Image.new_from_source(x, "")
assert im.width == 1
assert im.height == 1
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
@skip_if_no("tiffload_source")
@skip_if_no("tiffsave_target")
def test_connection_tiff(self):
x = pyvips.Target.new_to_memory()
self.mono.tiffsave_target(x)
y = pyvips.Source.new_from_memory(x.get("blob"))
im = pyvips.Image.tiffload_source(y)
assert (im - self.mono).abs().max() == 0
@skip_if_no("dzsave_target")
def test_connection_dz(self):
x = pyvips.Target.new_to_memory()
self.mono.dzsave_target(x)
data = x.get("blob")
# it'll be a zip, we could extract stuff and look for tiles
assert len(data) > 1000
if __name__ == '__main__':
pytest.main()
|