File: image_test.py

package info (click to toggle)
mapnik 2.2.0%2Bds1-7
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 30,288 kB
  • ctags: 18,382
  • sloc: cpp: 115,128; python: 9,298; xml: 5,692; ansic: 3,726; makefile: 160; sh: 159; lisp: 13
file content (67 lines) | stat: -rw-r--r-- 2,506 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import os, mapnik
from timeit import Timer, time
from nose.tools import *
from utilities import execution_path, run_all

def setup():
    # All of the paths used are relative, if we run the tests
    # from another directory we need to chdir()
    os.chdir(execution_path('.'))

def test_tiff_round_trip():
    filepath = '/tmp/mapnik-tiff-io.tiff'
    im = mapnik.Image(255,267)
    im.background = mapnik.Color('rgba(1,2,3,.5)')
    im.save(filepath,'tiff')
    im2 = mapnik.Image.open(filepath)
    eq_(im.width(),im2.width())
    eq_(im.height(),im2.height())
    eq_(len(im.tostring()),len(im2.tostring()))
    eq_(len(im.tostring('tiff')),len(im2.tostring('tiff')))

def test_jpeg_round_trip():
    filepath = '/tmp/mapnik-jpeg-io.jpeg'
    im = mapnik.Image(255,267)
    im.background = mapnik.Color('rgba(1,2,3,.5)')
    im.save(filepath,'jpeg')
    im2 = mapnik.Image.open(filepath)
    eq_(im.width(),im2.width())
    eq_(im.height(),im2.height())
    eq_(len(im.tostring()),len(im2.tostring()))
    eq_(len(im.tostring('jpeg')),len(im2.tostring('jpeg')))

def test_png_round_trip():
    filepath = '/tmp/mapnik-png-io.png'
    im = mapnik.Image(255,267)
    im.background = mapnik.Color('rgba(1,2,3,.5)')
    im.save(filepath,'png')
    im2 = mapnik.Image.open(filepath)
    eq_(im.width(),im2.width())
    eq_(im.height(),im2.height())
    eq_(len(im.tostring()),len(im2.tostring()))
    eq_(len(im.tostring('png')),len(im2.tostring('png')))
    eq_(len(im.tostring('png8')),len(im2.tostring('png8')))

def test_image_open_from_string():
    filepath = '../data/images/dummy.png'
    im1 = mapnik.Image.open(filepath)
    im2 = mapnik.Image.fromstring(open(filepath,'rb').read())
    eq_(im1.width(),im2.width())
    length = len(im1.tostring())
    eq_(length,len(im2.tostring()))
    eq_(len(mapnik.Image.fromstring(im1.tostring('png')).tostring()),length)
    eq_(len(mapnik.Image.fromstring(im1.tostring('jpeg')).tostring()),length)
    eq_(len(mapnik.Image.frombuffer(buffer(im1.tostring('png'))).tostring()),length)
    eq_(len(mapnik.Image.frombuffer(buffer(im1.tostring('jpeg'))).tostring()),length)
    
    # TODO - https://github.com/mapnik/mapnik/issues/1831
    eq_(len(mapnik.Image.fromstring(im1.tostring('tiff')).tostring()),length)
    eq_(len(mapnik.Image.frombuffer(buffer(im1.tostring('tiff'))).tostring()),length)

if __name__ == "__main__":
    setup()
    run_all(eval(x) for x in dir() if x.startswith("test_"))