Saturday 15 November 2008

in 5 minutes, for free (or 'balanced restored')

Originally posted in The Daily Daniel
--------------------------------------

People have said to me words along the lines of "why do you both with linux? it seems as though all it ever gives you is problems blah blah blah ..." - i don't know, I never listen to the rest. Yes, I do moan about linux a lot, and even in these pages ... well here's just 1 example of some routine thing I wanted to do where linux came to the rescue. I did it in 5 minutes

I had a folder full of pictures, and I wanted to make 2 copies of each - one a fraction of the size (for a thumbnail gallery), and one loads bigger - the images were smaller than the CCD used to capture them, so I knew I couldnt alias them. Right, well there's a linux command which does the job called 'convert'. Quick, really simple to use, e.g.:

>> convert my_image.jpg -resize 200% my_new_image.jpg

Does exactly what it says on the quick drying woodstain. But how to do the whole folder in one go? My old friend matlab comes to the rescue, and my personal favourite command 'system' - this bit of code will read in a folder of images, create 2 new folders, copy and resize each image, save it in the correct folder and append the file name with the operation done to it. All in the time it takes you to sneeze.

% find all the jpegs in the folder
>> direc=dir([pwd,filesep,'*.','jpeg']);

% create an empty cell to stick them in
>> filenames={};

% deal inputs into outputs
>> [filenames{1:length(direc),1}] = deal(direc.name);

% sort them and turn them into character arrays
>> filenames=sortrows(char(filenames{:}));

% make 2 directories - one called 'james_and_the_giant_peach' and one called 'thumblina'
>> mkdir([pwd,filesep,'james_and_the_giant_peach'])
>> mkdir([pwd,filesep,'thumblina'])

% simple loop
>> for i=1:size(filenames,1)

% giant images (500% size)
>> system(['convert ',deblank(filenames(i,:))...
,' -resize 500% ./james_and_the_giant_peach/',deblank(filenames(i,:)),'_giant.jpeg'])

% thumbnail images (20% size)
>> system(['convert ',deblank(filenames(i,:))...
,' -resize 20% ./thumblina/',deblank(filenames(i,:)),'_thumb.jpeg'])

>> end

Another example where matlab and linux combine beautifully. Matlab would have done it on its own (using imresize and imwrite commands), but it would have taken considerably longer.

Today is Boomtime. Good night

No comments: