#! /usr/bin/python3 # Last edited on 2025-12-12 07:12:27 by stolfi import sys, re from math import sin, cos, log, exp, floor, ceil, inf, nan, pi from error_funcs import arg_error, prog_error from process_funcs import run_command, bash import argparser def main(): page, clip, oname, inR, inG, inB = parse_options() maxvals_tb = read_clip_maxvals_table(page, clip) # Grab the monochrome images for this composite: components = [] for band in inR, inG, inB: maxval = maxvals_tb[band]['maxval'] cfile_in = f"MS/davis/{page}/{band}/clips/{clip}.png" cdir_ot = f"pages/{page}/clips/{clip}/mono" bash(f"mkdir -p {cdir_ot}") cfile_ot = f"{cdir_ot}/{band}.png" hmaxval = ("%04x" % maxval) hblack = "#000000000000" hwhite= f"#{hmaxval}{hmaxval}{hmaxval}" sys.stderr.write(f"{cfile_in} -> {cfile_ot}\n") bash \ ( f"convert {cfile_in}" + \ f" -set colorspace LinearGray" + \ f" -level-colors '{hblack},{hwhite}'" + \ f" -depth 16" + \ f" -colorspace Gray" + \ f" {cfile_ot}" ) bash(f"display -title '%f' {cfile_ot}") components.append(cfile_ot) # Create the composite: odir = f"pages/{page}/clips/{clip}/comps/hand" bash(f"mkdir -p {odir}") ofile = f"{odir}/{oname}.png" bash \ ( f"convert" + \ f" {components[0]}" + \ f" {components[1]}" + \ f" {components[2]}" + \ f" -combine" + \ f" {ofile}" ) bash(f"gimp -s -n {ofile}") return 0 # ---------------------------------------------------------------------- def read_clip_maxvals_table(page, clip): # Reads the file "pages/{page}/clips/{clip}/maxvals.txt" that describe the multispectral images # available for a certain clip of some page from the Lazarus 2010 multispectral set. # # Apart from blank and '#'-comment lines, each line of that file must # have the format # # "{band} {wlen} {illum} {maxval}" # # where, for all pages: # # {band} the TIFF image name as published. # # {wlen} the nominal wavelength (from the band's name) # {illum} light direction: # 0 = frontal, both lamps. # 1 = frontal, right lamp (from N on verso pages, from S on recto). # 2 = frontal, left lamp (from S on verso pages, from N on recto). # 3 = from behind # # {maxval} Max pixel value to use when converting from int to float. # # {vlo} 0.0002 fractile on "core" clip. # {vhi} 0.9998 fractile on "core" clip. # # Returns a dict of dicts {bands_tb} where {bands_tb[band][parm]} is # the value of field {parm} for band {band} where {parm} is 'wlen' # 'illum', 'maxval'. # maxvals_tb = { } fname = f"pages/{page}/clips/{clip}/maxvals.txt" with open(fname) as rd: for line in rd: line = re.sub(r"[#].*$", "", line).strip() if line != "": line = re.sub(r"[ ]+", " ", line) flds = line.split(' ') assert len(flds) == 4, "bad maxvals table file" band, wlen, illum, maxval = flds props = dict \ ( wlen = int(wlen), illum = int(illum), maxval = int(maxval), ) maxvals_tb[band] = props return maxvals_tb # ---------------------------------------------------------------------- def parse_options(): iarg = 1 # Next command line arg to parse page = sys.argv[iarg]; iarg += 1 clip = sys.argv[iarg]; iarg += 1 oname = sys.argv[iarg]; iarg += 1 inR = sys.argv[iarg]; iarg += 1 inG = sys.argv[iarg]; iarg += 1 inB = sys.argv[iarg]; iarg += 1 return page, clip, oname, inR, inG, inB # ---------------------------------------------------------------------- main()