Page 1 of 1
How do I use ImageMagick to warp image on a baseball cap
Posted: 2019-07-20T21:55:37-07:00
by direction
How to warp a image on a cap? Below is the result I want. Have no idea, I am new to ImageMagicK.
Any thought is appreciate.
Input images:
output images:
Re: How do I use ImageMagick to warp image on a baseball cap
Posted: 2019-07-20T22:39:09-07:00
by fmw42
Imagemagick does not have that kind of warping capability. It is a 2D tool and you need a 3D texture mapping system.
I suggest you try Photoshop 'puppet' warp.
Re: How do I use ImageMagick to warp image on a baseball cap
Posted: 2019-07-21T00:39:48-07:00
by direction
fmw42 wrote: ↑2019-07-20T22:39:09-07:00
Imagemagick does not have that kind of warping capability. It is a 2D tool and you need a 3D texture mapping system.
I suggest you try Photoshop 'puppet' warp.
fmw42, thanks!
However, trying to process it on the backend server. So I cannot use Photoshop.
Just found this link:
https://imagemagick.org/Usage/distorts/#polynomial
Not sure it works or not?
Re: How do I use ImageMagick to warp image on a baseball cap
Posted: 2019-07-21T03:14:41-07:00
by snibgo
Please don't multi-post. I have removed your other post.
You want to distort an image and paint that on your object. If you can do this once with any software on any computer, you can repeat the task using just ImageMagick.
The trick is to do it once using a special image called an "identity displacement map". This creates a distorted map. Then your server uses IM to distort any user-supplied image with this distorted map.
I show how to do this when the object is a 3D model:
Painting people. This also accounts for 3D lighting of the object, which may be overkill for your requirement.
Re: How do I use ImageMagick to warp image on a baseball cap
Posted: 2019-07-21T12:18:37-07:00
by direction
snibgo wrote: ↑2019-07-21T03:14:41-07:00
Please don't multi-post. I have removed your other post.
You want to distort an image and paint that on your object. If you can do this once with any software on any computer, you can repeat the task using just ImageMagick.
The trick is to do it once using a special image called an "identity displacement map". This creates a distorted map. Then your server uses IM to distort any user-supplied image with this distorted map.
I show how to do this when the object is a 3D model:
Painting people. This also accounts for 3D lighting of the object, which may be overkill for your requirement.
Snibgo,
I was trying to create a distorted map for the cap. I can imagine what the displacement map for the baseball cap should look like, but I found no way to create it using some software or coding.(I checked some displacement maps on
http://www.imagemagick.org/Usage/mappin ... on_unified).
Is it there any way to create a "identity displacement map " for a certain object like baseball cat, mug,...
Re: How do I use ImageMagick to warp image on a baseball cap
Posted: 2019-07-21T13:01:31-07:00
by snibgo
direction wrote:Is it there any way to create a "identity displacement map " for a certain object like baseball cat, mug,...
The identity displacement map represents zero distortion. You then distort that using any software you want onto any 3D object you want (mug, baseball cap, mug, tee-shirt, or whatever). Then you use IM and the distorted map to distort user-supplied images to the same 3D object.
If you have no software that can distort an image to the object, then you might be able to do it mathematically. That is easy for simple objects like cylindrical mugs, but needs more work for baseball caps.
Re: How do I use ImageMagick to warp image on a baseball cap
Posted: 2019-07-21T13:40:35-07:00
by fmw42
snibgo, would you say a bit more about how you modify or use the absolute displacement map with whichever you use of -compose distort or -compose displace, since they are more relative displacements. Do you just subtract the identity from the warped identity map to make it relative for use with -compose displace?
Re: How do I use ImageMagick to warp image on a baseball cap
Posted: 2019-07-21T15:26:21-07:00
by fmw42
snibgo, also were there quantization issues from either a 16-bit identity map (for images of sizes about 1600x1600) and with Q16 Imagemagick? Or did you have to use HDRI to get good results?
Re: How do I use ImageMagick to warp image on a baseball cap
Posted: 2019-07-21T15:43:10-07:00
by snibgo
The technique works with absolute, not relative, spatial displacement maps.
We start with the identity absolute displacement map. We then distort that using Photoshop or whatever gives the result we need. The result is a distorted absolute displacement map, and this can be used with IM to distort any image in the same way, with "-compose Distort -composite".
This process for spatial distortion is very similar to colour distortion, where we use a hald image, distort its colour in Photoshop or similar, then use the distorted hald to apply the same colour distortion to any other images with IM and "-hald-clut".
Quantization: human eyes are far more sensitive to spatial distortion than to colour distortion, so quantization causes more obvious problems. I recommend image calculation and storage are at least 32-bit floats. In v6, this means using Q32 HDRI. For v7, perhaps (untested) Q16 HDRI is sufficient provided we save maps with "-define quantum:format=floating-point -depth 32".
Re: How do I use ImageMagick to warp image on a baseball cap
Posted: 2019-07-21T16:50:29-07:00
by fmw42
OK thanks.
-compose distort is like an absolute displacement with zero distortion as mid gray in the center of the image, as I recall. So that would mean the gradient from black to white in X and Y would be the identity displacement map.
Re: How do I use ImageMagick to warp image on a baseball cap
Posted: 2019-07-21T17:25:35-07:00
by snibgo
Yes.
In absolute displacement maps, 50% at any pixel means "move the pixel that was at 50% of the width or height to this location". And 0% at any pixel means "move the pixel that was at the top or left to this location".
An identity absolute displacement map has #000 at top left, #f00 top right, #0f0 bottom left, and #ff0 bottom right (but the blue channel doesn't matter).
An identity relative displacement map is 50% in the red and green channels in every pixel. So if we distort that map using Photoshop or whatever the result is still a 50% gray all over, and this doesn't help us.
Re: How do I use ImageMagick to warp image on a baseball cap
Posted: 2019-07-21T17:58:44-07:00
by fmw42
See
https://imagemagick.org/script/compose.php
This reads a bit confusing. It says center relative, which would mean zero distortion at the center. But then it implies that the distortion is gray at the middle Am I misreading it?
distort Arguments: X-scale[xY-scale[+X-center+Y-center]][!][%]
Not available in "composite" at this time.
Exactly as per 'Displace' (above), but
using absolute coordinates, relative to the center of the overlay (or that given). Basically allows you to generate absolute distortion maps where 'black' will look up the left/top edge, and 'white' looks up the bottom/right edge of the destination image, according to the scale given.
The '!' flag not only switches percentage scaling, to use the destination image, but also the image the center offset of the lookup. This means the overlay can lookup a completely different region of the destination image.
Re: How do I use ImageMagick to warp image on a baseball cap
Posted: 2019-07-22T05:00:57-07:00
by snibgo
It isn't easy to summarise absolute displacement maps in a couple of sentences. Above, I ignored the possibility of using arguments and a mask, and of different-size images.
Consider just the map's red channel, with values from 0 to 100% of QuantumRange. If there are no arguments, any percentage value means the colour for that position is taken from that percentage of the width of the input image. If scale and offset are used, they modify the look-up calculation so maps values of 0 and 100% come from some other points along the width.
I think the "!" flag is relevant only when the input image and map are different sizes.
Re: How do I use ImageMagick to warp image on a baseball cap
Posted: 2019-07-22T07:52:27-07:00
by direction
snibgo wrote: ↑2019-07-21T13:01:31-07:00
direction wrote:Is it there any way to create a "identity displacement map " for a certain object like baseball cat, mug,...
The identity displacement map represents zero distortion. You then distort that using any software you want onto any 3D object you want (mug, baseball cap, mug, tee-shirt, or whatever). Then you use IM and the distorted map to distort user-supplied images to the same 3D object.
snibgo,
I am trying ‘displacement mapping’ today with a simple example.
input mesh:
displacement map:
expected output:
what I got: out.png
My code:
Code: Select all
convert input-mesh.jpg Displacement.jpg -fx 'p{v*w,j}' \
out.png
I am not sure my distortion code is correct or not?
The code is from imagemagick document, See:
https://imagemagick.org/Usage/mapping/#displace
Re: How do I use ImageMagick to warp image on a baseball cap
Posted: 2019-07-22T09:54:55-07:00
by snibgo
Your "expected output" has highlights and shadows. You won't get that from a simple displacement. Anthony's example does that with sphere_overlay.png and sphere_mask.png.
I wouldn't use "-fx" because it is too slow for ordinary photos that probably have millions of pixels.