Wednesday, September 9, 2009

Converting SVG to PDF

Inkscape output to PDF or save to PDF is no good. It changes the fonts to ugly Bitstream Vera whatever. Use Batik Rasterizer instead.


Install it anywhere you like. In my case, it's located in /opt/batik.

To convert files, use the command as shown (assuming you're in the directory where the SVG file is located):

$ java -jar /opt/batik/batik-rasterizer.jar -m application/pdf Figure1.svg


Thursday, September 3, 2009

Getting LibSVM to work with Weka on Mac

Somehow, the only way to use LibSVM with Weka is by using the bash command-line.



Step 1: Get Weka.
Assume the bleeding edge version 3.7.0. Unzip and put in /Applications folder.

Step 2: Get LibSVM.
a. Iowa State site (http://www.cs.iastate.edu/~yasser/wlsvm/wlsvm.zip):
If you use Safari to download, it will be unzipped in the Downloads directory. The files you need are ~/Downloads/WLSVM/lib/wlsvm.jar and /Downloads/WLSVM/lib/libsvm.jar
b. Taiwan site(http://www.csie.ntu.edu.tw/~cjlin/libsvm/libsvm-2.89.zip):
Using Safari to download, the file you need is ~/Downloads/libsvm-2.89/java/libsvm.jar

Step 3: Open Terminal and copy to Weka.app. Assume you have privileges to write into Weka.app.
a. Iowa State version:
$ cp ~/Downloads/WLSVM/lib/*.jar /Applications/Weka/weka-3-7-0.app/Contents/Resources/Java
b. Taiwan version:
$ cp ~/Downloads/libsvm-2.89/java/libsvm.jar /Applications/Weka/weka-3-7-0.app/Contents/Resources/Java

Step 4: Set CLASSPATH
$ export CLASSPATH=$CLASSPATH:/Applications/Weka/weka-3-7-0.app/Contents/Resources/Java/

Step 5:Run Weka from Terminal!
$ java -classpath $CLASSPATH:weka.jar:libsvm.jar weka.gui.GUIChooser &

Conclusion: Bash sucks less!

Thursday, August 13, 2009

Screen Capture on Mac

Unlike the PC, the Mac has no PrtScreen button. To capture a screen, use cmd-shift-4 to take screenshots and save to disk, and cmd-ctrl-shift-4 to take screenshots and copy to clipboard.

The captured screen will be on the desktop named Picture 1.pdf, Picture 2.pdf and so on. Some blogs say the default filetype is PNG, but not on my Mac.

To change default filetype to some other filetype, launch Terminal, and depending on what file type you want outputted, type (or copy and paste) the appropriate line below followed by return:

defaults write com.apple.screencapture type pdf
defaults write com.apple.screencapture type png
defaults write com.apple.screencapture type jpg
defaults write com.apple.screencapture type tif


Quit Terminal, log out of your account and log in for the change to take effect right away. To revert to the default png format, type 'defaults write com.apple.screencapture type png' as shown above (no single quotes), or delete the com.apple.screencapture plist file in your user preferences folder (no need to restart).

Using Canny edge detection in OpenCV

The second Python example works on my MacBook. So HighGUI does work on the Mac OS X.

Batch Image Processing with Python

Adapted from http://www.coderholic.com/batch-image-processing-with-python/

If you want to make changes to a single image, such as resizing or converting from one file format to another, then you’ll probably load up the image in an editor and manually make the required changes. This approach is great for a single image, but it doesn’t really scale past more than a few images, at which point it becomes time consuming, not to mention boring!

This is where Python and the Python Imaging Library (or PIL) come in, allowing you to write scripts that process images in batch.

Batch Processing

No matter what processing we want to do on our images the script outline will be the same: loop over all provided arguments, and perform the processing. The code to do this is shown below:


#!/usr/bin/env python
import sys
import Image

# Loop through all provided arguments
for i in range(1, len(sys.argv)):
try:
# Attempt to open an image file
filepath = sys.argv[i]
image = Image.open(filepath)
except IOError, e:
# Report error, and then skip to the next argument
print "Problem opening", filepath, ":", e
continue
# Perform operations on the image here

This will allow our script to be called in all of the following ways:

$ batch_process.py image.gif
$ batch_process.py image1.png image2.gif image3.jpg
$ batch_process.py *.png
$ batch_process.py image1.gif, *.png


With the help of PIL the image processing is really simple. Below is a collection of just some of the operations we could perform. For more details see the PIL documentation.

#Resize an image
image = image.resize((width, height), Image.ANTIALIAS)

# Convert to greyscale
image = image.convert('L')

# Blur
image = image.filter(ImageFilter.BLUR)

# Sharpen
image = image.filter(ImageFilter.SHARPEN)

Saving

Once we’ve processed the image we need to save the changes. In some instances we might just want to save over the original filename. If that’s the case we can just call the save method of image with its original filename. If we want to save the file under a different name, or as a different filetype we need to do a little more work:

# Split our original filename into name and extension
(name, extension) = os.path.splitext(filepath)

# Save with "_changed" added to the filename
image.save(name + '_changed' + extension)

# Save the image as a JPG
image.save(name + '.jpg')

Notice in the last example how PIL takes care of the conversion to JPG for us. All we do is provide the file extension and PIL does the rest for us.

A Complete Example

Each of the steps above can easily be put together to create a batch image processing script. Below is a complete script which creates thumbnails of all provided images, and saves them as PNG images, not matter what their original format:

!/usr/bin/env python
# Batch thumbnail generation script using PIL

import sys
import os.path
import Image

thumbnail_size = (28, 28)

# Loop through all provided arguments
for i in range(1, len(sys.argv)):
try:
# Attempt to open an image file
filepath = sys.argv[i]
image = Image.open(filepath)
except IOError, e:
# Report error, and then skip to the next argument
print "Problem opening", filepath, ":", e
continue

# Resize the image
image = image.resize(thumbnail_size, Image.ANTIALIAS)

# Split our original filename into name and extension
(name, extension) = os.path.splitext(filepath)

# Save the thumbnail as "(original_name)_thumb.png"
image.save(name + '_thumb.png')