2015-05-20 02:45:15 +05:30
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
|
|
|
|
2016-03-01 22:55:14 +05:30
|
|
|
import os,sys
|
2015-05-20 02:45:15 +05:30
|
|
|
import numpy as np
|
|
|
|
from optparse import OptionParser
|
2015-09-24 14:54:42 +05:30
|
|
|
from PIL import Image
|
2015-05-20 02:45:15 +05:30
|
|
|
import damask
|
|
|
|
|
2016-01-27 22:36:00 +05:30
|
|
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
|
|
|
scriptID = ' '.join([scriptName,damask.version])
|
2015-05-20 02:45:15 +05:30
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
|
|
|
Generate PNG image from data in given column vector containing RGB tuples.
|
|
|
|
|
|
|
|
""", version = scriptID)
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-l','--label',
|
|
|
|
dest = 'label',
|
|
|
|
type = 'string', metavar = 'string',
|
|
|
|
help = 'column containing RGB triplet')
|
|
|
|
parser.add_option('-d','--dimension',
|
|
|
|
dest = 'dimension',
|
|
|
|
type = 'int', nargs = 2, metavar = 'int int',
|
|
|
|
help = 'data dimension (width height)')
|
|
|
|
parser.add_option('--fliplr',
|
|
|
|
dest = 'flipLR',
|
|
|
|
action = 'store_true',
|
|
|
|
help = 'flip around vertical axis')
|
|
|
|
parser.add_option('--flipud',
|
|
|
|
dest = 'flipUD',
|
|
|
|
action = 'store_true',
|
|
|
|
help = 'flip around horizontal axis')
|
|
|
|
parser.add_option('--crop',
|
|
|
|
dest = 'crop',
|
|
|
|
type = 'int', nargs = 4, metavar = ' '.join(['int']*4),
|
|
|
|
help = 'pixels cropped on left, right, top, bottom')
|
|
|
|
parser.add_option('-N','--pixelsize',
|
|
|
|
dest = 'pixelsize',
|
|
|
|
type = 'int', metavar = 'int',
|
|
|
|
help = 'pixels per data point')
|
|
|
|
parser.add_option('-x','--pixelsizex',
|
|
|
|
dest = 'pixelsizex',
|
|
|
|
type = 'int', metavar = 'int',
|
|
|
|
help = 'pixels per data point along x')
|
|
|
|
parser.add_option('-y','--pixelsizey',
|
|
|
|
dest = 'pixelsizey',
|
|
|
|
type = 'int', metavar = 'int',
|
|
|
|
help = 'pixels per data point along y')
|
|
|
|
parser.add_option('--show',
|
|
|
|
dest = 'show',
|
|
|
|
action = 'store_true',
|
|
|
|
help = 'show resulting image')
|
2015-05-20 02:45:15 +05:30
|
|
|
|
|
|
|
parser.set_defaults(label = None,
|
|
|
|
dimension = [],
|
|
|
|
flipLR = False,
|
|
|
|
flipUD = False,
|
|
|
|
crop = [0,0,0,0],
|
2015-06-17 12:22:43 +05:30
|
|
|
pixelsize = 1,
|
2015-05-20 02:45:15 +05:30
|
|
|
pixelsizex = 1,
|
|
|
|
pixelsizey = 1,
|
|
|
|
show = False,
|
|
|
|
)
|
|
|
|
|
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
|
|
|
|
if options.dimension == []: parser.error('dimension of data array missing')
|
|
|
|
if options.pixelsize > 1: (options.pixelsizex,options.pixelsizey) = [options.pixelsize]*2
|
|
|
|
|
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2015-08-21 01:12:05 +05:30
|
|
|
if filenames == []: filenames = [None]
|
2015-05-20 02:45:15 +05:30
|
|
|
|
|
|
|
for name in filenames:
|
2015-08-21 01:12:05 +05:30
|
|
|
try:
|
|
|
|
table = damask.ASCIItable(name = name,
|
|
|
|
buffered = False,
|
|
|
|
labeled = options.label != None,
|
|
|
|
readonly = True)
|
|
|
|
except: continue
|
2015-09-24 14:54:42 +05:30
|
|
|
damask.util.report(scriptName,name)
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ read header ------------------------------------------
|
|
|
|
|
|
|
|
table.head_read()
|
2015-05-20 02:45:15 +05:30
|
|
|
|
2015-05-21 04:51:35 +05:30
|
|
|
# ------------------------------------------ process data ------------------------------------------
|
2015-05-20 02:45:15 +05:30
|
|
|
|
2015-06-17 12:22:43 +05:30
|
|
|
errors = []
|
|
|
|
|
|
|
|
missing_labels = table.data_readArray(options.label)
|
2015-05-21 04:51:35 +05:30
|
|
|
if len(missing_labels) > 0:
|
2015-08-08 00:33:26 +05:30
|
|
|
errors.append('column{} {} not found'.format('s' if len(missing_labels) > 1 else '',
|
|
|
|
', '.join(missing_labels)))
|
2015-06-17 12:22:43 +05:30
|
|
|
if table.label_dimension(options.label) != 3:
|
2015-08-08 00:33:26 +05:30
|
|
|
errors.append('column {} has wrong dimension'.format(options.label))
|
2015-06-17 12:22:43 +05:30
|
|
|
|
|
|
|
if errors != []:
|
2015-09-24 14:54:42 +05:30
|
|
|
damask.util.croak(errors)
|
2015-05-20 02:45:15 +05:30
|
|
|
table.close(dismiss = True) # close ASCII table file handles and delete output file
|
|
|
|
continue
|
|
|
|
|
2015-05-21 04:51:35 +05:30
|
|
|
# convert data to shape and arrange according to given options
|
2015-05-20 02:45:15 +05:30
|
|
|
if options.dimension != []: table.data = table.data.reshape(options.dimension[1],options.dimension[0],3)
|
|
|
|
if options.flipLR: table.data = np.fliplr(table.data)
|
|
|
|
if options.flipUD: table.data = np.flipud(table.data)
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
table.data = table.data.repeat(options.pixelsizex,axis=1).\
|
2015-05-20 02:45:15 +05:30
|
|
|
repeat(options.pixelsizey,axis=0)
|
|
|
|
|
|
|
|
table.data *= 1. if np.any(table.data > 1.0) else 255.0 # ensure 8 bit data range
|
2015-05-21 04:51:35 +05:30
|
|
|
|
2015-05-20 02:45:15 +05:30
|
|
|
(height,width,bands) = table.data.shape
|
2015-09-24 14:54:42 +05:30
|
|
|
damask.util.croak('image dimension: {0} x {1}'.format(width,height))
|
2015-05-20 02:45:15 +05:30
|
|
|
|
|
|
|
im = Image.fromarray(table.data.astype('uint8'), 'RGB').\
|
|
|
|
crop(( options.crop[0],
|
|
|
|
options.crop[2],
|
|
|
|
width -options.crop[1],
|
|
|
|
height-options.crop[3]))
|
|
|
|
|
|
|
|
# ------------------------------------------ output result -----------------------------------------
|
|
|
|
|
2015-08-21 01:12:05 +05:30
|
|
|
im.save(os.path.splitext(name)[0]+ \
|
|
|
|
('_'+options.label if options.label else '')+ \
|
|
|
|
'.png' if name else sys.stdout,
|
2015-08-08 00:33:26 +05:30
|
|
|
format = "PNG")
|
2015-05-20 02:45:15 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
table.close() # close ASCII table
|
|
|
|
if options.show: im.show()
|