Wednesday, June 11, 2008

Infrared? Channelshifting? WTF? Ok, I'll write a script....

I think I need a new challange. Infrared Photography!
Don't know what it is? ask wikipedia...

BUT, I read some tutorials and I found out that Infrared photos apear channel shifted. Red channels id blue channel an blue channel is red channel. That means that the colors look absolutly wired.

So I decided to wirte a script -python of course - that shifts the channels back. R->B B->R

It was easier than I thought.

I used some normal pictures to test it.

Example 1:
ORIGINAL IMAGE
the red bridge

After my script swapped Red and Blue it looks like this:
PYTHON SCRIPT TEST: Channel Shifting

Ok I know what you think... "WTF? Now it just looks wired! and simply wrong!"
But, I swap a normal Image in this case. When I take my first infrared photo it will look - maybe - like the wired channel-shifted photo, BUT after my script swapped red and blue it will -maybe - look like the original image!

got it?

After this fast success, I decided to mess arround a bit more with channels and python .
I created a second script which keeps one channel (red,green or blue) and overwrites the othe two with the avarage of all 3 channels.

This looks like this then (here i kept the blue channel):
PYTHON SCRIPT TEST: Channel Fun (Blue)

More results of this script can be found in my flickr photostream.

Here are the two scripts:

channelShift.py

import Image
import ImageEnhance
import math

print "ChannelShift"
inim = raw_input("input: ")
out = raw_input("Save as: ")

mod = int(raw_input("(R->B)=0 or (R->G->B->R)=1): "))

im1 = Image.open(inim)
imOut = out

w = im1.size[0]
h = im1.size[1]

print w
print h

x = 0
while x < w:
y = 0
while y < h:
p1 = im1.getpixel((x,y))

if mod == 0:
r = p1[2]
g = p1[1]
b = p1[0]
else:
r = p1[2]
g = p1[0]
b = p1[1]



im1.paste((r,g,b),(x,y,x+1,y+1))

y = y + 1
x = x + 1
if x % 10 == 0:
print x

im1.save(imOut)

print "end"



and
channelFun.py


import Image
import ImageEnhance
import math

print "ChannelFun"
inim = raw_input("input: ")
out = raw_input("Save as: ")
rtc = raw_input("Keep Channel (r,g,b): ")

im1 = Image.open(inim)
imOut = out

w = im1.size[0]
h = im1.size[1]

imred = Image.new("RGB", (w,h), (0,0,0))

print w
print h

x = 0
while x < w:
y = 0
while y < h:
p1 = im1.getpixel((x,y))

r = p1[0]
g = p1[1]
b = p1[2]


#reduce
reduced = int((r+g+b) / 3)

if (rtc == "r"):
imred.paste((r,reduced,reduced),(x,y,x+1,y+1))
elif (rtc == "g"):
imred.paste((reduced,g,reduced),(x,y,x+1,y+1))
elif (rtc == "b"):
imred.paste((reduced,reduced,b),(x,y,x+1,y+1))
else:
print "ERROR: " + rtc + " is not valid! USe r,g or b!"
x = w
break



y = y + 1
x = x + 1
if x % 10 == 0:
print "ROW: " + str(x) + " of " + str(w) + " rows processed ..."

imred.save(imOut)

print "end"

1 comment:

chonlatee said...

so i will write script for nude detect . do you have i dea ?. i can programming in python and PIL but i no idea for this.

sorry my englist