Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Wednesday, October 7, 2009

RGB to Gray-scale Image conversion in Python

# Open Source Programming

# Author : S.Ganesh Babu

# Program: RGB to Gray transform

import Image

im=Image.open('c:/House24.png').convert("L")

im.show(im)

# Open Source Programming

# Author : S.Ganesh Babu

# Program: RGB to Gray transform

import Image

im=Image.open('c:/House24.png').convert("L")

im.show(im)

Processing RGB Image in Python

import Image

im=Image.open('c:/House24.png')

source=im.split()

R, G, B = 0, 1, 2

# Select regions where red is less than 100

mask = source[R].point(lambda i: i < 100 and 255)

# process the green band

out=source[G].point(lambda i: i * 0.7)

# paste the processed band back, but only where red was < 100

source[G].paste(out,None,mask)

# build a new multiband image

im = Image.merge(im.mode,source)

im.show(im)

import Image

im=Image.open('c:/House24.png')

source=im.split()

R, G, B = 0, 1, 2

# Select regions where red is less than 100

mask = source[R].point(lambda i: i < 100 and 255)

# process the green band

out=source[G].point(lambda i: i * 0.7)

# paste the processed band back, but only where red was < 100

source[G].paste(out,None,mask)

# build a new multiband image

im = Image.merge(im.mode,source)

im.show(im)

RGB Channel splitting using Python

# Open Source Programming

# Author : S.Ganesh Babu

# Program: Splitting and merging of RGB Image

import Image

im=Image.open('c:/House24.png')

Imv=im.load()

#x,y=im.size

#RE=[]

#for i in range(x):

#    for j in range(y):

#        RE.append(Imv[i,j].__getitem__(0))

#print RE

#RE.show(RE)

r,g,b=im.split()

r.show(r)

g.show(g)

b.show(b)

im1=Image.merge("RGB",(r,g,b))

im1.show(im1)

# Open Source Programming

# Author : S.Ganesh Babu

# Program: Splitting and merging of RGB channels

import Image

im=Image.open('c:/House24.png')

Imv=im.load()

#x,y=im.size

#RE=[]

#for i in range(x):

#    for j in range(y):

#        RE.append(Imv[i,j].__getitem__(0))

#print RE

#RE.show(RE)

r,g,b=im.split()

r.show(r)

g.show(g)

b.show(b)

im1=Image.merge("RGB",(r,g,b))

im1.show(im1)

Reading the pixel values in gray scale image using Python

# Open Source Programming

# Author : S.Ganesh Babu

# Program: Reading the pixel values in gray scale image.

import Image

im=Image.open('c:/new.tif')

Imv=im.load()

x,y=im.size

for i in range(x):

for j in range(y):

print Imv[i,j]

im.show(im)

# Open Source Programming

# Author : S.Ganesh Babu

# Program: Reading the pixel values in gray scale image.

import Image

im=Image.open('c:/new.tif')

Imv=im.load()

x,y=im.size

for i in range(x):

for j in range(y):

print Imv[i,j]

im.show(im)

Geometric Transformation in images using Python

# Open Source Programming

# Author : S.Ganesh Babu

# Program: Splitting and merging of RGB Image

import Image

im=Image.open('c:/House24.png')

# RESIZE THE IMAGE

out=im.resize((128,128))

out.show(out)

# ROTATE ANY DEGREE

out=im.rotate(45) # counter - clockwise

out.show(out)

# FLIP IMAGE

out=im.transpose(Image.FLIP_LEFT_RIGHT)

out.show(out)

out=im.transpose(Image.FLIP_TOP_BOTTOM)

out.show(out)

# ROTATE 90,180,270 DEGREES

out=im.transpose(Image.ROTATE_270)

out.show(out)

# Open Source Programming

# Author : S.Ganesh Babu

# Program: Geometric Transformations

import Image

im=Image.open('c:/House24.png')

# RESIZE THE IMAGE

out=im.resize((128,128))

out.show(out)

# ROTATE ANY DEGREE

out=im.rotate(45) # counter - clockwise

out.show(out)

# FLIP IMAGE

out=im.transpose(Image.FLIP_LEFT_RIGHT)

out.show(out)

out=im.transpose(Image.FLIP_TOP_BOTTOM)

out.show(out)

# ROTATE 90,180,270 DEGREES

out=im.transpose(Image.ROTATE_270)

out.show(out)

Cropping Image in Python

import Image

im=Image.open('c:/House24.png')

box=(50,50,150,150)

region=im.crop(box)

region.show(region)

import Image

im=Image.open('c:/House24.png')

box=(50,50,150,150)

region=im.crop(box)

region.show(region)

Manipulating Image Pixels in Python

# Open Source Programming

# Author : S.Ganesh Babu

# Program: Manipulating Pixels

import Image

im=Image.open('c:/new.tif')

# multiply each pixels by 1.5

out=im.point(lambda i: i*1.5)

out.show(out)

# multiply each pixels by 0.5

out=im.point(lambda i: i*0.5)

out.show(out)

# Open Source Programming

# Author : S.Ganesh Babu

# Program: Manipulating Pixels

import Image

im=Image.open('c:/new.tif')

# multiply each pixels by 1.5

out=im.point(lambda i: i*1.5)

out.show(out)

# multiply each pixels by 0.5

out=im.point(lambda i: i*0.5)

out.show(out)

Image Transpose in Python

import Image

im=Image.open('c:/House24.png')

delta=50

x, y=im.size

delta=delta % x

if delta == 0 : im.show(im)

part1=im.crop((0,0, delta, y))

part2=im.crop((delta, 0, x, y))

im.paste(part2,(0,0, x-delta, y))

im.paste(part1,(x-delta, 0, x, y))

im.show(im)

import Image

im=Image.open('c:/House24.png')

delta=50

x, y=im.size

delta=delta % x

if delta == 0 : im.show(im)

part1=im.crop((0,0, delta, y))

part2=im.crop((delta, 0, x, y))

im.paste(part2,(0,0, x-delta, y))

im.paste(part1,(x-delta, 0, x, y))

im.show(im)

Sunday, September 27, 2009

Reading Image in Python

import array,Image,numpy
from datetime import datetime

s_time = datetime.now().microsecond
print "Starting Time " , s_time
im = Image.open('/home/anthoniraj/cameraman.tif')
pixels = im.load()

x,y =im.size
print x,y
#print pixels[0,0].__getitem__(3)
for i in range(x):
for j in range(y):
print pixels[i,j]

im.show(im)
e_time = datetime.now().microsecond
print "Ending Time %" ,e_time
print "Total Execution time " ,(e_time-s_time)