Binary Coder

February 14, 2012

Using OpenCV for Python in Windows

OpenCV stands for Open Computer Vision Library and can be used to process and transform images.

  • You can get the latest windows files here. I downloaded and extracted OpenCV-2.3.1-win-superpack.exe
  • After extracting the exe, copy the contents of C:\OpenCV-2.3.1-win-superpack\opencv\build\python\2.7 to C:\dev\Python27_32bit\Lib\site-packages\
  • After that, install numpy and scipy in your Python installation using easy_install

Now you are ready to test it. You can run this sample program that comes with OpenCV. Since the code points to a URL for building.jpg that is not there, you can point it to any other image URL on the web.

January 21, 2012

Python Imaging Library (PIL) : Installing on widows

I used easy_install to install PIL in Python 2.7.2 on my windows box. However I got this error with the 64-bit version of Python :

C:\dev\Python27\Scripts>easy_install pil
Searching for pil
Reading http://pypi.python.org/simple/pil/
Reading http://www.pythonware.com/products/pil
Reading http://effbot.org/zone/pil-changes-115.htm
Reading http://effbot.org/downloads/#Imaging
Best match: PIL 1.1.7
Downloading http://effbot.org/media/downloads/PIL-1.1.7.tar.gz
Processing PIL-1.1.7.tar.gz
Running PIL-1.1.7\setup.py -q bdist_egg --dist-dir c:\users\sgeorge\appdata\local\temp\easy_install-r4zhu9\PIL-1.1.7\egg-dist-tmp-b3pkv1
WARNING: '' not a valid package name; please use only.-separated package names in setup.py
error: Setup script exited with error: Unable to find vcvarsall.bat

I found out here that you need the free Visual Studio 2008 Express Edition (ISO file : 749MB). This can be extracted using 7-zip. I found out the hard way that this error does NOT go away if you install Visual Studio 2010 (ISO file : 694MB). Now I have this new error:

error: Setup script exited with error: command 'cl.exe' failed: No such file or directory

The above error went away when I closed and opened the cmd window. However I got this error message after that:


File "C:\dev\Python27\lib\distutils\msvc9compiler.py", line 299, in query_vcvarsall
raise ValueError(str(list(result.keys())))


I gave up and installed 32-bit version of Python and this worked fine

January 19, 2012

Using the cURL to test web services

I was working on the Google App Engine tutorial to write a web service. They suggested using cURL to test it. cURL is a command line tool for transferring data I downloaded it and used it like this on windows:

C:\dev>curl -H 'content-type: application/json' -d '{"text": "Hello world"}' http://localhost:8080/postservice.post_note

Note: I had a space after the colons above , and it gave me the following errors in the windows command prompt:

curl: (3) [globbing] unmatched close brace/bracket at pos 12
curl: (7) couldn't connect to host
curl: (6) Could not resolve host: 8080; Host not found

Installing python modules with easy_install

I wanted to add modules Jinja2 and markupsafe to python. All I needed to do was run the easy_install module with the a parameter. easy_install will search for the module, download and install it.  First you need to download setuptools from this location. I downloaded the version for windows and ran the setup. It detects the Python directory on your machine and installs easy_install.exe under the Scripts subdirectory.

Here's an example usage:


C:\dev\Python27_32bit\Scripts>easy_install markupsafe
Searching for markupsafe
Reading http://pypi.python.org/simple/markupsafe/
Reading http://dev.pocoo.org/
Best match: MarkupSafe 0.15
Downloading http://pypi.python.org/packages/source/M/MarkupSafe/MarkupSafe-0.15.tar.gz#md5=4e7c4d965fe5e033fa2d7bb7746bb186
Processing MarkupSafe-0.15.tar.gz
Running MarkupSafe-0.15\setup.py -q bdist_egg --dist-dir c:\users\sgeorge\appdata\local\temp\easy_install-vfp6j3\MarkupSafe-0.15\egg-dist-tmp-eazrvh_speedups.c
   Creating library build\temp.win32-2.7\Release\markupsafe\_speedups.lib and object build\temp.win32-2.7\Release\markupsafe\_speedups.exp
Adding markupsafe 0.15 to easy-install.pth file

Installed c:\dev\python27_32bit\lib\site-packages\markupsafe-0.15-py2.7-win32.egg
Processing dependencies for markupsafe
Finished processing dependencies for markupsafe

January 18, 2012

Using patch

I got this error working on Google App Engine SDK 1.6.1 with Python 2.7.2 and trying to use Jinja2 on Windows:
WARNING  2012-01-18 23:09:40,239 py_zipimport.py:139] Can't open zipfile C:\dev\Python27\lib\site-packages\setuptools-0.6c11-py2.7.egg: IOError: [Errno 13] file not accessible: 'C:\\dev\\Python27\\lib\\site-packages\\setuptools-0.6c11-py2.7.egg'

Comment 29 on this post suggested that I apply a patch. I downloaded the patch util and executed this command to apply the patch:
C:\Program Files (x86)\GnuWin32\bin>patch "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver_import_hook.py" < "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver_import_hook.patch"


Howerver, I got this error:
patching file C:\Program Files (x86)\Google\google_appengine\google\appengine\tool\dev_appserver_import_hook.py
Assertion failed: hunk, file ../patch-2.5.9-src/patch.c, line 354

This post suggested that I open the file in wordpad and save it using:
C:\Program Files (x86)\GnuWin32\bin>write "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver_import_hook.patch"

After that when I ran patch, it worked and I got this message:
patching file C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver_import_hook.py
Hunk #1 succeeded at 257 (offset 7 lines).
Hunk #2 succeeded at 280 with fuzz 2 (offset 7 lines).

Although this did not fix my initial issue, I got to learn how to do a patch.

February 19, 2008

HTML to Excel with JSP

response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition","attachment; filename=" + "vmd.xls");
String htmlTable = (String)request.getParameter("htmlTable");
out.print(htmlTable); //htmlTable is string which contains HTML table code.

Basic JDBC: Executing a simple SELECT statement


// Load the JDBC driver.
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");

// Establish a connection
url = "jdbc:db2:INSURANCE";
username = "myName";
password="myPassword";
Connection connection = DriverManager.getConnection(url,username,password);

// Create and execute query
String query = "SELECT name, age, dob FROM COMMERCIAL.AUTO";
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next())
{
String name = rs.getString("name");
int age = rs.getInt("age");
Date = rs.getDate("dob");
}

connection.close();