Page MenuHomec4science

pyconv5.py
No OneTemporary

File Metadata

Created
Tue, Nov 12, 21:13

pyconv5.py

#!/usr/bin/env python
'''
Combine images
Yves Revaz
mar oct 24 16:35:46 CEST 2006
'''
from numarray import *
from optparse import OptionParser
import string
import sys
import os
import Image
import ImageDraw
import ImageFont
import ImagePalette
import ImageTk
from Nbody import *
def parse_options():
usage = "usage: %prog [options] file"
parser = OptionParser(usage=usage)
# parser.add_option("-f",
# action="store",
# dest="inputfile",
# type="string",
# default = None,
# help="input file",
# metavar=" FILE NAME")
parser.add_option("-o",
action="store",
dest="outputfile",
type="string",
default = None,
help="oputput file",
metavar=" FILE NAME")
(options, args) = parser.parse_args()
if len(args) == 0:
print "you must specify a filename"
sys.exit(0)
files = args
return files,options
def logfilter(mat,cd):
mn = 0.
mx = 255.
mat = 255.*log(1.+(mat-mn)/(cd)) / log(1.+(mx-mn)/(cd))
return mat
def linfilter(mat,mn,mx):
mat = clip(mat,mn,mx)
mat = 255.*(mat-mn)/(mx-mn)
return mat
#############################
# main
#############################
# get options
files,options = parse_options()
#inputfile = options.inputfile
outputfile = options.outputfile
#############################
# open file
#############################
# red
img1 = Image.open(files[0])
r1,g1,b1 = img1.split()
r1 = fromstring(r1.tostring(),UInt8)
g1 = fromstring(g1.tostring(),UInt8)
b1 = fromstring(b1.tostring(),UInt8)
# green
img2 = Image.open(files[1])
r2,g2,b2 = img2.split()
r2 = fromstring(r2.tostring(),UInt8)
g2 = fromstring(g2.tostring(),UInt8)
b2 = fromstring(b2.tostring(),UInt8)
# blue
img3 = Image.open(files[2])
r3,g3,b3 = img3.split()
r3 = fromstring(r3.tostring(),UInt8)
g3 = fromstring(g3.tostring(),UInt8)
b3 = fromstring(b3.tostring(),UInt8)
if (img1.size!=img2.size):
print "images do not have the same size !"
sys.exit()
size = img1.size
#############################
# compose images
#############################
# blue : gas
palette = Palette('blulut')
i3 = linfilter(b3,0,128)
i3 = logfilter(i3,128)
#i3 = logfilter(b3,2)
#palette = Palette('light')
#i3 = logfilter(b3,50)
r3 = palette.getr(i3)
g3 = palette.getg(i3)
b3 = palette.getb(i3)
# red : stars
palette = Palette('redlut')
#i1 = logfilter(r1,64)
i1 = r1
r1 = palette.getr(i1)
g1 = palette.getg(i1)
b1 = palette.getb(i1)
# greed : halo
palette = Palette('greenlut')
i2 = logfilter(r2,64)
i2 = r2
r2 = palette.getr(i2)
g2 = palette.getg(i2)
b2 = palette.getb(i2)
# combine images
# add green
r = r2
g = g2
b = b2
# add blue
ar = 1.
ag = 1.
ab = 1.
r = ar*r3+(1-ar)*r
g = ag*g3+(1-ag)*g
b = ab*b3+(1-ab)*b
# add red
ar = r1/255.
ag = r1/255.
ab = r1/255.
#ar = 0.0
#ag = 0.0
#ab = 0.0
r = ar*r1+(1-ar)*r
g = ag*g1+(1-ag)*g
b = ab*b1+(1-ab)*b
r = clip(r,0,255)
g = clip(g,0,255)
b = clip(b,0,255)
r = r.astype(UInt8)
g = g.astype(UInt8)
b = b.astype(UInt8)
#
image_r = Image.fromstring("L",size,r)
image_g = Image.fromstring("L",size,g)
image_b = Image.fromstring("L",size,b)
# merge image
img = Image.merge("RGB",(image_r,image_g,image_b))
img.save(outputfile)

Event Timeline