Mirroring images

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
janilrau

Mirroring images

Post by janilrau »

I have to add additional image information to the four borders of the original image. This consists the mirrored part of the adjacent border. This is needed to create a canvas print, mounted on a 4 centimeter frame without cropping the original image:

You can find an example here: http://www.artido.de/gfx/fotoleinwand/k ... ellung.jpg (see image above "gespiegelt")

Thank you
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Mirroring images

Post by Bonzo »

Done in php, you could probably shorten the code and adapt it if not using php.

Code: Select all

<?php
// Create a filler image
$size = getimagesize('64.jpg');
exec("convert -size {$size[0]}x{$size[1]} xc:white filler.png");

// Mirror the image 4 ways
exec("convert 64.jpg +flip temp1.jpg");
exec("convert 64.jpg -flip temp2.jpg");
exec("convert 64.jpg +flop temp3.jpg");
exec("convert 64.jpg -flop temp4.jpg");

// Make the rows
exec("convert filler.png temp3.jpg filler.png -append output1.png");
exec("convert temp2.jpg 64.jpg temp1.jpg -append output2.png");
exec("convert filler.png temp4.jpg filler.png -append output3.png");

// Put it all together
exec("convert output1.png output2.png output3.png +append output4.png");

// crop sizes
$width = ( $size[0] * 1.25 );
$height = ( $size[1] * 1.25 );

// crop the image
exec("convert output4.png -gravity center -crop {$width}x{$height}+0+0 output.png");

// Delete the tempory images
unlink("temp1.jpg");
unlink("temp2.jpg");
unlink("temp3.jpg");
unlink("temp4.jpg");
unlink("output1.png");
unlink("output2.png");
unlink("output3.png");
unlink("output4.png");
?>
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Mirroring images

Post by fmw42 »

You can do the following to unfold the edges (mirrored) by say 12 pixels

pad=12
convert inputimage \( +clone -bordercolor white -border ${pad}x${pad} \) +swap -virtual-pixel mirror -fx "v.p{i-$pad,j-$pad}" outputimage

The bordercolor is immaterial, it just increases the size of the resulting image and is covered by the unfolded areas. The fx operator processes the second image to fill the size of the first image, so the +swap puts them in the right order. The order was switched so that I could use +clone as it needs to have the first image defined before it.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Mirroring images

Post by anthony »

Taking this a little further you can do other things too.

For example you can overlay the original image on a resized lighter version of the same image...

Code: Select all

  convert rose: \
     \( -clone 0 -resize 140% -fill white -colorize 30% \) \
     \( -clone 0 -bordercolor black -border 1x1 \) \
     -delete 0 -gravity center -composite  rose_self_bordered.gif
You can adjust the border size to be a specific size instead of a percentage, also adjust the amount of white (colorize percentage), or even it you dimm it with white ;-)

Alternatively instead of dimming the frame with white, you can use -frame. The value of the resize is the original image size +20 pixels

Code: Select all

  convert rose: \
     \( -clone 0 -resize 90x66\! \) \
     \( -clone 0 -mattecolor '#8886' -frame 10x10+3+3 \) \
     -delete 0  -composite  rose_self_framed.gif
Note that in both cases a underlay and overlay is generated from the
same image, which is then combine to produce a border with colors
coming from the original image.

I think I may add these to the IM Examples thumbnail page under a new section titled 'self framing'. When I get time to do it is another matter.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
janilrau

Re: Mirroring images

Post by janilrau »

Thanks a lot, I made it this way, but a bit smarter, since your routine needs lots of memory!

I just flip/flop and crop the four needed sides. Then I reassemble the final image.

Best regards
Karsten
karsten dot jursch at fleiheit dot com
Bonzo wrote:Done in php, you could probably shorten the code and adapt it if not using php.

Code: Select all

<?php
// Create a filler image
$size = getimagesize('64.jpg');
exec("convert -size {$size[0]}x{$size[1]} xc:white filler.png");

// Mirror the image 4 ways
exec("convert 64.jpg +flip temp1.jpg");
exec("convert 64.jpg -flip temp2.jpg");
exec("convert 64.jpg +flop temp3.jpg");
exec("convert 64.jpg -flop temp4.jpg");

// Make the rows
exec("convert filler.png temp3.jpg filler.png -append output1.png");
exec("convert temp2.jpg 64.jpg temp1.jpg -append output2.png");
exec("convert filler.png temp4.jpg filler.png -append output3.png");

// Put it all together
exec("convert output1.png output2.png output3.png +append output4.png");

// crop sizes
$width = ( $size[0] * 1.25 );
$height = ( $size[1] * 1.25 );

// crop the image
exec("convert output4.png -gravity center -crop {$width}x{$height}+0+0 output.png");

// Delete the tempory images
unlink("temp1.jpg");
unlink("temp2.jpg");
unlink("temp3.jpg");
unlink("temp4.jpg");
unlink("output1.png");
unlink("output2.png");
unlink("output3.png");
unlink("output4.png");
?>
Post Reply