Img process funcs.py

From GeoMod

Jump to: navigation, search
from raster_map import *
from visual import *
import sys
import os, subprocess

class rgb_image:
    def __init__(self, nrows, ncols, maxvalue, filetype, r, g, b):
        self.nrows = nrows
        self.ncols = ncols
        self.maxvalue = maxvalue
        self.filetype = filetype
        self.r = raster_map(r)
        self.g = raster_map(g)
        self.b = raster_map(b)
        #print self.filetype.lower()

        if self.filetype.lower().strip() <> "rgb":
            sys.exit()

    def write_ImageMagic_text_image(self, fname):
        outf = open(fname, "w")

        '''Write header'''
        outl = "# ImageMagick pixel enumeration: {0},{1},255,RGB \n".format(self.ncols, self.nrows)
        outf.write(outl)
        '''write file'''
        for i in range(self.nrows):
            for j in range(self.ncols):
                
                outl = "{0},{1}: ({2:3d},{3:3d},{4:3d}) \n".format(j, i, int(self.r.data[i,j]), int(self.g.data[i,j]), int(self.b.data[i,j]) )
                outf.write(outl)
            
        outf.close()


def read_ImageMagick_text_image(fname):
    infile = open(fname,"r")
    
    #read header
    line = infile.readline()
    l = line.split(':')
    (ncols, nrows, maxvalue, filetype) = l[1].split(",")
    print "nrows=",nrows
    print "ncols=", ncols
    print "maxvalue=", maxvalue
    print "filetype=", filetype.lower()
    nrows = int(nrows)
    ncols = int(ncols)
    maxvalue = int(maxvalue)

    r = zeros((nrows,ncols), int)
    g = zeros((nrows,ncols), int)
    b = zeros((nrows,ncols), int)

    for i in range(nrows):
        for j in range(ncols):
            line = infile.readline()
            (r[i,j],g[i,j],b[i,j]) = line.split("(")[1].split(")")[0].split(",")
            #print r[i,j],g[i,j],b[i,j]
            
    infile.close()
    img = rgb_image(nrows, ncols, maxvalue, filetype.strip(), r, g, b)

    return img


Personal tools