Help with displacement map + Perl syntax

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
Ryland

Help with displacement map + Perl syntax

Post by Ryland »

I'm working on an application that will make an image of a soft drink can, and I want to distort the graphics to simulate 3D perspective (rounded top and bottom). I've got a displacement filter that works in Photoshop, but not in ImageMagick, and I suspect it's because I don't know the correct PerlMagick syntax. It should produce a relatively can-shaped distortion, but for some reason it's skewing on the X-axis:

Image

I know there's a way to define how much to distort on each axis on the command line, and I'm pretty sure I should be setting the X axis to zero, but I can't figure out what the Perl syntax is. Can someone give me an example? Thanks in advance.
Ryland

Re: Help with displacement map + Perl syntax

Post by Ryland »

Oh, I forgot: this is the Perl code I'm using.

Code: Select all

use Image::Magick;

$checker = Image::Magick->new;
$checker->Set(size=>"200x350");
$checker->Read("checker.png");

$dmap = Image::Magick->new;
$dmap->Set(size=>"200x350");
$dmap->Read("displacementmap.png");

$base = Image::Magick->new;
$base->Set(size=>"200x350");
$base->Read("xc:#FFFFFF");

$check->Composite(image=>$dmap,compose=>'Displace',gravity=>'Center');

$base->Composite(image=>$checker,compose=>'Over',gravity=>'Center');

$base->Write("png:checker_distort.png");

undef $base;
undef $checker;
undef $dmap;
Ryland

Re: Help with displacement map + Perl syntax

Post by Ryland »

Sorry, I just realized there's a whole other forum dedicated to PerlMagick, I'll take this over there.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Help with displacement map + Perl syntax

Post by anthony »

I would be interested to know how you generated that displacement map :-)
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Ryland

Re: Help with displacement map + Perl syntax

Post by Ryland »

anthony wrote:I would be interested to know how you generated that displacement map :-)
I made it in Photoshop, but it wouldn't be difficult to do in ImageMagick. First I made a reflected horizontal gradient, because I wanted more displacement toward the vertical center of the image than at the right and left edges. Then I made a normal vertical gradient, and inverted the first gradient using the second gradient as a mask, so that the displacement would go up at the top of the image and down at the bottom.

Image
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Help with displacement map + Perl syntax

Post by anthony »

Very tricky...
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Help with displacement map + Perl syntax

Post by anthony »

IM solution (command line)...

Code: Select all

   convert rose: -fx 'cos(pi*(i/w-.5))' -evaluate multiply .5 \
           \( +clone -negate \) \
           \( -size 70x46 gradient: -flip \) -composite \
           displace_cylinder.jpg

   composite  displace_cylinder.jpg  rose: \
              -background black -virtual-pixel background \
              -displace 0x10  cylindrical rose:
Remember grey = no displacement, not white!

Also as the displacement values represents the 'destination' offset, rather than the source offset, (see http://www.imagemagick.org/Usage/compose/#displace ) the 'cosine()' function in the above is distorted by the linear inversion. Making the curve become rather straight toward the edges.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Ryland

Re: Help with displacement map + Perl syntax

Post by Ryland »

It may be worthwhile to point out that only a specific region of the displacement map is going to get used, corresponding to the label area of the can.

Image

Only the blue region will get distorted. (Actually, a separate graphic the size of the blue region will get distorted, and then composited onto the base can image.)

As I preview this, I realize it would be a better idea to invert the displacement map so that the left and right edges are distorted toward the vertical center, instead of the vertical center being curved up and down - downsampling the edges instead of upsampling the center.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Help with displacement map + Perl syntax

Post by anthony »

I agree. and that is what my revised map did. It made the center line grey, meaning no change.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply