Sunday, December 12, 2010

checking for Images

I needed to parse a directory and determine which files are images that can be displayed on a panel in my java application. I thought of checking the extensions of files and returning a boolean value of 'true' if they matched those of image files. Examining the folder, I found that somebody had renamed the files to include incorrect file extensions. 'Fake.jpg' was actually a text file. Some image files didn't have extensions at all. 'RealImage' was a .png file. So, checking for extensions wouldn't work.

Then I thought of taking a file and finding out whether the file data bytes started with magic numbers which identify a particular image format .For example ,a PNG file header starts with an 8 bytes signature of 0x89504e470d0a1a0a whereas a GIF89a file has a signature 0x474946383961.

Examining the bytes is the right way to find out the file format. But then ,I didn't want to put too many if then blocks to check every possible image file format that may happen to be in the directory. So I thought of using java's ImageIO class to check for valid images.

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

public class Utils {
public static boolean isImageFile(String filename) throws IOException{
boolean isImage=false;
FileInputStream is = new FileInputStream(filename);;
ImageInputStream imginstream = ImageIO.createImageInputStream(is);
// get image readers who can read the image format
Iterator iter = ImageIO.getImageReaders(imginstream);
if (iter.hasNext()) {
isImage=true;
}
return isImage;
}
}



running directories and zip files in python

After coding an application in python,I wanted to distribute the source in a way any user can run the application without bothering to install it.Luckily ,in Python 2.6 ,a zip archive or directory can be executed by passing their names to python interpreter.
The section http://docs.python.org/release/2.6.5/whatsnew/2.6.html#other-language-changes mentions.
"Directories and zip archives containing a __main__.py file can now be executed directly by passing their name to the interpreter.The directory or zip archive is automatically inserted as the first entry in sys.path."

Suppose my application contains a package called 'mypackage' which has all the application code.I can create a front-end to the application and put it in a file called __main__.py

import mypackage
mypackage.myapplication_method()

1.executing the directory
I put this __main__.py file and mypackage into a folder mycode.Now the folder structure is say,

C:\python\mycode
-- mypackage
-- __main__.py

Now ,at the command prompt ,I can cd to C:\python and call python mycode to execute the application.

2.executing the zip archive
Put the __main__.py and mypackage in to a zip file ,say myzip.zip.Copy this zipfile to C:\python directory.At the command prompt ,cd to C:\python and run python myzip.zip to execute the application.

There is no need to set PYTHONPATH.
These two facilities make life easier for those who doesn't want to go to the trouble of creating/running setup scripts just to distribute/execute a demo application which may contain multiple packages.