Displacement map

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Acrossfy
Posts: 9
Joined: 2011-03-02T09:46:22-07:00
Authentication code: 8675308

Displacement map

Post by Acrossfy »

Hello, could you tell me how to make something like this – viewtopic.php?f=1&t=16921#p62696 using imagick?
Thank you in advance)
Acrossfy
Posts: 9
Joined: 2011-03-02T09:46:22-07:00
Authentication code: 8675308

Re: Displacement map

Post by Acrossfy »

My example:

Code: Select all

	$displace->adaptiveResizeImage( 100, null );
	$image->adaptiveResizeImage( 100, null );

 	$image->compositeImage( $displace, Imagick::COMPOSITE_DISPLACE, 0, 0 );

        $shirt->compositeImage( $image, Imagick::COMPOSITE_OVER, 188, 200 );
It works - Image. But image don`t transform like this - Image.
Sorry for my bad english.
Acrossfy
Posts: 9
Joined: 2011-03-02T09:46:22-07:00
Authentication code: 8675308

Re: Displacement map

Post by Acrossfy »

I'm interested in this particular part of code, how to implement it in imagick syntax?

Code: Select all

convert image.jpg displace.png -alpha set -virtual-pixel transparent -compose displace -set option:compose:args -5x-5 -composite result.png
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Displacement map

Post by fmw42 »

I don't really know Imagick that well, but try

$image->compositeImage( $displace, Imagick::COMPOSITE_DISPLACE, -5, -5 );
Acrossfy
Posts: 9
Joined: 2011-03-02T09:46:22-07:00
Authentication code: 8675308

Re: Displacement map

Post by Acrossfy »

These are the other options, they are responsible for the positioning..
Image
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Displacement map

Post by fmw42 »

But image don`t transform like this
You need to create and use the grayscale displacement map as well as the image. see viewtopic.php?f=1&t=16921#p62696
Acrossfy
Posts: 9
Joined: 2011-03-02T09:46:22-07:00
Authentication code: 8675308

Re: Displacement map

Post by Acrossfy »

I use exactly a grayscale displacement map.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Displacement map

Post by fmw42 »

$image->compositeImage( $displace, Imagick::COMPOSITE_DISPLACE, 0, 0 );

You have not specified the displacement image, nor the correct displacement values of -5, -5


see http://us3.php.net/manual/en/function.i ... eimage.php

bool Imagick::compositeImage ( Imagick $composite_object , int $composite , int $x , int $y [, int $channel = Imagick::CHANNEL_ALL ] )

Also your x, y values should be -5, -5 not 0, 0

And I don't know what your $displace is? It does not look like the grayscale image from my example on the earlier link
$displace->adaptiveResizeImage( 100, null );
Acrossfy
Posts: 9
Joined: 2011-03-02T09:46:22-07:00
Authentication code: 8675308

Re: Displacement map

Post by Acrossfy »

Code:

Code: Select all

<?php
$image = new imagick( "image.jpg" );
$image->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_TRANSPARENT);
$image->setImageBackgroundColor( new ImagickPixel( "none" ) );
$image->setImageFormat("png");
$displace = new imagick( "displace.png" );
$displace->setImageBackgroundColor( new ImagickPixel( "none" ) );   
$displace->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_TRANSPARENT);
$displace->setImageBackgroundColor( new ImagickPixel( "none" ) );

$image->compositeImage( $displace, Imagick::COMPOSITE_DISPLACE, -5, -5 );

header( "Content-Type: image/png" );
echo $image;
?>
Attachments
Result
Result
result.png (97.76 KiB) Viewed 38442 times
Displace
Displace
displace.png (52.03 KiB) Viewed 38443 times
Image
Image
image.jpg (34.72 KiB) Viewed 38443 times
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Displacement map

Post by fmw42 »

In my example, the original image that was blurred to form the displacement image came from a section of the Tshirt and not the image that was to be distorted. The Tshirt folds and shading provided the basis for blurring to get the kind of distortion needed. You need some near white and near black regions near the edges to make them push in or out.

Try applying -auto-level to your displacement image. then try increasing or decreasing the value -5, -5 and/or reversing the signs.

Best advice. Take my images and values and see if you can reproduce the effect in iMagick.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Displacement map

Post by fmw42 »

You also missed the point that you have to distort the image with the displacement map AND then composite the result over the background image. You have only distorted the image. You have not placed it over the background image. See all my steps at the original link.
Acrossfy
Posts: 9
Joined: 2011-03-02T09:46:22-07:00
Authentication code: 8675308

Re: Displacement map

Post by Acrossfy »

Code: Select all

<?php
$shirt = new imagick( "shirt.jpg" );
$image = new imagick( "image.png" );
$displace = new imagick( "displace.png" );

$image->compositeImage( $displace, Imagick::COMPOSITE_DISPLACE, 14, -10 );

$shirt->compositeImage( $image, Imagick::COMPOSITE_OVER, 60, 80);

header( "Content-Type: image/jpg" );
echo $shirt;
?>
Result:
Image

Images "image.png" and "displace.png" have a padding:
Image
This suggests that the images need to increase the viewport, such as here. But I do not know how to do it with Imagick.
It completely removes the background. But displace looks ugly, regardless of the values of x and y.
Also I do not know how to do autolevel for displavement image. This is not in the documentation.

Sorry for my bad Englsh again)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Displacement map

Post by fmw42 »

$image->compositeImage( $displace, Imagick::COMPOSITE_DISPLACE, 14, -10 );
try much smaller numbers (both negative)

$image->compositeImage( $displace, Imagick::COMPOSITE_DISPLACE, -5, -5 );

or

$image->compositeImage( $displace, Imagick::COMPOSITE_DISPLACE, -1, -1 );

or even smaller values if needed.

Where did you get the displacement image (from my example)?
Acrossfy
Posts: 9
Joined: 2011-03-02T09:46:22-07:00
Authentication code: 8675308

Re: Displacement map

Post by Acrossfy »

You don`t understand, I have many times tried different values, including those that you recommended. This does not affect the results.
I've used your displacement map, but I added padding in the Photoshop - it completely removes the background.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Displacement map

Post by fmw42 »

why are you padding the displacement map? try using all my images and see if you can reproduce my results before trying your image. The displacement map needs to be the same size as the image. So you need to resize your image to the size of the displacement map.

What version of IM and what version of iMagick are you using?

Can you test in command line mode? If not try doing it in PHP with the exec function to see if you can reproduce my results. Then try to get the equivalent iMagick commands to work.
Post Reply