From 04353f8915165c6e4ec91eb79e3e1b82df19c532 Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Fri, 28 Apr 2017 18:27:15 +0100 Subject: [PATCH] added new_from_image() to python and tests --- ChangeLog | 1 + TODO | 9 ++++---- doc/using-python.xml | 4 ++-- python/packages/gi/overrides/Vips.py | 31 +++++++++++++++++++++------- test/test_iofuncs.py | 22 ++++++++++++++++++++ 5 files changed, 54 insertions(+), 13 deletions(-) diff --git a/ChangeLog b/ChangeLog index 92d0226d..93daddf8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,7 @@ - supports fits images with leading non-image HDUs, thanks benepo - add vips_image_new_from_image() and vips_image_new_from_image1() ... make a constant image +- add new_from_image() to Python as well 23/4/17 started 8.5.5 - doc polishing diff --git a/TODO b/TODO index 1a7615c3..4e02743b 100644 --- a/TODO +++ b/TODO @@ -1,13 +1,14 @@ -- can we change VImage::new_from_image() to not inherit bands? too awkward to - make a one-band image +- Add new_from_image() to C++ - needs some docs + needs docs - add to python, C, ruby, php etc etc + also check php, ruby - does cpp image.bandjoin(12) work? how about bandjoin(std::vector({1, 2})) + check bandjoin_const use in Python too + - not sure about utf8 error messages on win - strange: diff --git a/doc/using-python.xml b/doc/using-python.xml index 39657da1..cafe1711 100644 --- a/doc/using-python.xml +++ b/doc/using-python.xml @@ -162,8 +162,8 @@ im = Vips.Image.new_from_file(sys.argv[1], access = Vips.Access.SEQUENTIAL) for each file format. The C equivalent to this function, vips_image_new_from_file(), has more extensive documentation. Try help(Vips.Image) to see a list of all the image - constructors --- you can load from memory, or create from an array, - for example. + constructors --- you can load from memory, create from an array, + or create from a constant, for example. diff --git a/python/packages/gi/overrides/Vips.py b/python/packages/gi/overrides/Vips.py index 55043ae2..c071d5e4 100644 --- a/python/packages/gi/overrides/Vips.py +++ b/python/packages/gi/overrides/Vips.py @@ -91,13 +91,7 @@ def imageize(match_image, value): # assume this is a pixel constant ... expand into an image using # match as a template - pixel = (Vips.Image.black(1, 1) + value).cast(match_image.format) - image = pixel.embed(0, 0, match_image.width, match_image.height, - extend = Vips.Extend.COPY) - image = image.copy(interpretation = match_image.interpretation, - xres = match_image.xres, - yres = match_image.yres) - return image + return match_image.new_from_image(value) # we'd like to use memoryview to avoid copying things like ICC profiles, but # unfortunately pygobject does not support this ... so for blobs we just use @@ -621,6 +615,29 @@ def add_doc(value): class Image(Vips.Image): # for constructors, see class methods above + def new_from_image(self, value): + """Create a new image based on an existing one. + + A new image is created with the same width, height, format, + interpretation, resolution and offset as self, but with every pixel + having the value of value. + + You can pass an array to create a many-band image. + """ + + # we'd like to call the vips function vips_image_new_from_image() but we + # can't call __getattr__ methods from a subclass + pixel = (Vips.Image.black(1, 1) + value).cast(self.format) + image = pixel.embed(0, 0, self.width, self.height, + extend = Vips.Extend.COPY) + image = image.copy(interpretation = self.interpretation, + xres = self.xres, + yres = self.yres, + xoffset = self.xoffset, + yoffset = self.yoffset) + + return image + # output def write_to_file(self, vips_filename, **kwargs): diff --git a/test/test_iofuncs.py b/test/test_iofuncs.py index 0cfd8f55..b4b377a1 100755 --- a/test/test_iofuncs.py +++ b/test/test_iofuncs.py @@ -62,5 +62,27 @@ class TestIofuncs(unittest.TestCase): for case in cases: self.assertEqualObjects(split(case[0]), case[1]) + def test_new_from_image(self): + im = Vips.Image.mask_ideal(100, 100, 0.5, reject = True, optical = True) + + im2 = im.new_from_image(12) + + self.assertEqual(im2.width, im.width) + self.assertEqual(im2.height, im.height) + self.assertEqual(im2.interpretation, im.interpretation) + self.assertEqual(im2.format, im.format) + self.assertEqual(im2.xres, im.xres) + self.assertEqual(im2.yres, im.yres) + self.assertEqual(im2.xoffset, im.xoffset) + self.assertEqual(im2.yoffset, im.yoffset) + self.assertEqual(im2.bands, 1) + self.assertEqual(im2.avg(), 12) + + im2 = im.new_from_image([1,2,3]) + + self.assertEqual(im2.bands, 3) + self.assertEqual(im2.avg(), 2) + + if __name__ == '__main__': unittest.main()