2007/08/16

pyScreenSpy, a Python implementation using Java libraries & JPython

I love Python, I dislike Java. But Java is maintained by a large corporation and has some tools that Python does not. It is possible to write a program in Python, using Python's core library and Java's library in the same program and compile it in a Java bytecode executable. All you need is to download and setup JPython. Here are the implications of developing a program using JPython:

Advantages:

  • Use both Java's and Python's libraries seamlessly in the same program.
  • Will work on all platforms that Java works.
Disadvantages:
  • Java's virtual machine is long to load (~3sec compared to Python's ~0.1 sec)
  • Some functionalities of Java and of Python are not available in JYthon, and the documentation about that is not very good.
As an example, I re-coded parts of the JScreenSpy application that I published a few days ago using JPython in 21 lines. This is just an example, I will re-release a fully working and ameliorated version (1.1) of JScreenSpy in Python in the following days.

Here is the source file, you can execute it using jpython pyScreenSpy.py
#Pythons imports
import os

#Java Imports
from javax import imageio
from java import awt
from java.awt import image
from java import io

#Determine current screen size
toolkit = awt.Toolkit.getDefaultToolkit();
screenSize = toolkit.getScreenSize();
screenRect = awt.Rectangle(screenSize);

#Create screenshot
robot = awt.Robot();

#Take ScreenShot
image = robot.createScreenCapture(screenRect);
imageio.ImageIO.write(image, "jpg", io.File("test.jpg") );
21 lines, in Python, using Java's libraries. How cool is that? Very!

1 comment:

Arun said...

Very impressive. Gives me another reason to learn Python