test.py 2.92 KB
Newer Older
André Anjos's avatar
André Anjos committed
1
2
3
4
5
6
7
8
9
10
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
# Laurent El Shafey <laurent.el-shafey@idiap.ch>
# Wed Aug 14 12:27:57 CEST 2013
#
# Copyright (C) 2011-2014 Idiap Research Institute, Martigny, Switzerland

"""Runs some image tests
"""

11
12
import os
import numpy
André Anjos's avatar
André Anjos committed
13
from bob.io.base import load, write, test_utils
André Anjos's avatar
André Anjos committed
14
15
16

# These are some global parameters for the test.
PNG_INDEXED_COLOR = test_utils.datafile('img_indexed_color.png', __name__)
17
PNG_INDEXED_COLOR_ALPHA = test_utils.datafile('img_indexed_color_alpha.png', __name__)
18
PNG_RGBA_COLOR = test_utils.datafile('img_rgba_color.png', __name__)
19
20
PNG_GRAY_ALPHA = test_utils.datafile('img_gray_alpha.png', __name__)
PNG_tRNS = test_utils.datafile('img_trns.png', __name__)
André Anjos's avatar
André Anjos committed
21

22

André Anjos's avatar
André Anjos committed
23
24
25
def test_png_indexed_color():
  # Read an indexed color PNG image, and compared with hardcoded values
  img = load(PNG_INDEXED_COLOR)
26
27
28
29
  assert img.shape == (3, 22, 32)
  assert img[0, 0, 0] == 255
  assert img[0, 17, 17] == 117

30
31
32
def test_png_rgba_color():
  # Read an indexed color PNG image, and compared with hardcoded values
  img = load(PNG_RGBA_COLOR)
33
34
35
  assert img.shape == (3, 22, 32)
  assert img[0, 0, 0] == 255
  assert img[0, 17, 17] == 117
36

37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
def test_png_indexed_color_alpha():
  # Read an indexed color+alpha PNG image, and compared with hardcoded values
  img = load(PNG_INDEXED_COLOR_ALPHA)
  assert img.shape == (3,22,32)
  assert img[0,0,0] == 255
  assert img[0,17,17] == 117

def test_png_indexed_trns():
  # Read an tRNS PNG image (without alpha), and compared with hardcoded values
  img = load(PNG_tRNS)
  assert img.shape == (3,22,32)
  assert img[0,0,0] == 255
  assert img[0,17,17] == 117

def test_png_gray_alpha():
  # Read a gray+alpha PNG image, and compared with hardcoded values
  img = load(PNG_GRAY_ALPHA)
  assert img.shape == (22,32)
  assert img[0,0] == 255
  assert img[17,17] == 51


59

60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def transcode(filename):

  tmpname = test_utils.temporary_filename(suffix=os.path.splitext(filename)[1])

  try:
    # complete transcoding test
    image = load(filename)

    # save with the same extension
    write(image, tmpname)

    # reload the image from the file
    image2 = load(tmpname)

    assert numpy.array_equal(image, image2)

  finally:
77
78
79
    if os.path.exists(tmpname):
      os.unlink(tmpname)

80
81
82

def test_netpbm():

83
84
85
86
87
88
89
  transcode(test_utils.datafile('test.pbm', __name__))  # indexed, works fine
  transcode(test_utils.datafile('test.pgm', __name__))  # indexed, works fine
  transcode(test_utils.datafile('test.ppm', __name__))  # indexed, works fine
  transcode(test_utils.datafile('test_2.pgm', __name__))  # indexed, works fine
  transcode(test_utils.datafile('test_2.ppm', __name__))  # indexed, works fine
  # transcode(test_utils.datafile('test.jpg', __name__)) #does not work
  # because of re-compression
90
91
92


def test_cpp_interface():
Manuel Günther's avatar
Manuel Günther committed
93
  from ._library import _test_io
94
95
96
97
98
99
100
101
  import tempfile
  import shutil

  tmpdir = tempfile.mkdtemp(prefix="bob_io_image")
  try:
    _test_io(tmpdir)
  finally:
    shutil.rmtree(tmpdir)