Monday, April 18, 2011

Compress Scanned PDF document using ImageMagick

Today, I needed to scan a document and emailed it to a friend. The scanned document was over 20M for a page and I had several pages to be sent. Apparently, the scanned document was too large and  I needed to compress them.

One way to do that in Mac is using Preview. Open the scanned pdf with Preview, choose "File/Save As" and then "Reduce File Size" in the "Quartz Filter" drop down list.  However, the image result I got from this method was very poor. The characters in the image were almost unreadable.

I tied several other ways by googling the topic, most of the methods I found did not allow me to set compression ratio, Finally, I found ImageMagic which allowed me to set compression ratio explicitly.  Here is the command I used to compress the scanned PDF file.

convert -density 300x300 -quality 5 -compress  jpeg input.pdf output.pdf


The quality parameter is used to  set compression ratio and it should be between 1 to 99 with 1 being the lowest quality and highest compression ratio setting.

Friday, April 1, 2011

Streamline building linux applications on Mac with Textmate

Most of the work I do on a daily basis is developing and building middleware on a Linux system. However, I also like to write code with Textmate on my Macbook. The major reason I like Textmate is its extensibility and the keyboard shortcuts.

On the other hand, writing code with Textmate on the Mac and then upload it Linux machine to compile  is tedious and painful. Two things needs to be automated. First, I don't want to upload the code to the Linux machine every time I make some modification to the code. I tried to use MacFuse and sshfs to mount a remote directory on my Mac, but there was a serious performance issue when I tried to edit files in the mounted directories with Textmate. Second, I want a feature in which clicking on the error messages generated by the compiler would link to the corresponding line in the source file.  The C bundle in Textmate already provide this feature, but it only works for compiling code on the local machine.

To achieve these goals, I modified the make command in Textmate C bundle so that when a project contains a Makefile.tm file, it will use the file instead of the regular Makefile. The Makefile.tm file then uses rsync to upload files and then invokes the make command remotely via ssh. 

However, that still doesn't solve the error message linking problem. That is because the user home directory in Linux usually starts with "/home" while  in OS X it starts with "/Users".  It would be simple if we could directly use "ln -s /Users/huangming /home" to link my home directory in OS X under "/home".  Unfortunately,  it is not quite as easy as that. OS X reserves "/home" for auto-mounting NFS directories. Typing the "ln" command in the terminal won't work.

If you don't use NFS,  this issue can be solved by modifying "/etc/auto_master" file, comment the line starting with "/home", save the file and restart the system.  Afterwards, using the "ln" command to link home directory under "/home" will work.

With the above modification, the process of developing applications for Linux on a Mac can be greatly streamlined.


Saturday, December 11, 2010

How to use Subdowloader on a Mac

SubDownloader is a very convenient tool to download subtitles files for movies or TV shows. However, it does not work well on Macs. The latest official build for Mac  (2.0.9.3) uses some obsolete  APIs to interact with www.opensubtitles.org and it no longer works. Building directly from the latest source tarball (2.0.13) will hit the infamous dependency hell because of its reliance on the QtPy.

Recently I found another way to use SubDownloader on Macs.  Instead of resolving the dependency issues on Macs, I just gave upon using the graphical interface of SubDowloader and made it work for the command line only; then I used Automator to build a service so that it could download subtitles by right clicking a file or folder from Finder and choose Download Subtitles in the popup menu. It turned out to work very well.

Procedures to install SubDownloader on a Mac:
  1. Download SubDownloader source tarball and decompress it.
  2. Open terminal and patch a bug in subdownloader-2.0.14
    • $ sed -i.bak 's/subtitle = video\.getOneSubtitle()/subtitles = video.getOnlineSubtitles()[0]/' ~/Downloads/subdownloader-2.0.14/modules/filter.py
  3. Download kaa-base and kaa-metadata from http://sourceforge.net/projects/freevo/files/ and decompress them.
  4. Install Xcode if you haven't done that.
  5. Open terminal and type the following commands.
    • $ sudo python setup.py install
    • $ cd Downloads/
    • $ sudo python setup.py install
    • $ cd ../kaa-medata-0.7.7/src/disc/
    • $ curl -O http://cddb-py.sourceforge.net/CDDB/unix/cdrommodule.c
    • $ cd ../..
    • $ sudo python setup.py install
  6. Create a service using Automator
    • Open Automator.
    • Click the "Service" icon and then click "Choose" button on the bottom.
    • Choose "files and Folders" and "Finder" on the list boxes right of the text "Service receives selected".
    • Drag "Run Shell Script" from the list of actions into the workflow window.
    • Select "/usr/bin/python" and paste the following script into script window; change the text "/Users/myname/Downloads/subdownloader-2.0.14" in the script to the folder which you place the subdownloader files.
    import os,sys
    if __name__ == '__main__':
      paths = []
      for i in range(1,len(sys.argv)):
        paths.append(os.path.realpath(sys.argv[i]))
    
      os.chdir("%s/Downloads/subdownloader-2.0.14" %(os.environ["HOME"]))
      for x in paths:
        os.system("./run.py -c -D -l en --rename-subs -V  \"%s\"" % x)
    
  7. Save the workflow with the name "Download Subtitles"