<s> import numpy as np <EOL> from os import path <EOL> from nose . tools import with_setup <EOL> _test_dir = None <EOL> _testimgname = None <EOL> try : <EOL> from mahotas . io import freeimage <EOL> except OSError : <EOL> from nose import SkipTest <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> def _create_tempdir ( ) : <EOL> import tempfile <EOL> global _test_dir , _testimgname <EOL> _test_dir = tempfile . mkdtemp ( prefix = '<STR_LIT>' ) <EOL> _testimgname = path . join ( _test_dir , "<STR_LIT>" ) <EOL> def _remove_tempdir ( ) : <EOL> from shutil import rmtree <EOL> rmtree ( _test_dir ) <EOL> create_remove = with_setup ( setup = _create_tempdir , teardown = _remove_tempdir ) <EOL> @ create_remove <EOL> def test_freeimage ( ) : <EOL> img = np . arange ( <NUM_LIT> ) . reshape ( ( <NUM_LIT:16> , <NUM_LIT:16> ) ) . astype ( np . uint8 ) <EOL> freeimage . imsave ( _testimgname , img ) <EOL> img_ = freeimage . imread ( _testimgname ) <EOL> assert img . shape == img_ . shape <EOL> assert np . all ( img == img_ ) <EOL> @ create_remove <EOL> def test_as_grey ( ) : <EOL> colour = np . arange ( <NUM_LIT:16> * <NUM_LIT:16> * <NUM_LIT:3> ) . reshape ( ( <NUM_LIT:16> , <NUM_LIT:16> , <NUM_LIT:3> ) ) <EOL> freeimage . imsave ( _testimgname , colour . astype ( np . uint8 ) ) <EOL> c2 = freeimage . imread ( _testimgname , as_grey = True ) <EOL> assert len ( c2 . shape ) == <NUM_LIT:2> <EOL> assert c2 . shape == colour . shape [ : - <NUM_LIT:1> ] <EOL> def test_rgba ( ) : <EOL> rgba = path . join ( <EOL> path . dirname ( __file__ ) , <EOL> '<STR_LIT:data>' , <EOL> '<STR_LIT>' ) <EOL> rgba = freeimage . imread ( rgba ) <EOL> assert np . all ( np . diff ( rgba [ : , : , <NUM_LIT:3> ] . mean ( <NUM_LIT:1> ) ) < <NUM_LIT:0> ) <EOL> @ create_remove <EOL> def test_save_load_rgba ( ) : <EOL> img = np . arange ( <NUM_LIT> ) . reshape ( ( <NUM_LIT:8> , <NUM_LIT:8> , <NUM_LIT:4> ) ) . astype ( np . uint8 ) <EOL> freeimage . imsave ( _testimgname , img ) <EOL> img_ = freeimage . imread ( _testimgname ) <EOL> assert img . shape == img_ . shape <EOL> assert np . all ( img == img_ ) <EOL> def test_fromblob ( ) : <EOL> img = np . arange ( <NUM_LIT:100> , dtype = np . uint8 ) . reshape ( ( <NUM_LIT:10> , <NUM_LIT:10> ) ) <EOL> s = freeimage . imsavetoblob ( img , '<STR_LIT>' ) <EOL> assert np . all ( freeimage . imreadfromblob ( s ) == img ) <EOL> s = freeimage . imsavetoblob ( img , '<STR_LIT>' ) <EOL> assert np . all ( freeimage . imreadfromblob ( s ) == img ) <EOL> def test_1bpp ( ) : <EOL> bpp = path . join ( <EOL> path . dirname ( __file__ ) , <EOL> '<STR_LIT:data>' , <EOL> '<STR_LIT>' ) <EOL> bpp = freeimage . imread ( bpp ) <EOL> assert bpp . sum ( ) <EOL> assert bpp . sum ( ) < bpp . size <EOL> @ create_remove <EOL> def test_multi ( ) : <EOL> testtif = _test_dir + '<STR_LIT>' <EOL> f = np . zeros ( ( <NUM_LIT:16> , <NUM_LIT:16> ) , np . uint8 ) <EOL> fs = [ ] <EOL> for t in range ( <NUM_LIT:8> ) : <EOL> f [ : t , : t ] = t <EOL> fs . append ( f . copy ( ) ) <EOL> freeimage . write_multipage ( fs , testtif ) <EOL> fs2 = freeimage . read_multipage ( testtif ) <EOL> for f , f2 in zip ( fs , fs2 ) : <EOL> assert np . all ( f == f2 ) <EOL> @ create_remove <EOL> def test_uint16 ( ) : <EOL> img = np . zeros ( ( <NUM_LIT:32> , <NUM_LIT:32> ) , dtype = np . uint16 ) <EOL> freeimage . imsave ( _testimgname , img ) <EOL> img_ = freeimage . imread ( _testimgname ) <EOL> assert img . shape == img_ . shape <EOL> assert img . dtype == img_ . dtype <EOL> assert np . all ( img == img_ ) </s>
