class TestTransform(object): def setup(self): self.src_img = ImageSource(create_debug_img((200, 200), transparent=False)) self.src_srs = SRS(31467) self.dst_size = (100, 150) self.dst_srs = SRS(4326) self.dst_bbox = (0.2, 45.1, 8.3, 53.2) self.src_bbox = self.dst_srs.transform_bbox_to(self.src_srs, self.dst_bbox) def test_transform(self, mesh_div=4): transformer = ImageTransformer(self.src_srs, self.dst_srs, mesh_div=mesh_div) result = transformer.transform(self.src_img, self.src_bbox, self.dst_size, self.dst_bbox, image_opts=ImageOptions(resampling='nearest')) assert isinstance(result, ImageSource) assert result.as_image() != self.src_img.as_image() assert result.size == (100, 150) def _test_compare_mesh_div(self): """ Create transformations with different div values. """ for div in [1, 2, 4, 6, 8, 12, 16]: transformer = ImageTransformer(self.src_srs, self.dst_srs, mesh_div=div) result = transformer.transform(self.src_img, self.src_bbox, self.dst_size, self.dst_bbox) result.as_image().save('/tmp/transform-%d.png' % (div,))
class TestTransform(object): def setup(self): self.src_img = ImageSource(create_debug_img((200, 200), transparent=False)) self.src_srs = SRS(31467) self.dst_size = (100, 150) self.dst_srs = SRS(4326) self.dst_bbox = (0.2, 45.1, 8.3, 53.2) self.src_bbox = self.dst_srs.transform_bbox_to(self.src_srs, self.dst_bbox) def test_transform(self, mesh_div=4): transformer = ImageTransformer(self.src_srs, self.dst_srs, mesh_div=mesh_div) result = transformer.transform(self.src_img, self.src_bbox, self.dst_size, self.dst_bbox, image_opts=ImageOptions(resampling='nearest')) assert isinstance(result, ImageSource) assert result.as_image() != self.src_img.as_image() assert result.size == (100, 150) def _test_compare_mesh_div(self): """ Create transformations with different div values. """ for div in [1, 2, 4, 6, 8, 12, 16]: transformer = ImageTransformer(self.src_srs, self.dst_srs, mesh_div=div) result = transformer.transform(self.src_img, self.src_bbox, self.dst_size, self.dst_bbox) result.as_image().save('/tmp/transform-%d.png' % (div,))
def test_from_non_seekable_file(self): with open(self.tmp_filename, 'rb') as tmp_file: data = tmp_file.read() class FileLikeDummy(object): # "file" without seek, like urlopen response def read(self): return data ir = ImageSource(FileLikeDummy(), 'png') assert ir.as_buffer(seekable=True).read() == data assert ir.as_image().size == (100, 100) assert ir.as_buffer().read() == data
def test_from_non_seekable_file(self): with open(self.tmp_filename, "rb") as tmp_file: data = tmp_file.read() class FileLikeDummy(object): # "file" without seek, like urlopen response def read(self): return data ir = ImageSource(FileLikeDummy(), "png") assert ir.as_buffer(seekable=True).read() == data assert ir.as_image().size == (100, 100) assert ir.as_buffer().read() == data
class TestTransform(object): def setup(self): self.src_img = ImageSource( create_debug_img((200, 200), transparent=False)) self.src_srs = SRS(31467) self.dst_size = (100, 150) self.dst_srs = SRS(4326) self.dst_bbox = (0.2, 45.1, 8.3, 53.2) self.src_bbox = self.dst_srs.transform_bbox_to(self.src_srs, self.dst_bbox) def test_transform(self): transformer = ImageTransformer(self.src_srs, self.dst_srs) result = transformer.transform( self.src_img, self.src_bbox, self.dst_size, self.dst_bbox, image_opts=ImageOptions(resampling="nearest"), ) assert isinstance(result, ImageSource) assert result.as_image() != self.src_img.as_image() assert result.size == (100, 150) def _test_compare_max_px_err(self): """ Create transformations with different div values. """ for err in [0.2, 0.5, 1, 2, 4, 6, 8, 12, 16]: transformer = ImageTransformer(self.src_srs, self.dst_srs, max_px_err=err) result = transformer.transform( self.src_img, self.src_bbox, self.dst_size, self.dst_bbox, image_opts=ImageOptions(resampling="nearest"), ) result.as_image().save("/tmp/transform-%03d.png" % (err * 10, ))
def test_from_image(self): img = Image.new('RGBA', (100, 100)) ir = ImageSource(img, (100, 100), PNG_FORMAT) assert ir.as_image() == img assert is_png(ir.as_buffer())
def test_from_file(self): with open(self.tmp_filename, 'rb') as tmp_file: ir = ImageSource(tmp_file, 'png') assert ir.as_buffer() == tmp_file assert ir.as_image().size == (100, 100)
def test_from_filename(self): ir = ImageSource(self.tmp_filename, PNG_FORMAT) assert is_png(ir.as_buffer()) assert ir.as_image().size == (100, 100)
def test_from_image(self): img = Image.new("RGBA", (100, 100)) ir = ImageSource(img, (100, 100), PNG_FORMAT) assert ir.as_image() == img assert is_png(ir.as_buffer())
def test_from_file(self): with open(self.tmp_filename, "rb") as tmp_file: ir = ImageSource(tmp_file, "png") assert ir.as_buffer() == tmp_file assert ir.as_image().size == (100, 100)
def test_from_filename(self): ir = ImageSource(self.tmp_filename, PNG_FORMAT) assert is_png(ir.as_buffer()) assert ir.as_image().size == (100, 100)