Posterous theme by Cory Watilo

MacOS X 10.7.x, Python 2.7 and PIL

Python Imaging Library (PIL) on Mac OS X Lion using Python 2.7

Here is how to build PIL. You must have the Apple Developer tools installed in order for this work. These can be found in the Apple Store under the name Xcode. Python 2.7 is bundled with OS X Lion so everybody should have it already. Download the linked requirements below and save them in your /tmp directory or copy them there manually.

If you wish to use the Finder to copy them, you'll need to press G to get the "Go to folder dialog". Enter /tmp to open a Finder window for that directory. For the rest of this tutorial, however, you will need to know how to use the Terminal.app to complete this tutorial.

Before we can build PIL we will need to build both libjpeg and libfreetype. We will start with libjpeg. Follow these commands. $ means the user can be any non-super user. # means that the user must be logged in as a super user or root level account.

$ cd /tmp
$ tar xzvf jpegsrc.v8d.tar.gz
$ cd jpeg-8d
$ env CFLAGS="-O -g -isysroot /Developer/SDKs/MacOSX10.7.sdk -arch i386 -arch x86_64" LDFLAGS="-arch i386 -arch x86_64" ./configure --disable-dependency-tracking
<when that completes>
$ make
<if there are no errors>
$ sudo make install 

Next up we need to build libfreetype

$ cd /tmp
$ tar xzvf freetype-2.4.8.tar.gz
$ cd freetype-2.4.8
$ env CFLAGS="-O -g -isysroot /Developer/SDKs/MacOSX10.7.sdk -arch i386 -arch x86_64" LDFLAGS="-arch i386 -arch x86_64" ./configure --disable-dependency-tracking
<when that completes>
$ make
<if there are no errors>
$ sudo make install

Finally before we can build PIL. First make sure you're using the correct python.

$ which python
<should be something like '/Library/Frameworks/Python.framework/Versions/2.7/bin/python'> 

If you don't see 2.7 in the output from the which command, try:

$ which python2.7
<as long as you don't see '-bash: python2.7: command not found', you'll likely be ok> 

If you need to use python2.7 instead of python, please replace python with python2.7 in all places going forward. Without further ado, let's get to the final step and build PIL.

$ cd /tmp
$ tar xzvf Imaging-1.1.7.tar.gz
$ cd Imaging-1.1.7
$ env CFLAGS="-O -g -isysroot /Developer/SDKs/MacOSX10.7.sdk -arch i386 -arch x86_64" LDFLAGS="-arch i386 -arch x86_64" MACOSX_DEPLOYMENT_TARGET="10.7" ARCHFLAGS="-arch i386 -arch x86_64" python setup.py build
<when that completes and if there are no errors>
$ sudo env CFLAGS="-O -g -isysroot /Developer/SDKs/MacOSX10.7.sdk -arch i386 -arch x86_64" LDFLAGS="-arch i386 -arch x86_64" MACOSX_DEPLOYMENT_TARGET="10.7" ARCHFLAGS="-arch i386 -arch x86_64" python setup.py install

Ok, let's test to see if it's installed correctly.

$ python
Python 2.7 (r27:82508, Jul 3 2010, 21:12:11)
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import Image
>>>  

As long as you get another empty prompt you have successfully installed and built PIL. Enjoy using it.

 

 

Keywords: Python2.7 PIL Mac OS X Lion

Posting files with node and expressjs

I am writing this, mostly to myself as a reminder on how to do it. The documentation for node and expressjs in general is really quite bad. They really expect everybody to basically read the JavaScript and learn through the code.

So this post is on how to submit a file using a multipart form request and how to manipulate the file on the server side once it's been sent. The first thing to know is that in the most recent versions of expressjs and nodejs (as of this writing), you don't need any other tools like connect-form or various others that do the same thing in order to parse the multipart request.

If your form element has a name attribute called "upload", then in the req object passed into the route function, you'll have a files object. This files object acts as a dictionary for the form inputs of type file. In my case, <input type="file" name="upload"> you will end up seeing the following if you did a console.dir(req.files.upload);

{
  upload: {
    size: 332853,
    path: '/private/tmp/test/uploads/ef8037ea95188bef1f0b2e88dc4d1294.jpg',
    name: '26159.jpg',
    type: 'image/jpeg',
    lastModifiedDate: Wed, 14 Dec 2011 08: 30: 41 GMT,
    _writeStream: {
      path: '/private/tmp/test/uploads/ef8037ea95188bef1f0b2e88dc4d1294.jpg',
      fd: 8,
      writable: false,
      flags: 'w',
      encoding: 'binary',
      mode: 438,
      bytesWritten: 332853,
      busy: false,
      _queue: [],
      drainable: true
    },
    length: [Getter],
    filename: [Getter],
    mime: [Getter]
  }
}

Looking carefully at the above output, you'll see that an object literal with many interesting properties was assigned as the value of req.files.upload. If I add another input with the same name, it becomes an array of these same properties as seen below. Before you ask, yes the file names in the 'name' field are correct. They weren't nicely named to begin with.

{
  upload: [
    {
      size: 206727,
      path: '/private/tmp/test/uploads/854573a2aad50ce26eaf775d6ec53353.jpg',
      name: '44905_uGR2j-f7d482888874046f92b93801df9a37e2-3.jpg',
      type: 'image/jpeg',
      lastModifiedDate: Wed, 14 Dec 2011 08: 45: 09 GMT,
      _writeStream: [Object],
      length: [Getter],
      filename: [Getter],
      mime: [Getter]
    },
    {
      size: 360499,
      path: '/private/tmp/test/uploads/991c1680134ee162f50330f6c1c2d53f.jpg',
      name: '89933_NV8Jd-291d372b2d1e0f6813e60ffebd2c9baf.jpg',
      type: 'image/jpeg',
      lastModifiedDate: Wed, 14 Dec 2011 08: 45: 09 GMT,
      _writeStream: [Object],
      length: [Getter],
      filename: [Getter],
      mime: [Getter]
    }
  ]
}

In order to know where these files will be saved, you need to specify it in your app.js file for express. I created a directory called uploads in the root of my project and pointed the uploaded files directory here. I also told the bodyParser() to keep the file name extensions from the original file name.

To do this, change the code from:

app.use(express.bodyParser());

to this code:

  app.use(express.bodyParser({
    uploadDir: __dirname + '/uploads',
    keepExtensions: true
  }));

This alone is enough to get you started. Remember to add the encoding type to your form element in the html. Without out, you won't get anything but a file name string.

<form action="/sendfile" method="POST" enctype="multipart/form-data">

Feel free to leave questions or comments and I'll update this accordingly.

MacOS X 10.6.x, Python2.7 and PIL

Python Imaging Library (PIL) on Mac OS X Snow Leopard using Python 2.7

If you are trying to do this under OS X Lion, please see my other similar related article http://nyteshade.posterous.com/macos-x-107x-python-27-and-pil

Here is how to build PIL. You must have the Apple Developer tools installed in order for this work. They are located on your Snow Leopard install disc. I am assuming you're using the Python 2.7 distribution fromwww.python.org. Download the linked requirements below and save them in your /tmp directory or copy them there manually.

If you wish to use the Finder to copy them, you'll need to press G to get the "Go to folder dialog". Enter /tmp to open a Finder window for that directory. For the rest of this tutorial, however, you will need to know how to use the Terminal.app to complete this tutorial.

Before we can build PIL we will need to build both libjpeg and libfreetype. We will start with libjpeg. Follow these commands. $ means the user can be any non-super user. # means that the user must be logged in as a super user or root level account.

$ cd /tmp
$ tar xzvf jpegsrc.v7.tar.gz
$ cd jpeg-7
$ env CFLAGS="-O -g -isysroot /Developer/SDKs/MacOSX10.6.sdk -arch i386 -arch x86_64" LDFLAGS="-arch i386 -arch x86_64" ./configure --disable-dependency-tracking
<when that completes>
$ make
<if there are no errors>
$ sudo make install 

Next up we need to build libfreetype

$ cd /tmp
$ tar xzvf freetype-2.4.4.tar.gz
$ cd freetype-2.4.4
$ env CFLAGS="-O -g -isysroot /Developer/SDKs/MacOSX10.6.sdk -arch i386 -arch x86_64" LDFLAGS="-arch i386 -arch x86_64" ./configure --disable-dependency-tracking
<when that completes>
$ make
<if there are no errors>
$ sudo make install

Finally before we can build PIL. First make sure you're using the correct python.

$ which python
<should be something like '/Library/Frameworks/Python.framework/Versions/2.7/bin/python'> 

If you don't see 2.7 in the output from the which command, try:

$ which python2.7
<as long as you don't see '-bash: python2.7: command not found', you'll likely be ok> 

If you need to use python2.7 instead of python, please replace python with python2.7 in all places going forward. Without further ado, let's get to the final step and build PIL.

$ cd /tmp
$ tar xzvf Imaging-1.1.7.tar.gz
$ cd Imaging-1.1.7
$ python setup.py build
<when that completes and if there are no errors>
$ sudo python setup.py install

Ok, let's test to see if it's installed correctly.

$ python
Python 2.7 (r27:82508, Jul 3 2010, 21:12:11)
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import Image
>>>  

As long as you get another empty prompt you have successfully installed and built PIL. Enjoy using it.

 

 

Keywords: Python2.7 PIL Mac OS X Snow Leopard

 

Scripting Fallout New Vegas

I was playing Fallout New Vegas last night after a sizable break. I spent a couple of hours getting my visual mods to work the way that I want them to. I've still not beaten the game so I am not quite ready to use some of the more drastic mods that the affect the built-in hardcore mode or greater story arc. I am an avid Fallout 3/NV fan. I think Bethesda really did a phenomenal job with Fallout 3 despite the whining I hear from the creators and fans of Fallout 1 and 2 which were good games as well.

The more I play however, the more I feel inclined to jump back into scripting my own mods for the game. For me it feels like a foot step into the gaming industry which is also something I want. More on this later as well as some modding ideas.