Friday 22 January 2010

Websiting

This morning I checked my bug report on Imagick and nothing had been done, so I thought I better change my website to call ImageMagick from the command line instead of using the PHP Imagick extension.

I looked up how to do sharpening in ImageMagick, and found some info about adaptiveSharpen, which is meant to be more like Photoshop's smart sharpen filter. So I edited my page to do an adaptive sharpen, resize, then another adaptive sharpen using ImageMagick. But when I tested it, I found that I was getting a 504 Gateway Time-out from Nginx.

Googling about this I found this thread on stack overflow: How do I prevent a Gateway Timeout with Nginx. After adding fastcgi_read_timeout 120; to my site config, I found that I was still getting the timeout error. Looking at the error logs I found the problem was not with my nginx/fastcgi backend, but rather the nginx frontend, timing out waiting for the response from the nginx backend.

So I added proxy_read_timeout 120; to the front end Nginx config, but still got a 504 Gateway Time-out. I decided to run the ImageMagick command in the terminal and time how long it actually took. I don't know how to time stuff in Linux so I just ran top refreshing every second and kept an eye on the time column until the process ended, which was quite boring.
$HOME/apps/ImageMagick/bin/convert \
-limit memory 64 \
-limit map 128 \
-filter Lanczos \
$originalImage \
-adaptive-sharpen 0x.6 \
-set option:filter:filter Lanczos \
-set option:filter:blur 0.8 \
-resize 1024x720 \
-adaptive-sharpen 0x.6 \
$HOME/resizedImage.jpg
Amazingly it managed to take 6 minutes 45 seconds to finish (this was with a 10000px x 5000px approx image. So I tried the same thing with unsharp mask to see if was any quicker:
$HOME/apps/ImageMagick/bin/convert \
-limit memory 64 \
-limit map 128 \
-filter Lanczos \
$originalImage \
-unsharp 0.5, 0.5, 5, 0.05 \
-set option:filter:filter Lanczos \
-set option:filter:blur 0.8 \
-resize 1024x720 \
-unsharp 0.5, 0.5, 1.66667, 0.05 \
$HOME/resizedImage.jpg
But this took 16 minutes 57 seconds - now that was a long time to wait just watching top!

Also, someone had replied to my question on the Ubuntu forums on how to record memory usage of a program/process until the program or process finishes, here's the script they suggested:
$#!/bin/bash
$HOME/apps/ImageMagick/bin/convert -limit memory 64 -limit map 128 $ORIGINAL_IMAGE -set option:filter:filter Lanczos -set option:filter:blur 0.8 -resize 1024x720 $HOME/resizedImage.jpg &
CMD_PID=$!

if [ $? -eq 0 ]; then
while [ $? -eq 0 ]; do
sleep 2
ps --no-headers -p $CMD_PID -o pid,%cpu,rss,cmd >> test.txt
done
fi

I must admit I don't really understand how it works - $? contains the return code of the last executed command. So I would of thought that $? would start off as null (or something similar) and then be 0 when ImageMagick has successfully processed the image. But it works, so I guess that's all that matters really.

The time taken for processing by ImageMagick is quite bad, especially considering that I also need to convert the image to sRGB, create two smaller versions, and create a thumbnail as well. So I looked to see if I could find anything about Lightroom 3 and creating an export plugin like the one it ships with for Flickr. Then I could resize my images in Lightroom (which could also use the superior smart sharpen) and upload the different sized images to the web server instead of the web server having to do the image resizing and sharpening.

I couldn't find anything so I posted a question about it on the Lightroom forums.

On my website it doesn't currently have any facility for dealing with grayscale images (as far as I'm aware), so I checked to see how grayscale (Dot Gain 20%) images look in a non colour managed web browser. First I had to find a non color managed browser. I tried Firefox with an adobe RGB first, but it rendered it with the correct colours, so I guess that Firefox 3.5 is now colour managed.

So I tried IE8, which of course isn't colour managed (given that FF has only recently implemented colour management, I wouldn't expect IE to implement for a couple of years). There was a very noticable difference between the grayscale image in IE and in Photoshop - much more contrasty in IE, with the darker tones very dark in IE.

After this I did more work on trying to get the equivalent ImageMagick command to my Imagick commands. I needed to get ImageMagick to convert the image to sRGB. But again, I needed a way to check whether the conversion had been successful. I don't have Internet Explorer on Ubuntu, so I couldn't use that. I tried Conkeror, but it wouldn't do anything when I dragged an image onto it.

I tried Konqueror, which loaded the image at half its actual size (weird, since it wasn't resizing it to fit in the screen or anything, just halving its size). I knew that Firefox was colour managed, so I tried opening an image in Firefox and also the 'eye of GNOME' default image viewer in Ubuntu. Strangely, it seems that the eye of gnome isn't colour managed, which is weird for a specialised image viewing program. Anyway, this at least gave me a way to compare a colour managed and non-colour managed version of an image.

I tried removing the sharpening before resizing the image, and this massively sped up the process - even though ImageMagick was now also converting the image to sRGB (from adobe RGB). I didn't time it, but it took about 30 seconds compared to the nearly 7 minutes it was taking before.

I then tried tweaking the sharpening settings to try and get the output as good as it was when using Imagick, but changing the adaptive sharpen parameters didn't seem to make any difference. Adaptive Sharpen must be working though, since when I was using it on the image before resizing as well as after resizing (as well as taking a lot longer), the final image was sharpened a lot more than the same one processed with unsharp (instead of adaptive sharpen).

In the evening I finished watching the Mysterians with Mauser and L, and watched Power Rangers with L. I played on Banjo Kazooie Nuts & Bolts, but it was really hard to control with the Xbox steering wheel (and pedals). Then I did some more website stuff.

The weather today was overcast and rained all day.

Food
Breakfast: Orange Marmalade Toast Sandwich; Cup o' Tea.
Lunch: Grated Mature Cheddar with Salad Sandwich; Packet of Prawn Cocktail Flavour Crisps; Clementine; Grapes; Mint KitKat; Cup o' Tea; 2x Pieces of Sainsbury's Mint Creme Chocolate.
Dinner: Breaded Fish Portion; Tartar Sauce; Peas; Tinned New Potatoes; Ground Black Pepper. Pudding was some Apple Crumble that L made at School today with Custard. Coffee; 2x Pieces of Sainsbury's Truffle Chocolate; Piece of Sainsbury's Caramel Chocolate.
Supper: Shortbread finger; Dark Chocolate Digestive; Cup o' Tea.

No comments: