def test_write_lzw(): for itype in [ uint8, uint16, uint32, uint64, int8, int16, int32, int64, float32, float64, complex64, complex128 ]: #image = array([[1,2,3], [4,5,6]], itype) image = array([range(10000)], itype) #image = array([[0]*14000], itype) fn = mktemp('.tif') tif = TIFFimage(image) tif.write_file(fn, compression='lzw') del tif #os.system('wc %s; echo %s' % (fn, image.nbytes)) tif = TIFF.open(fn, 'r') image2 = tif.read_image() tif.close() #os.remove(fn) atexit.register(os.remove, fn) for i in range(image.size): if image.flat[i] != image2.flat[i]: print ` i, image.flat[i - 5:i + 5].view( dtype=uint8), image2.flat[i - 5:i + 5].view(dtype=uint8) ` break assert image.dtype == image2.dtype assert (image == image2).all()
def test_rw_rgb(): itype = uint8 dt = dtype(dict(names=list('rgb'), formats=[itype]*3)) image = zeros((2, 3), dtype=dt) image['r'][:, 0] = 250 image['g'][:, 1] = 251 image['b'][:, 2] = 252 fn = mktemp('.tif') tif = TIFFimage(image) tif.write_file(fn, compression='lzw') # , samples='rgb') del tif tif = TIFFfile(fn) data, names = tif.get_samples() tif.close() atexit.register(os.remove, fn) print(image) print(data) assert itype == data[0].dtype, repr((itype, data[0].dtype)) assert (image['r'] == data[0]).all() assert (image['g'] == data[1]).all() assert (image['b'] == data[2]).all()
def test_write_read(): for compression in [None, 'lzw']: for itype in [ uint8, uint16, uint32, uint64, int8, int16, int32, int64, float32, float64, complex64, complex128 ]: image = array([[1, 2, 3], [4, 5, 6]], itype) fn = mktemp('.tif') if 0: tif = TIFF.open(fn, 'w') tif.write_image(image, compression=compression) tif.close() else: tif = TIFFimage(image) tif.write_file(fn, compression=compression) del tif tif = TIFFfile(fn) data, names = tif.get_samples() assert names == ['sample0'], repr(names) assert len(data) == 1, repr(len(data)) assert image.dtype == data[0].dtype, repr( (image.dtype, data[0].dtype)) assert (image == data[0]).all() tif.close() atexit.register(os.remove, fn)
def test_write_lzw(): for itype in [uint8, uint16, uint32, uint64, int8, int16, int32, int64, float32, float64, complex64, complex128]: #image = array([[1,2,3], [4,5,6]], itype) image = array([range(10000)], itype) #image = array([[0]*14000], itype) fn = mktemp('.tif') tif = TIFFimage(image) tif.write_file(fn, compression='lzw') del tif #os.system('wc %s; echo %s' % (fn, image.nbytes)) tif = TIFF.open(fn,'r') image2 = tif.read_image() tif.close() #os.remove(fn) atexit.register(os.remove, fn) for i in range(image.size): if image.flat[i] != image2.flat[i]: print `i, image.flat[i-5:i+5].view(dtype=uint8),image2.flat[i-5:i+5].view(dtype=uint8)` break assert image.dtype==image2.dtype assert (image==image2).all()
def test_issue69(): itype = np.uint32 image = np.array([[[1, 2, 3], [4, 5, 6]]], itype) fn = mktemp('issue69.tif') tif = TIFFimage(image) tif.write_file(fn) del tif tif = lt.TIFF3D.open(fn) tif.close() atexit.register(os.remove, fn)
def test_simple_slicing(): for planar_config in [1, 2]: for compression in [None, 'lzw']: for itype in [ uint8, uint16, uint32, uint64, int8, int16, int32, int64, float32, float64, complex64, complex128 ]: image = random.randint(0, 100, size=(10, 6, 7)).astype(itype) fn = mktemp('.tif') if 0: if planar_config == 2: continue tif = TIFF.open(fn, 'w') tif.write_image(image, compression=compression) tif.close() else: tif = TIFFimage(image) tif.write_file(fn, compression=compression, planar_config=planar_config) del tif tif = TIFFfile(fn) arr = tif.get_tiff_array() data = arr[:] assert len(data) == len(image), repr(len(data)) assert image.dtype == data.dtype, repr( (image.dtype, data[0].dtype)) assert (image == data).all() assert arr.shape == image.shape _indices = [0, slice(None), slice(0, 2), slice(0, 5, 2)] for _i0 in _indices[:1]: for i1 in _indices: for i2 in _indices: sl = (_i0, i1, i2) assert (arr[sl] == image[sl]).all(), repr(sl) tif.close() atexit.register(os.remove, fn)
def test_issue19(): size = 1024 * 32 # 1GB # size = 1024*63 # almost 4GB, test takes about 60 seconds but succeeds image = ones((size, size), dtype=uint8) print('image size:', image.nbytes / 1024**2, 'MB') fn = mktemp('issue19.tif') tif = TIFFimage(image) try: tif.write_file(fn) except OSError as msg: if 'Not enough storage is available to process this command'\ in str(msg): # Happens in Appveyour CI del tif atexit.register(os.remove, fn) return else: raise del tif tif = TIFFfile(fn) tif.get_tiff_array()[:] # expected failure tif.close() atexit.register(os.remove, fn)