Blog Archive

Labels

Search This Blog

Tuesday, August 7, 2012

Python Image enhancing snippet

Imaging and basic image processing with Python....

This is not going to be the typical and formal kind of blog you may know... err... em just saying, just incase;-)
So.... Ummm Python image enhancement code.

IMAGES and IMAGE ENHANCEMENT

Images and Python Imaging Libraries (PIL)

THE Geeky DEFINITION OF AN IMAGE

Images are basically binary files that contains color definitions and mappings, pixel data, compression/lookup-tables, and visual data enities of an image such as dpi(resolution), width and height, structured in a well organised format for easy read and write by computer applications. They may also contain other properties such as EXIF data and other general or unique properties as defined by the image format.

First things first, download PIL here... Download the most recent version you see there.

Well, I'll skip some stories and get you what you want...
Next, we may want to try this code

There are 3 levels of automatic image adjustment I use.... The one implemented here is only 2, the 3rd is somewhat complex and its about 100lines.....

Here is a fully usable code

-------------------------------------------------------------------------------------------

import operator
import Image
import ImageOps

def equalize(im):
ims = im.convert("L")
h = ims.histogram()
lut = []
for b in range(0, len(h), 256):
# step size
step = reduce(operator.add, h[b:b+256]) / 255
# create equalization lookup table
n = 0
for i in range(256):
lut.append(n / step)
n = n + h[i+b]
# map image through lookup table
#return im.point(lut*im.layers)
return im.point(lut)

def autoOptimize(im):
try:
equalize(im)
except ValueError:
print "Error in Level-1 optimization method!!!!!"
im = ImageOps.autocontrast(im, cutoff = 0)
return im

-------------------------------------------------------------------------------------------
With this piece of code above, you may be able to automatically enhance images....
Just copy and paste the code, save the file as any name you may wish <Optimizer>(mine)... Import it to your code and juast call the autoOptimize function... example

test.py
--------------
import Optimizer
image = open('flowers.jpg')
Optimizer.autoOptimize(image)
image.show()
---------------------

PLEASE NOTE THAT The equalization algorithim was from EFFBOT.

I would add a step-3 optimization algorithm soon.

Thanks!. TiM
err... I am currently writing exams... i will update by 19th October, 2012 21:00hrs GMT :::Sorry for the inconveniences. 

UPDATE: Many things happened between the date I posted this and this update... more than 300 days later..
  • like, participating in ACM-ICPC 2012 South African Regional Contest
  • my Dad passing away... :-(
  • taking on a new programming language... C#
  • ...and several hundreds more that need not to be mentioned
So, I will just give you external links to help out...
UNDERSTAND SOME BASICS
for beginners

some advanced stuff... buy the book

That's that for this...

No comments:

Post a Comment