Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _test_read(filename=None):
import sys
import time
if filename is None:
if len(sys.argv) != 2:
print 'Run `libtiff.py ` for testing.'
return
filename = sys.argv[1]
print 'Trying to open', filename, '...',
tiff = TIFF.open(filename)
print 'ok'
print 'Trying to show info ...\n','-'*10
print tiff.info()
print '-'*10,'ok'
print 'Trying show images ...'
t = time.time ()
i = 0
for image in tiff.iter_images(verbose=True):
#print image.min(), image.max(), image.mean ()
i += 1
print '\tok',(time.time ()-t)*1e3,'ms',i,'images'
raise ValueError(text)
def _default_colormap(reverse=False):
# Basic B&W colormap
if reverse:
return [[ x*256 for x in range(255, -1, -1) ]]*3
return [[ x*256 for x in range(256) ]]*3
def _eval_or_none(key, eval_func):
try:
return eval_func(kwargs[key])
except KeyError:
return None
log.info("Creating output file '%s'" % (output_fn,))
tiff = TIFF.open(output_fn, "w")
# Extract keyword arguments
cmap = kwargs.pop("cmap", None)
sat_id = int(kwargs.pop("sat_id"))
chan_id = int(kwargs.pop("chan_id"))
data_source = str(kwargs.pop("data_source"))
tile_width = int(kwargs.pop("tile_width", 512))
tile_length = int(kwargs.pop("tile_length", 512))
data_cat = str(kwargs.pop("data_cat"))
pixel_xres = float(kwargs.pop("pixel_xres"))
pixel_yres = float(kwargs.pop("pixel_yres"))
origin_lat = float(kwargs.pop("origin_lat"))
origin_lon = float(kwargs.pop("origin_lon"))
image_dt = kwargs.pop("image_dt")
projection = str(kwargs.pop("projection"))
meridian_west = float(kwargs.pop("meridian_west", 0.0))
def _test_copy():
tiff = TIFF.open('/tmp/libtiff_test_compression.tiff', mode='w')
arr = np.zeros ((5,6), np.uint32)
for i in range(arr.shape[0]):
for j in range (arr.shape[1]):
arr[i,j] = 1+i + 10*j
#from scipy.stats import poisson
#arr = poisson.rvs (arr)
tiff.SetField('ImageDescription', 'Hey\nyou')
tiff.write_image(arr, compression='lzw')
del tiff
tiff = TIFF.open('/tmp/libtiff_test_compression.tiff', mode='r')
print tiff.info()
arr2 = tiff.read_image()
assert (arr==arr2).all(),'arrays not equal'
def _tag_read():
a = TIFF.open("/tmp/libtiff_test_custom_tags.tif", "r")
tmp = a.read_image()
assert tmp.shape==(512,512),"Image read was wrong shape (%r instead of (512,512))" % (tmp.shape,)
tmp = a.GetField("XPOSITION")
assert tmp == 42.0,"XPosition was not read as 42.0"
tmp = a.GetField("ARTIST")
assert tmp=="MY NAME","Artist was not read as 'MY NAME'"
tmp = a.GetField("LibtiffTestByte")
assert tmp==42,"LibtiffTestbyte was not read as 42"
tmp = a.GetField("LibtiffTestuint16")
assert tmp==42,"LibtiffTestuint16 was not read as 42"
tmp = a.GetField("LibtiffTestMultiuint32")
assert tmp==[1,2,3,4,5,6,7,8,9,10],"LibtiffTestMultiuint32 was not read as [1,2,3,4,5,6,7,8,9,10]"
tmp = a.GetField("LibtiffTeststr")
assert tmp=="FAKE","LibtiffTeststr was not read as 'FAKE'"
tmp = a.GetField("PRIMARYCHROMATICITIES")
assert (arr==arr2).all(),'arrays not equal'
for compression in ['none','lzw','deflate']:
for sampleformat in ['int','uint','float']:
for bitspersample in [256,128,64,32,16,8]:
if sampleformat=='float' and (bitspersample < 32 or bitspersample > 128):
continue
if sampleformat in ['int','uint'] and bitspersample > 64:
continue
#print compression, sampleformat, bitspersample
tiff.copy ('/tmp/libtiff_test_copy2.tiff',
compression=compression,
imagedescription='hoo',
sampleformat=sampleformat,
bitspersample=bitspersample)
tiff2 = TIFF.open('/tmp/libtiff_test_copy2.tiff', mode='r')
arr3 = tiff2.read_image()
assert (arr==arr3).all(),'arrays not equal %r' % ((compression, sampleformat, bitspersample),)
print 'test copy ok'
def _tag_write():
a = TIFF.open("/tmp/libtiff_test_custom_tags.tif", "w")
a.SetField("ARTIST", "MY NAME")
a.SetField("LibtiffTestByte", 42)
a.SetField("LibtiffTeststr", "FAKE")
a.SetField("LibtiffTestuint16", 42)
a.SetField("LibtiffTestMultiuint32", (1,2,3,4,5,6,7,8,9,10))
a.SetField("XPOSITION", 42.0)
a.SetField("PRIMARYCHROMATICITIES", (1.0, 2, 3, 4, 5, 6))
arr = numpy.ones((512,512), dtype=numpy.uint8)
arr[:,:] = 255
a.write_image(arr)
print "Tag Write: SUCCESS"
def _test_write_float():
tiff = TIFF.open('/tmp/libtiff_test_write.tiff', mode='w')
arr = np.zeros ((5,6), np.float64)
for i in range(arr.shape[0]):
for j in range (arr.shape[1]):
arr[i,j] = i + 10*j
print arr
tiff.write_image(arr)
del tiff
tiff = TIFF.open('/tmp/libtiff_test_write.tiff', mode='r')
print tiff.info()
arr2 = tiff.read_image()
print arr2
def _test_copy():
tiff = TIFF.open('/tmp/libtiff_test_compression.tiff', mode='w')
arr = np.zeros ((5,6), np.uint32)
for i in range(arr.shape[0]):
for j in range (arr.shape[1]):
arr[i,j] = 1+i + 10*j
#from scipy.stats import poisson
#arr = poisson.rvs (arr)
tiff.SetField('ImageDescription', 'Hey\nyou')
tiff.write_image(arr, compression='lzw')
del tiff
tiff = TIFF.open('/tmp/libtiff_test_compression.tiff', mode='r')
print tiff.info()
arr2 = tiff.read_image()
assert (arr==arr2).all(),'arrays not equal'
for compression in ['none','lzw','deflate']:
for sampleformat in ['int','uint','float']:
for bitspersample in [256,128,64,32,16,8]:
if sampleformat=='float' and (bitspersample < 32 or bitspersample > 128):
continue
if sampleformat in ['int','uint'] and bitspersample > 64:
continue
#print compression, sampleformat, bitspersample
tiff.copy ('/tmp/libtiff_test_copy2.tiff',
compression=compression,
imagedescription='hoo',
def __init__(self, filename, mode='r'):
"""Open a tiff file.
see: libtiff.TIFF.open()
"""
self.tiff = TIFF.open(filename, mode)
self.tiff.ninjo_tags_dict = ninjo_tags_dict
self.tiff.ninjo_tags = ninjo_tags