Showing posts with label python image processing. Show all posts
Showing posts with label python image processing. Show all posts
Thursday, May 7, 2009
Generate a thumbnail of a web page
I found this usefull page which describes how to capture a screenshot of a web page from a given URL. It basically uses Firefox's rendering engine in order to generate a view of the page and then saves it to a JPG.
Sunday, May 3, 2009
Sharpening images

This piece of code shows how to sharpen a grayscale image (for color images do the same for each channel).
Sharpening is performed by applying a Laplacian operator on the image and adding the output to the original image.
Finally, this is the operator used (Laplacian+image):
-1 -1 -1
-1 9 -1
-1 -1 -1
-1 -1 -1
(see this nice application in which you can perform convolution with the above operator on a given image)
The code (assumes an image named imname is loadable, final output is stored in image named gray):
gray=cvLoadImage(imname,0)
imwidth = gray.rows
imheight = gray.cols
# convert to 32 bit
gray2=cvCreateImage (cvSize (imheight, imwidth), 32, 1)
for r in range(gray.rows):
for c in range(gray.cols):
gray2[r][c]=gray[r][c]
# define the filter:
lapl=cvCreateImage (cvSize (imheight, imwidth), 32, 1)
m=cvCreateImage (cvSize (3,3), 32, 1)
m[0][0]=-1
m[0][1]=-1
m[0][2]=-1
m[2][0]=-1
m[2][1]=-1
m[2][2]=-1
m[1][0]=-1
m[1][2]=-1
m[1][1]=8
cvFilter2D(gray2,lapl,m)
maxv=0
for r in range(gray.rows):
for c in range(gray.cols):
if(lapl[r][c]>maxv):
maxv=lapl[r][c]
for r in range(gray.rows):
for c in range(gray.cols):
v=int(255*lapl[r][c]/maxv)
gray[r][c]=gray[r][c]+v
maxv=0
for r in range(gray.rows):
for c in range(gray.cols):
if(gray[r][c]>maxv):
maxv=gray[r][c]
for r in range(gray.rows):
for c in range(gray.cols):
v=int(255*gray[r][c]/maxv)
gray[r][c]=v
Tuesday, April 14, 2009
This example is an application for displaying an image with the ability to stretch the window size arbitrarily (thus enabling to zoom in on the image):
from Tkinter import *
import Image, ImageTk
import sys
#
# an image viewer
class UI(Canvas):
if not sys.argv[1:]:
else:
root = Tk()
root.title(filename)
im = Image.open(filename)
UI(root, im)
root.mainloop()
from Tkinter import *
import Image, ImageTk
import sys
#
# an image viewer
class UI(Canvas):
def __init__(self, master, im):
x, y, orig_w, orig_h = im.getbbox()
Canvas.__init__(self, master,width=orig_w, height=orig_h)
tmpim = ImageTk.PhotoImage(im)
self.origim = im
self.image = tmpim
self.create_image(orig_w/2, orig_h/2, image=tmpim)
self.bind('
top=self.winfo_toplevel()
top.rowconfigure(0, weight=1)
top.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)
self.columnconfigure(0, weight=1)
self.grid(sticky=N+S+E+W)
def resizeImage(self,event):
im = self.origim.resize((event.width, event.height),Image.ANTIALIAS)
tmpim = ImageTk.PhotoImage(im)
self.image = tmpim
self.create_image(event.width/2, event.height/2, image=tmpim)
if not sys.argv[1:]:
print 'need an image name!'
exit()
else:
filename = sys.argv[1]
root = Tk()
root.title(filename)
im = Image.open(filename)
UI(root, im)
root.mainloop()
Sunday, April 12, 2009
converting 8bit image to 32 bit image
If you have an 8bit source image and you want to convert it to a 32 bit image you do it using cvConvertScale:
newim = cvCreateImage (cvSize (src.rows, src.cols, 32, 1)
cvConvertScale(src,newim)
newim = cvCreateImage (cvSize (src.rows, src.cols, 32, 1)
cvConvertScale(src,newim)
How to crop images with opencv in python
Found this helpful tip from a fellow python-opencv user:
find it here
find it here
Labels:
crop image,
opencv,
python,
python example,
python image processing
Saturday, April 11, 2009
Opencv example
Here's a short example showing how to use openCV with Python.It reads an image from a file, displays the image, the Harris corner detector on that image and the Canny edge image:
(save this in a file named tmp.py and run with: python tmp.py)
import Image
import os
import sys
from opencv.cv import *
from opencv.highgui import *
def analyzeImage(f,name):
f = open(sys.argv[1],'r')
analyzeImage(f,sys.argv[1])
(save this in a file named tmp.py and run with: python tmp.py
import Image
import os
import sys
from opencv.cv import *
from opencv.highgui import *
def analyzeImage(f,name):
im=Image.open(f)
try:
- if(im.size[0]==1 or im.size[1]==1):
- return
print (name+' : '+str(im.size[0])+','+ str(im.size[1]))
le=1
if(type(im.getpixel((0,0)))==type((1,2))):
- le=len(im.getpixel((0,0)))
gray = cvCreateImage (cvSize (im.size[0], im.size[1]), 8, 1)
edge1 = cvCreateImage (cvSize (im.size[0], im.size[1]), 32, 1)
edge2 = cvCreateImage (cvSize (im.size[0], im.size[1]), 8, 1)
edge3 = cvCreateImage (cvSize (im.size[0], im.size[1]), 32, 3)
for h in range(im.size[1]):
- for w in range(im.size[0]):
- p=im.getpixel((w,h))
if(type(p)==type(1)):
- gray[h][w] = im.getpixel((w,h))
else:
- gray[h][w] = im.getpixel((w,h))[0]
cvCornerHarris(gray,edge1,5,5,0.1)
cvCanny(gray,edge2,20,100)
cvNamedWindow("win")
cvShowImage("win", gray);
cvNamedWindow("win2")
cvShowImage("win2", edge1);
cvNamedWindow("win3")
cvShowImage("win3", edge2);
cvWaitKey()
f.close()
except Exception,e:
- print e
print 'ERROR: problem handling '+ name
f = open(sys.argv[1],'r')
analyzeImage(f,sys.argv[1])
Image processing tools for python
The most basic library is PIL. it provides pretty basic tools for reading,writing and displaying images, some tools for drawing. In terms of image processing it has some functions for cropping, resizing, sharpening images. For more complex operations I found it insufficient.
The library I found to be useful so far is the Python binding for OpenCV. It can be found here. Installing it should not be too difficult, though I could not get it working until I installed the following:
sudo apt-get install python-opencv
The library I found to be useful so far is the Python binding for OpenCV. It can be found here. Installing it should not be too difficult, though I could not get it working until I installed the following:
sudo apt-get install python-opencv
I will describe here my short experience in trying to develop image processing applications using python.
I am developing on Ubuntu, which I highly recommend. For anyone familiar with Linux but is afraid of all the trouble in installing it, Ubuntu is an extremely user friendly environment with an almost Plug-and-Play installation process (you can download it here Ubuntu Download).
For developers linux/ubuntu offers many opensource tools which are easily accessible and ready to use.
The application I am working on involves crawling the web, searching for images, creating a DB of these images and then processing theses images in order to create a useful indexing (you might say it is sort of an image retrieval search engine). It is still in it's very early stages and much of the work is experimental. I will describe here some of the hardships I had to overcome in the process, hopefully it might save some other people's time.
So.... Let's dive right into the WEE BUFETS (Warnings,Errors, Exceptions, Bugs, Unanticipated results, Features, and other Excrements of Toxic Software)
Convert string to file handle:
Here's one sucker I had to deal with yesterday. It goes a little something like this:
Let's say you have a string which is a binary buffer holding the contents of a file. You want to
convert it to a file handle but you don't want to go through the trouble of of writing it to a file and then opening it and getting the file handle. You can do this using the following python code:
import StringIO
f = StringIO.StringIO("string containing file data")
That's it! now you can treat f as a regular file handle much the same way you would after a call to the 'open' function - I encountered this when I saved an image file in a mysql DB as a blob object, then reading from the database.
I am developing on Ubuntu, which I highly recommend. For anyone familiar with Linux but is afraid of all the trouble in installing it, Ubuntu is an extremely user friendly environment with an almost Plug-and-Play installation process (you can download it here Ubuntu Download).
For developers linux/ubuntu offers many opensource tools which are easily accessible and ready to use.
The application I am working on involves crawling the web, searching for images, creating a DB of these images and then processing theses images in order to create a useful indexing (you might say it is sort of an image retrieval search engine). It is still in it's very early stages and much of the work is experimental. I will describe here some of the hardships I had to overcome in the process, hopefully it might save some other people's time.
So.... Let's dive right into the WEE BUFETS (Warnings,Errors, Exceptions, Bugs, Unanticipated results, Features, and other Excrements of Toxic Software)
Convert string to file handle:
Here's one sucker I had to deal with yesterday. It goes a little something like this:
Let's say you have a string which is a binary buffer holding the contents of a file. You want to
convert it to a file handle but you don't want to go through the trouble of of writing it to a file and then opening it and getting the file handle. You can do this using the following python code:
import StringIO
f = StringIO.StringIO("string containing file data"
Subscribe to:
Posts (Atom)