Thursday, August 13, 2009

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).

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 ProcessingNo 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:

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)

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:

Be default, we all can use python interpreter to test smaller codes but really, it is not a feasible way to write fast code so you can use Eclipse, probably the best open source IDE in the market and create and manage python programs using it.

IBM's Eclipse project was a veritable breath of fresh air, at a moment when it was most needed. The development of open source IDEs seemed to have stopped – with a lot of users sticking to emacs and vi, despite spectacular (but unfruitful) development from Kdevelop and Anjuta.

Eclipse is very extensible and able to work with just about any programming language. In this short tutorial, we will talk about how to set up a Python development environment, using Eclipse and PyDev. The reference distribution is Ubuntu, but aside from the command to install packages, everything will be identical on any distribution.

Step 1.

Obtain and install the Java Runtime Environment (JRE). In most cases, there is a package readily available for most distributions, and it is a dependency for Eclipse. You only need to install JRE manually if there is no readily-available package, or if your package manager does not resolve dependencies automatically (and unless you are using plain RPM, without Yum, or Slackware's package manager without a tool like slapt-get, chances are it does)

Step 2.

Install Python. The version you use depends on your needs. PyDev has a rather wide support.

Step 3.

Install Eclipse and PyDev. Most distributions have a separate package for Eclipse and PyDev. However, there are a lot of distributions which do not provide a PyDev package, since Eclipse can install it itself.

If PyDev cannot be installed by using the distribution's package manager, it can be installed by using Eclipse's Update Manager. To install PyDev from Eclipse, go to Help, Software Updates and click Find and Install.

In the wizard window that appears, choose “Search for new features to install” and click Next. This will open a new window. Click “New Remote Site”, and in the dialog box that appears, enter “PyDev Extensions” in the Name text box, and http://www.pydev.com.br/updates in the URL text box. Click Ok and follow the subsequent on-screen instructions.

Step 4.

Configure the Python interpreter. After you have installed PyDev, open Eclipse and go to Window, Preferences, PyDev, Interpretor – Python (or Jython if you use it instead of Python). Near the Python interpreters list box, click New and navigate to the Python executable on your system (it's usually /usr/bin/python. If it's not there, run the command 'which python' without quotes, and use the path it returns). PyDev will automatically detect some required settings. Once this is over, you are ready to go.

You can now use Eclipse to work on your Python projects. Most project management functions should be familiar to you, and PyDev has a wide range of features, from code completion to refactoring tools.