Friday, 4 May 2012

Batch converting PDF to EPS

Today I found myself needing to convert about 20 vector images from PDF to EPS, because a journal that I want to submit an article to, demands that all graphics are in EPS format.

Searching the net, I found several solutions, many of them involving the pdf2ps or pdftops command-line utilities. The best solution was the one I found here, which converts all the images in a folder with a single-line for loop:

for f in *.pdf; do pdftops -eps $f; done

The code is run on the command line. I've only tried this in Linux, but some version of it may work in Windows or OSX as well.

Monday, 20 February 2012

Batch converting images files from one format to another using ImageMagick

Let's say you have 500 JPG images and want to convert them all to PNG in one go. The mogrify function in the ImageMagick software suite comes in handy here:

mogrify -format png *.jpg

This produces a copy of each image in the new format. It's possible to combine the conversion with other operations, for example resizing and conversion to monochrome:

mogrify -resize 50% -monochrome -format png *.jpg

I often create PDF vector graphics which I use together with PDFLaTeX. However, sometimes I need bitmap versions of the vector graphics images. Mogrify can convert from PDF to bitmap formats (for example PNG), but it's important to set the density of the sampling grid in the conversion (dots per inch), otherwise the bitmap image might have a too low resolution. Example:

mogrify -format png -density 600 *.pdf

The commands above can be executed directly in the terminal if you are inside the folder containing the images. The folder can also be specified explicitly, for example

mogrify -format png /home/martin/images/*.jpg

Wednesday, 4 January 2012

Running a graphical application remotely using SSH

This finding is in no way revolutionary, but I found it useful, so maybe others will too. I'm running Ubuntu Linux on my laptop, and recently I wanted to run a Matlab script on a Linux workstation. I can log on the the workstation using ssh, for example

ssh martin@server.name.no

which gives me access to the terminal. However, SSH can also be used for graphical programs, if the server is running a X window system. The SSH connection is configured for X session forwarding by adding a "-X" switch at logon,

ssh -X martin@server.name.no

Such forwarding is further explained here. If I logon using the above syntax and start matlab, it appears as a regular window on my desktop, although it's running on the workstation. Unfortunately it can be a bit slow in responding to mouse and keyboard input, so using the terminal version of Matlab may in some cases be just as good.

Sunday, 11 December 2011

Changing the bit depth of images using ImageMagick

Today I wanted to create a simple black-and-white version of a grayscale image that I have. This is pretty easily done in Matlab (you can scale the image to values between 0 and 1 and use round()), but I was guessing there was probably already some simple way to do it in ImageMagick - and there is!

This is the original version of the image (from the great graphical novel series "Bone"):


To convert it to a 3-bit image (8 levels), I used the -depth switch with the convert command:

convert bone.jpg -colorspace gray -depth 3 bone3bit.jpg


And similarly, for black-and-white (1 bit image):

convert bone.jpg -colorspace gray -depth 1 bone1bit.jpg






Friday, 11 November 2011

Setting up Matlab mex compilation in Ubuntu


NOTE: This post was updated on January 16, 2012.

Recently I have been trying to compile some Matlab mex-files from the excellent Machine Vision Toolbox by Peter Corke. When I first tried to compile, I got the following warning:

Warning: You are using gcc version "4.6.1-9ubuntu3)".  The version
         currently supported with MEX is "4.3.4".

I tried several ways of "downgrading" gcc, but as it turns out, the newer version of gcc on Ubuntu 11.10 is backwards compatible and works fine. The message above is simply a warning, not an error, and there is no need to change gcc to an older version. However, my real problem was this: I got errors of the type

error: expected expression before ‘/’ token

I had no idea why this error was popping up, until I read a post on similar issues on xcorr. It turns out that C++ style "//" comments are not allowed in the standard mex configuration that is set when you run "mex -setup". To avoid the error, open the mexopts.sh file (mine is located in ~/.matlab/R2011b/mexopts.sh), and replace all occurences of "-ansi" with "-std=c99".

Thursday, 3 November 2011

Batch renaming files in Ubuntu Linux

I often deal with a large number of data files, for example files containing ultrasound data from experiments. Recently I had to rename about 200 files which were named something like this,

oldName001.mat, oldName002.mat, ..., oldName200.mat

to something like this:

newName001.mat, newName002.mat, ..., newName200.mat

Basically, I wanted to keep the numbering but change the base of the name. I found out that on Ubuntu 11.10, which I'm currently using, there are two different flavors of the "rename" command. One is a Perl script, and using this, the syntax for doing the above operation is

rename "s/oldName/newName/" *mat

To test without actually renaming, use the "-n" switch. Type "man rename" or have a look at these Webmaster Tips for several more examples of this usage. However, there is also a rename command as part of the linux-util package. To distinguish this from the above command in Ubuntu, call the command as rename.ul:

rename.ul oldName newName *mat

Notice that the syntax is simpler and easier to remember for this version. Type "man rename.ul" or have a look at this NixCraft post for more examples. 

Finally I found out that it is also possible to batch rename files using Thunar, which is the default file manager in Ubuntu 11.10. Mark all the files, right-click and choose "rename", and use the "Search and replace" option.

Wednesday, 26 October 2011

Installing Matlab with standalone user on Ubuntu Linux

OK, after doing this in an awkward manner a couple of times, here is how I install Matlab on Ubuntu with a standalone license:

  • Download the files from Mathworks
  • Run the install script with sudo (as root), but DO NOT activate the license yet. Choose custom install, use default install location, tick the box for creating symbolic links.
  • After installing, try to run Matlab (not as root). The activation procedure will start automatically. Register your own user name for the standalone license.
  • Matlab (with GUI) can now be run by calling the command "matlab -desktop". To create a application launcher for it, use the "Main Menu" program (also called Alacarte, can be installed with the Ubuntu Software Center). Get a logo for the launcher here, or put it right where it belongs by using this command:
sudo wget http://upload.wikimedia.org/wikipedia/commons/2/21/Matlab_Logo.png -O /usr/share/icons/matlab.png

I also recommend having a look at the Ubuntu Documentation for Matlab installation. It might not always be completely up to date, but the procedure doesn't change much between releases.