Today I was reading trough the ImageMagick ChangeLog and noticed an interesting entry. "Add support for liquid rescaling". I rushed to check the MagickWand API docs and there it was: MagickLiquidRescaleImage! After about ten minutes of hacking the Imagick support was done. Needless to say; I was excited
For those who don't know what seam carving is check the demo here. More detailed information about the algorithm can be found here: "Seam Carving for Content-Aware Image Resizing" by Shai Avidan and Ariel Shamir
To use this functionality you need to install at least ImageMagick 6.3.8-2 and liblqr. Remember to pass --with-lqr to ImageMagick configuration line. You can get liblqr here: http://liblqr.wikidot.com/. The Imagick side of the functionality should appear in the CVS today if everything goes as planned.
Here is a really simple example just to illustrate the results of the operation. The parameters might be far from optimal (didn't do much testing yet). The original dimensions of image are 500x375 and the resulting size is 500x200.
Update: the functionality is pending until license issues are solved.
The original image by flickr/jennconspiracy

And the result:

Update. On kenrick's request here is an image which is scaled down to 300x300

#1 by Luc Raymond on February 13, 2008 - 11:40 pm
Quote
nice! I can’t wait to test the linux imagick version..
#2 by kenrick on February 14, 2008 - 12:12 am
Quote
hmm you coulda just cropped the pic since it was all sky to get the same efft
why not reduce the width instead? it would be more dramatic.
#3 by kenrick on February 14, 2008 - 12:30 am
Quote
pff effect, maybe i should learn how to type
#4 by Mikko Koppanen on February 14, 2008 - 12:39 am
Quote
Hi kenrick,
added another image which is the same image in 300×300 dimensions.
Trackback: PHPDeveloper.org
Pingback: Mikko Koppanen’s Blog: Seam carving | Development Blog With Code Updates : Developercast.com
#5 by Albertini on February 14, 2008 - 1:38 pm
Quote
Hi Koppanen,
Wow, really impressive.
I will put my previous question again since I still not solved my problem.
I would like to know how to make a random access to pixels.
It’s installed imagick 2.1.0RC3 lib.
I don’t want to iterate the pixels for performance purposes.
Thanks in advance,
Albertini
#6 by Mikko Koppanen on February 15, 2008 - 7:48 pm
Quote
Albertini,
I don’t know if there is a way to do random access directly. You can take an iterator for 1×1 pixels thou which is about the same.
#7 by PAW on February 15, 2008 - 9:00 pm
Quote
That’s very good stuff, Mikko.
Is your second image really seam-carved, or just cropped from the top? I took scientific measurements of your images (by laying my pen acroos the screen) and it seems to be simply cropped, rather than low-energy seams of pixel removal, from the sea, or foreground mountains.
Cheers
=PAW=
Pingback: Kowalikus » Seam carving w PHP - inteligentne skalowanie
#8 by Ertai on February 18, 2008 - 9:21 pm
Quote
Hi Mikko,
I’m trying to perform direct access to image pixel using the function “getImagePixelColor”.
However, the color array is empty. Also, I cannot use the function “setColorValue” once it does not accept any value as an possible color channel.
Here an example of what I am doing:
$pixel = $image->getImagePixelColor(10,10);
$color = $pixel->getColor();
$v = ($color[0] + $color[1] + $color[2])/3;
$pixel->setColorValue(imagick::CHANNEL_RED,$v);
Pingback: Savaitgalio skaitiniai #53 » Pixel.lt
#9 by Albertini on February 23, 2008 - 4:21 pm
Quote
Ok. Thanks Mikko.
#10 by George on February 27, 2008 - 2:32 pm
Quote
Hey Mikko,
Could we have an example of image enlarging using seam carving?
#11 by Aho on March 2, 2008 - 6:00 pm
Quote
I had loved to try this out, but when I try to build ImageMagick 6.3.9-1, it complains about it not finding glib.h when using –with-lqr, I guess it’s for it’s not located in /usr/include, but /usr/include/glib-2.0 (my system uses both the old and the newer 2.0 version of glib), and I can’t figure out how I get the configure to use an optional location for the headers.
#12 by Justin Patrin on March 16, 2008 - 10:26 pm
Quote
I’ve installed liblqr and ImageMagick 6.3.8.3 (with lqr support) and tried to build Imagick. The MagickLibVersion was set to 0×638 in ImageMagick but imagick.c has MagickLibVersion > 0×638 in it. I switched that to MagickLibVersion >= 0×638 and I can call liquidrescaleimage but I get the following error:
Fatal error: Uncaught exception ‘ImagickException’ with message ‘delegate library support not built-in `test.jpg’ (LQR)’ in seamcarving.php:6 Stack trace: #0 seamcarving.php(6): Imagick->liquidrescaleimage(500, 200, 3, 25) #1 {main} thrown in seamcarving.php on line 6
I’m seeing that this is an error in the ImageMagick library but I don’t know why. Any ideas?
#13 by Mikko Koppanen on March 16, 2008 - 10:29 pm
Quote
Justin,
did you pass –with-lqr to imagemagick configure command? Why do you build 6.3.8-3 instead of the latest version?
#14 by Justin Patrin on March 16, 2008 - 10:49 pm
Quote
It looks like ImageMagick isn’t configuring with lqr support for some reason. liblqr is definitely installed so I don’t know what’s happening here.
#15 by Mikko Koppanen on March 16, 2008 - 10:51 pm
Quote
Justin,
did you pass –with-lqr to imagemagick configure command?
#16 by Mikko Koppanen on March 16, 2008 - 10:52 pm
Quote
Should be with 2 x – but my blog seems to be stripping the second one.
#17 by Justin Patrin on March 17, 2008 - 5:54 pm
Quote
Yes, I compiled –with-lqr but the last output of configure is showing “no”. Something in there is disabling it.
#18 by izne on March 20, 2008 - 3:00 am
Quote
is there option to create protected areas (like faces or whole people) to not stretch them? as in the demo on youtube?
#19 by Aho on March 21, 2008 - 11:30 pm
Quote
Justin, I had trouble with this first, I had to include the path to the library to get it give a yes result:
–with-lqr=/usr/lib
but then I run into the trouble that glib-2.0 ain’t found properly when building the features depending on liblqr, even if the rest of imagemagick do find the headers without trouble.
#20 by Justin Patrin on March 22, 2008 - 7:48 am
Quote
No matter what I put in the configure line it won’t find lqr. I really hate the opaqueness of autoconf.
#21 by Justin Patrin on March 22, 2008 - 9:14 am
Quote
I finally got it working but I had to patch the configure script. It looks like either pkg-config isn’t set up right for lqr or there’s just something wrong with the configure script. Here’s what I added to configure, just before:
if test “$have_lqr” = ‘yes’; then
have_lqr=’yes’
LQR_CFLAGS=’-I/usr/include/lqr-1 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include’
LQR_LIBS=’-L/usr/lib -llqr-1′
#22 by Justin Patrin on March 23, 2008 - 9:39 pm
Quote
That didn’t quite come out right as the whitespace is being removed. Before this line:
—-
if test “$have_lqr†= ‘yes’; then
—-
put the following lines:
—-
have_lqr=’yes’
LQR_CFLAGS=’-I/usr/include/lqr-1 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include’
LQR_LIBS=’-L/usr/lib -llqr-1′
—-
The quotes above are supposed to be single quotes, not backquotes, the blog is switching them.
#23 by Aho on April 6, 2008 - 12:25 am
Quote
Did get the lqr to work in imagemagick++ 6.4.0.4, had to add -lglib-2.0 to the LQR_LIBS variable too, but when I buil the imagick 2.1.1 I don’t have any lqr functions at all (getting the undefined function error when trying to use the example code here).
#24 by Aho on April 6, 2008 - 1:53 am
Quote
I thought that the 2.1 was the cvs version, but looking around among the microsoft files, it turned out it had to be 2.2, sadly no good instructions how to get the cvs version, but managed after a bit experimenting to get together ar half working pecl package (it didn’t install the file, had to ctrl-c before pecl tried to install and then manually move files), at least it seems to work on x86 and PowerPC, just Sparc and amd64 to test, but thats for another day.
#25 by Mikko Koppanen on April 6, 2008 - 5:55 am
Quote
Aho,
http://phpfi.com/307759
#26 by Aho on April 6, 2008 - 9:53 am
Quote
Kiitos.
What about documentation, is there someone working on it?
The one I managed to find is at php.net and there is quite a lot more to wish for.
#27 by Mikko Koppanen on April 6, 2008 - 1:56 pm
Quote
Aho,
I don’t think there is anyone actively working on it at the moment. I don’t have that much time now days because of the new work etc. Do you feel like volunteering ?
#28 by Aho on April 6, 2008 - 8:48 pm
Quote
Sure, just the trouble I would need documentation to do that
If I knew more about imagemagick and imagick, then I think I would be able to write something, but all I know is more or less what I have read from your blogs, using your examples as base in what ever I done. Half of the parameters, I don’t know what they do.
I would be more than happy if we could clone you, twice or three times
#29 by Mikko Koppanen on April 6, 2008 - 9:13 pm
Quote
Aho,
luckily Imagick follows ImageMagick MagickWand API quite closely so the documentation for methods is quite similar to the ones found here: http://imagemagick.org/script/magick-wand.php. You can see for example: http://imagemagick.org/api/magick-image.php#MagickTintImage vs http://uk2.php.net/imagick-tintimage
#30 by Aho on April 11, 2008 - 11:07 pm
Quote
Then I will take a look as soon as I have time for this, got a new job and will be relocated to a new town, but as soon as I have settled I will try to improve the documentation.
Thanks for the links.
#31 by yves on April 12, 2008 - 3:00 am
Quote
Is it possible to aplpy this algorith to intelligently resize charts? An example would be http://lab.bcb.iastate.edu/sandbox/yves/map/sample.gif
Much obliged,
Yves Sucaet
Iowa State University
#32 by Mikko Koppanen on April 17, 2008 - 2:43 pm
Quote
yves,
seam carving does not work well if there is a lot of straight lines on plain background. you would need to refine the algorithm quite a bit.
#33 by izne on May 11, 2008 - 12:58 am
Quote
again…. is there option to create protected areas (like faces or whole people) ?
Pingback: Seam Carving and Imagick
Pingback: Ivo's Blog - jansch.nl » Blog Archive » php|tek 2008: day 1
#34 by Wayne on August 9, 2008 - 4:52 pm
Quote
Hi Mikko,
I would like to know if there is a mean to check if test.jpg is located at the good place on the server ?
$im = new Imagick( ‘test.jpg’ );
If the file don’t exist, this message will be displayed :
Fatal error: Uncaught exception ‘ImagickException’ with message ‘unable to open file `test.jpg’
And I would like to display a message like ‘The file don’t exist !’ rather than ‘Fatal error: Uncaught exception ‘ImagickException”
I know, I could use file_exist() to check that… but no… I would like to use the error returned by Imagick because I could check too if the file is really an image file or not.
Do you think it is possible ?
Thanks !
#35 by Mikko Koppanen on August 9, 2008 - 5:58 pm
Quote
Wayne,
Uncaught exception means that you’re not handling the possible exception. You should do something like this:
try {
$im = new imagick(“test.jpg”);
catch (ImagickException $e) {
echo “Unable to read file: ” , $e->getMessage();
}
#36 by Wayne on August 9, 2008 - 7:57 pm
Quote
Thanks Mikko, it is perfect
#37 by Liam on January 3, 2009 - 2:43 pm
Quote
Any updates on the license issue?
#38 by Mikko Koppanen on January 4, 2009 - 1:57 pm
Quote
The licensing issues have been solved
#39 by Samurai on January 27, 2009 - 12:17 am
Quote
Is there a way to get this to get liblqr for windows?
#40 by John.Sujith on July 29, 2009 - 2:49 pm
Quote
hi Mikko,
do you have any idea about red eye reduction through imagemagick ,if so please let me know ,expecting for your valuable comments thank you
best regards
sujith
#41 by Saiyine on August 3, 2009 - 2:35 pm
Quote
Thanks for the patch for liquid rescaling, Justin.
#42 by Saiyine on August 3, 2009 - 2:36 pm
Quote
Ooops, too quick: magick/resize.c:71:17: error: lqr.h: No such file or directory
Pingback: Imagick blog » Blog Archive » Seam carving
#43 by Marcin on June 17, 2010 - 6:24 pm
Quote
What a shame there is no way to use this on windows