added new_from_image() to python

and tests
This commit is contained in:
John Cupitt 2017-04-28 18:27:15 +01:00
parent 7882602dfe
commit 04353f8915
5 changed files with 54 additions and 13 deletions

View File

@ -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

9
TODO
View File

@ -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<double>({1, 2}))
check bandjoin_const use in Python too
- not sure about utf8 error messages on win
- strange:

View File

@ -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
<code>help(Vips.Image)</code> 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.
</para>
<para>

View File

@ -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):

View File

@ -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()