예제 #1
0
    def test_webcolor_named(self):
        self.assertEqual(rgba.webcolor('red'),
                         rgba(255, 0, 0, 255))
        self.assertEqual(rgba.webcolor('RED'),
                         rgba(255, 0, 0, 255))

        # http://en.wikipedia.org/wiki/The_Colour_of_Magic
        self.assertRaises(ValueError, rgba.webcolor, 'octarine')
예제 #2
0
    def test_webcolor_hex(self):
        # Abbreviated
        self.assertEqual(rgba.webcolor('#0f0'), rgba(0, 255, 0, 255))
        self.assertEqual(rgba.webcolor('#0F0'), rgba(0, 255, 0, 255))

        # Full
        self.assertEqual(rgba.webcolor('#0000ff'), rgba(0, 0, 255, 255))
        self.assertEqual(rgba.webcolor('#0000FF'), rgba(0, 0, 255, 255))

        # No hash in front
        self.assertRaises(ValueError, rgba.webcolor, '0000ff')
예제 #3
0
    def test_webcolor_hex(self):
        # Abbreviated
        self.assertEqual(rgba.webcolor('#0f0'),
                         rgba(0, 255, 0, 255))
        self.assertEqual(rgba.webcolor('#0F0'),
                         rgba(0, 255, 0, 255))

        # Full
        self.assertEqual(rgba.webcolor('#0000ff'),
                         rgba(0, 0, 255, 255))
        self.assertEqual(rgba.webcolor('#0000FF'),
                         rgba(0, 0, 255, 255))

        # No hash in front
        self.assertRaises(ValueError, rgba.webcolor, '0000ff')
예제 #4
0
    def test_align_to_grid(self):
        with LibVips.disable_warnings():
            # bluemarble.tif is a 1024 × 1024 whole-world map.
            dataset = VipsDataset(inputfile=self.inputfile)
            dataset.align_to_grid()
            self.assertEqual(dataset.image.Xsize(), 1024)
            self.assertEqual(dataset.image.Ysize(), 1024)
            self.assertEqual(dataset.RasterXSize, 1024)
            self.assertEqual(dataset.RasterYSize, 1024)
            self.assertExtentsEqual(dataset.GetExtents(),
                                    dataset.GetTiledExtents())

            # bluemarble-foreign.tif is a 500 × 250 whole-world map.
            dataset = VipsDataset(inputfile=self.foreignfile)
            dataset.align_to_grid()
            self.assertEqual(dataset.image.Xsize(), 512)
            self.assertEqual(dataset.image.Ysize(), 512)
            self.assertEqual(dataset.RasterXSize, 512)
            self.assertEqual(dataset.RasterYSize, 512)
            self.assertEqual(dataset.GetExtents(), dataset.GetTiledExtents())

            # bluemarble-spanning-foreign.tif is a 154 × 154 whole-world map.
            dataset = VipsDataset(inputfile=self.spanningforeignfile)
            dataset.align_to_grid()
            self.assertEqual(dataset.image.Xsize(), 256)
            self.assertEqual(dataset.image.Ysize(), 256)
            self.assertEqual(dataset.RasterXSize, 256)
            self.assertEqual(dataset.RasterYSize, 256)
            self.assertExtentsEqual(dataset.GetExtents(),
                                    dataset.GetTiledExtents())
            # The upper-left corner should be transparent
            data = numpy.frombuffer(dataset.image.tobuffer(),
                                    dtype=numpy.uint8)
            self.assertEqual(tuple(data[0:4]), rgba(0, 0, 0, 0))
예제 #5
0
    def test_save(self):
        image = VImage.new_rgba(width=1, height=1,
                                ink=rgba(r=0, g=0, b=0, a=0))
        self.storage.save(x=0, y=1, z=2, image=image)
        self.storage.save(x=1, y=0, z=2, image=image)
        self.storage.save(x=1, y=0, z=3, image=image)
        self.storage.waitall()
        self.assertEqual(set(recursive_listdir(self.outputdir)),
                         set(['2/',
                              '2/0/',
                              '2/0/1.png',
                              '2/1/',
                              '2/1/0.png',
                              '3/',
                              '3/1/',
                              '3/1/0.png']))

        # Is this a real file?
        self.assertFalse(
            os.path.islink(os.path.join(self.outputdir, '2', '0', '1.png'))
        )

        # Does the symlinking work?
        self.assertEqual(
            os.readlink(os.path.join(self.outputdir, '2', '1', '0.png')),
            os.path.join(os.path.pardir, '0', '1.png')
        )
        self.assertEqual(
            os.readlink(os.path.join(self.outputdir, '3', '1', '0.png')),
            os.path.join(os.path.pardir, os.path.pardir, '2', '0', '1.png')
        )
예제 #6
0
    def test_simple(self):
        renderer = JpegRenderer()

        # Black 1×1 image
        image = VImage.new_rgba(width=1, height=1,
                                ink=rgba(r=0, g=0, b=0, a=255))

        black = renderer.render(image=image)
        black_md5 = intmd5(black)

        # Transparent 1×1 image
        image = VImage.new_rgba(width=1, height=1,
                                ink=rgba(r=0, g=0, b=0, a=0))

        transparent = renderer.render(image=image)
        self.assertEqual(intmd5(transparent), black_md5)
예제 #7
0
    def test_save(self):
        image = VImage.new_rgba(width=1, height=1,
                                ink=rgba(r=0, g=0, b=0, a=0))
        self.storage.save(x=0, y=1, z=2, image=image)
        self.storage.save(x=1, y=0, z=2, image=image)
        self.storage.waitall()
        self.assertEqual(set(os.listdir(self.outputdir)),
                         set([
                             '2-0-1-f1d3ff8443297732862df21dc4e57262.png',
                             '2-1-0-f1d3ff8443297732862df21dc4e57262.png'
                         ]))

        # Is this a real file?
        self.assertFalse(
            os.path.islink(os.path.join(
                self.outputdir, '2-0-1-f1d3ff8443297732862df21dc4e57262.png'
            ))
        )

        # Does the symlinking work?
        self.assertEqual(
            os.readlink(os.path.join(
                self.outputdir, '2-1-0-f1d3ff8443297732862df21dc4e57262.png'
            )),
            '2-0-1-f1d3ff8443297732862df21dc4e57262.png'
        )
예제 #8
0
    def test_simple(self):
        renderer = JpegRenderer()

        # Black 1×1 image
        image = VImage.new_rgba(width=1,
                                height=1,
                                ink=rgba(r=0, g=0, b=0, a=255))

        black = renderer.render(image=image)
        black_md5 = intmd5(black)

        # Transparent 1×1 image
        image = VImage.new_rgba(width=1,
                                height=1,
                                ink=rgba(r=0, g=0, b=0, a=0))

        transparent = renderer.render(image=image)
        self.assertEqual(intmd5(transparent), black_md5)
예제 #9
0
    def test_gradient_3(self):
        # Three colors - one gradient split in half
        dark_red = rgba(127, 0, 0, 255)
        colors = ColorGradient({0: self.red, 128: dark_red, 255: self.black})
        self.assertEqual(
            colors._clauses(band='r'),
            [('n >= 0', '-1.0 * n + {0}'.format(float(self.red.r))),
             ('n >= 255', self.black.r)])
        self.assertEqual(colors._clauses(band='g'), [])
        self.assertEqual(colors._clauses(band='a'), [('n >= 0', self.red.a)])
        self.assertEqual(
            colors._expression(band='r'), 'where(n >= 255, {black}, '
            'where(n >= 0, -1.0 * n + {red}, {false}))'.format(
                black=self.black.r,
                red=float(self.red.r),
                false=ColorGradient.BACKGROUND.r))
        self.assertEqual(colors._expression(band='g'), None)
        self.assertEqual(
            colors._expression(band='a'),
            'where(n >= 0, {red}, {false})'.format(
                red=self.red.a, false=ColorGradient.BACKGROUND.a))

        # Three colors - one gradient split in half
        dark_red = rgba(127, 0, 0, 255)
        colors = ColorGradient({0: self.red, 64: dark_red, 255: self.black})
        self.assertEqual(
            colors._clauses(band='r'),
            [('n >= 0', '-0.5 * n + {0}'.format(float(self.red.r))),
             ('n >= 64', '-1.5039370078740157 * n + 223.251968503937'),
             ('n >= 255', self.black.r)])
        self.assertEqual(colors._clauses(band='g'), [])
        self.assertEqual(colors._clauses(band='a'), [('n >= 0', self.red.a)])
        self.assertEqual(
            colors._expression(band='r'), 'where(n >= 255, {black}, '
            'where(n >= 64, -1.5039370078740157 * n + 223.251968503937, '
            'where(n >= 0, -0.5 * n + {red}, {false})))'.format(
                black=self.black.r,
                red=float(self.red.r),
                false=ColorGradient.BACKGROUND.r))
        self.assertEqual(colors._expression(band='g'), None)
        self.assertEqual(
            colors._expression(band='a'),
            'where(n >= 0, {red}, {false})'.format(
                red=self.red.a, false=ColorGradient.BACKGROUND.a))
예제 #10
0
 def setUp(self):
     self.transparent = rgba(0, 0, 0, 0)
     self.black = rgba(0, 0, 0, 255)
     self.red = rgba(255, 0, 0, 255)
     self.green = rgba(0, 255, 0, 255)
     self.blue = rgba(0, 0, 255, 255)
     self.white = rgba(255, 255, 255, 255)
예제 #11
0
 def setUp(self):
     self.transparent = rgba(0, 0, 0, 0)
     self.black = rgba(0, 0, 0, 255)
     self.red = rgba(255, 0, 0, 255)
     self.green = rgba(0, 255, 0, 255)
     self.blue = rgba(0, 0, 255, 255)
     self.white = rgba(255, 255, 255, 255)
예제 #12
0
    def test_save(self):
        # We must create this on disk
        self.storage = MbtilesStorage.create(renderer=self.renderer,
                                             filename=self.tempfile.name,
                                             metadata=self.metadata)

        # Transparent 1×1 image
        image = VImage.new_rgba(width=1, height=1,
                                ink=rgba(r=0, g=0, b=0, a=0))

        # Save it twice, assuming that MBTiles will deduplicate
        self.storage.save(x=0, y=1, z=2, image=image)
        self.storage.save(x=1, y=0, z=2, image=image)
        self.storage.waitall()

        # Assert that things were saved properly
        self.assertEqual(
            [(z, x, y, intmd5(data))
             for z, x, y, data in self.storage.mbtiles.all()],
            [
                (2, 0, 1, 89446660811628514001822794642426893173),
                (2, 1, 0, 89446660811628514001822794642426893173),
            ]
        )

        # Close the existing database.
        self.storage.mbtiles.close()

        # Re-open the created file
        storage = MbtilesStorage(renderer=self.renderer,
                                 filename=self.tempfile.name)

        # Read out of the backend
        self.assertEqual(
            [(z, x, y, intmd5(data))
             for z, x, y, data in storage.mbtiles.all()],
            [
                (2, 0, 1, 89446660811628514001822794642426893173),
                (2, 1, 0, 89446660811628514001822794642426893173),
            ]
        )
예제 #13
0
    def test_align_to_grid(self):
        with LibVips.disable_warnings():
            # bluemarble.tif is a 1024 × 1024 whole-world map.
            dataset = VipsDataset(inputfile=self.inputfile)
            dataset.align_to_grid()
            self.assertEqual(dataset.image.Xsize(), 1024)
            self.assertEqual(dataset.image.Ysize(), 1024)
            self.assertEqual(dataset.RasterXSize, 1024)
            self.assertEqual(dataset.RasterYSize, 1024)
            self.assertExtentsEqual(dataset.GetExtents(),
                                    dataset.GetTiledExtents())

            # bluemarble-foreign.tif is a 500 × 250 whole-world map.
            dataset = VipsDataset(inputfile=self.foreignfile)
            dataset.align_to_grid()
            self.assertEqual(dataset.image.Xsize(), 512)
            self.assertEqual(dataset.image.Ysize(), 512)
            self.assertEqual(dataset.RasterXSize, 512)
            self.assertEqual(dataset.RasterYSize, 512)
            self.assertEqual(dataset.GetExtents(),
                             dataset.GetTiledExtents())

            # bluemarble-spanning-foreign.tif is a 154 × 154 whole-world map.
            dataset = VipsDataset(inputfile=self.spanningforeignfile)
            dataset.align_to_grid()
            self.assertEqual(dataset.image.Xsize(), 256)
            self.assertEqual(dataset.image.Ysize(), 256)
            self.assertEqual(dataset.RasterXSize, 256)
            self.assertEqual(dataset.RasterYSize, 256)
            self.assertExtentsEqual(dataset.GetExtents(),
                                    dataset.GetTiledExtents())
            # The upper-left corner should be transparent
            data = numpy.frombuffer(dataset.image.tobuffer(),
                                    dtype=numpy.uint8)
            self.assertEqual(tuple(data[0:4]),
                             rgba(0, 0, 0, 0))
예제 #14
0
 def setUp(self):
     # Transparent 1×1 image
     self.image = VImage.new_rgba(width=1, height=1,
                                  ink=rgba(r=0, g=0, b=0, a=0))
예제 #15
0
 def setUp(self):
     # Transparent 1×1 image
     self.image = VImage.new_rgba(width=1,
                                  height=1,
                                  ink=rgba(r=0, g=0, b=0, a=0))
예제 #16
0
 def test_get_hash(self):
     image = VImage.new_rgba(width=1, height=1,
                             ink=rgba(r=0, g=0, b=0, a=0))
     self.assertEqual(self.storage.get_hash(image=image),
                      long('f1d3ff8443297732862df21dc4e57262', base=16))
예제 #17
0
    def test_webcolor_named(self):
        self.assertEqual(rgba.webcolor('red'), rgba(255, 0, 0, 255))
        self.assertEqual(rgba.webcolor('RED'), rgba(255, 0, 0, 255))

        # http://en.wikipedia.org/wiki/The_Colour_of_Magic
        self.assertRaises(ValueError, rgba.webcolor, 'octarine')
예제 #18
0
 def test_create(self):
     self.assertEqual(rgba(0, 0, 0),
                      rgba(0, 0, 0, 255))
예제 #19
0
    def test_gradient_3(self):
        # Three colors - one gradient split in half
        dark_red = rgba(127, 0, 0, 255)
        colors = ColorGradient({0: self.red,
                                128: dark_red,
                                255: self.black})
        self.assertEqual(
            colors._clauses(band='r'),
            [('n >= 0', '-1.0 * n + {0}'.format(float(self.red.r))),
             ('n >= 255', self.black.r)]
        )
        self.assertEqual(colors._clauses(band='g'),
                         [])
        self.assertEqual(colors._clauses(band='a'),
                         [('n >= 0', self.red.a)])
        self.assertEqual(colors._expression(band='r'),
                         'where(n >= 255, {black}, '
                         'where(n >= 0, -1.0 * n + {red}, {false}))'.format(
                             black=self.black.r,
                             red=float(self.red.r),
                             false=ColorGradient.BACKGROUND.r
                         ))
        self.assertEqual(colors._expression(band='g'),
                         None)
        self.assertEqual(colors._expression(band='a'),
                         'where(n >= 0, {red}, {false})'.format(
                             red=self.red.a,
                             false=ColorGradient.BACKGROUND.a
                         ))

        # Three colors - one gradient split in half
        dark_red = rgba(127, 0, 0, 255)
        colors = ColorGradient({0: self.red,
                                64: dark_red,
                                255: self.black})
        self.assertEqual(
            colors._clauses(band='r'),
            [('n >= 0', '-0.5 * n + {0}'.format(float(self.red.r))),
             ('n >= 64', '-1.5039370078740157 * n + 223.251968503937'),
             ('n >= 255', self.black.r)]
        )
        self.assertEqual(colors._clauses(band='g'),
                         [])
        self.assertEqual(colors._clauses(band='a'),
                         [('n >= 0', self.red.a)])
        self.assertEqual(
            colors._expression(band='r'),
            'where(n >= 255, {black}, '
            'where(n >= 64, -1.5039370078740157 * n + 223.251968503937, '
            'where(n >= 0, -0.5 * n + {red}, {false})))'.format(
                black=self.black.r,
                red=float(self.red.r),
                false=ColorGradient.BACKGROUND.r
            ))
        self.assertEqual(colors._expression(band='g'),
                         None)
        self.assertEqual(colors._expression(band='a'),
                         'where(n >= 0, {red}, {false})'.format(
                             red=self.red.a,
                             false=ColorGradient.BACKGROUND.a
                         ))
예제 #20
0
 def test_create(self):
     self.assertEqual(rgba(0, 0, 0), rgba(0, 0, 0, 255))