def __str__(self, key_blacklist = None): '''The primary line, ie not including any comments''' if key_blacklist is None: key_blacklist = [] self.update() dbg() dbg('original: %s' % self.text) dbg('variables: %s' % self.variables) ret = self.prefix() printed = set() for k in self.variable_print_order(): if k in key_blacklist: continue if k in self.variables: v = self.variables[k] dbg('k: %s, v: %s' % (repr(k), repr(v))) printed.add(k) ret += ' %s' % self.print_variable(k) for k in self.variables: if k in key_blacklist: continue if k in printed: continue ret += ' %s' % self.print_variable(k) dbg('final: %s' % ret) return ret
def make_tile(self, pim, x, y, row, col): '''Make a tile given an image, the upper left x and y coordinates in that image, and the global row/col indices''' if self.dry: if self.verbose: print 'Dry: not making tile w/ x%d y%d r%d c%d' % (x, y, row, col) else: xmin = x ymin = y xmax = min(xmin + self.tw, pim.width()) ymax = min(ymin + self.th, pim.height()) nfn = self.get_name(row, col) if self.verbose: print 'Subtile %s: (x %d:%d, y %d:%d)' % (nfn, xmin, xmax, ymin, ymax) ip = pim.subimage(xmin, xmax, ymin, ymax) ''' Images must be padded If they aren't they will be stretched in google maps ''' if ip.width() != self.tw or ip.height() != self.th: dbg('WARNING: %s: expanding partial tile (%d X %d) to full tile size' % (nfn, ip.width(), ip.height())) ip.set_canvas_size(self.tw, self.th) # http://www.pythonware.com/library/pil/handbook/format-jpeg.htm # JPEG is a good quality vs disk space compromise but beware: # The image quality, on a scale from 1 (worst) to 95 (best). # The default is 75. # Values above 95 should be avoided; # 100 completely disables the JPEG quantization stage. ip.image.save(nfn, quality=95) self.mark_done(row, col)
def from_tagged_file_names(image_file_names): engine = GridStitch() engine.image_file_names = image_file_names dbg('Orig file names: %s' % str(image_file_names)) file_names_canonical = list() for file_name in image_file_names: new_fn = os.path.realpath(file_name) engine.canon2orig[new_fn] = file_name file_names_canonical.append(new_fn) engine.coordinate_map = ImageCoordinateMap.from_tagged_file_names(file_names_canonical) return engine
def from_tagged_file_names(image_file_names): engine = GridStitch() engine.image_file_names = image_file_names dbg('Orig file names: %s' % str(image_file_names)) file_names_canonical = list() for file_name in image_file_names: new_fn = os.path.realpath(file_name) engine.canon2orig[new_fn] = file_name file_names_canonical.append(new_fn) engine.coordinate_map = ImageCoordinateMap.from_tagged_file_names( file_names_canonical) return engine
def reparse(self): self.variables = dict() first = True #for token in self.text.split(' '): for (k, v) in self.get_tokens(): #print 'token: "%s"' % token #k = token[0] #v = token[1:] dbg('k: %s, v: %s' % (repr(k), repr(v))) # We can still have empty string if not v is None and len(v) == 0: v = None if first: prefix = k if not v is None and len(v) > 0: print 'Line: %s' % self.text print 'ERROR: line type should not have value: %s' % repr(v) raise Exception('confused') first = False continue # Convert if possible try: if k in self.key_variables(): pass elif k in self.int_variables(): v = int(v) elif k in self.float_variables(): v = float(v) elif k in self.string_variables(): # Already in string form pass else: print 'WARNING: unknown data type on %s (full: %s)' % (k, self.text) raise Exception('Unknown key') except: print 'line: %s' % self.text print 'key: %s, value: %s' % (repr(k), repr(v)) self.print_variables() raise # Ready to roll self.set_variable(k, v)
def get_tokens(self): ''' Returns a list of (k, v) pairs If it has no v, v will be None Tokens can have quotes around them Ex: n"TIFF c:NONE r:CROP" Internally, we do not store these Instead, they will be re-added when writing ''' tokens = list() i = 0 # Some version have a0, some have a=0 although a0 seems much more common while i < len(self.text): k = '' v = None # Find the key: keep going until we hit either ", number, or space while i < len(self.text): c = self.text[i] # End of this k/v? if c == ' ': i += 1 break # A quoted value? elif c == '"': i += 1 v = '' # Note we skip the " while True: if i >= len(self.text): raise Exception('Missing closing " on %s' % self.text) c = self.text[i] if c == '"': i += 1 break v += c i += 1 # Think we should have at most one quoted thingy break # A numeric value? elif c in '+-0123456789': v = '' # Note we include the original char while i < len(self.text): c = self.text[i] if c == ' ': i += 1 break v += c i += 1 break else: # This may not be bulletproof but I think its good enough # These lines show up when you add images in Hugin # ex bad: a=a but I'm not sure thats valid anyway if c != '=': k += c i += 1 # Discard extra spaces and some other corner cases if len(k) > 0 : tokens.append((k, v)) dbg(tokens) return tokens