How do I use ImageMagick to warp image on a baseball cap

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
direction
Posts: 16
Joined: 2019-07-20T21:21:37-07:00
Authentication code: 1152

How do I use ImageMagick to warp image on a baseball cap

Post 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:
Image
Image

output images:
Image
Image
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How do I use ImageMagick to warp image on a baseball cap

Post 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.
direction
Posts: 16
Joined: 2019-07-20T21:21:37-07:00
Authentication code: 1152

Re: How do I use ImageMagick to warp image on a baseball cap

Post 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?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How do I use ImageMagick to warp image on a baseball cap

Post 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.
snibgo's IM pages: im.snibgo.com
direction
Posts: 16
Joined: 2019-07-20T21:21:37-07:00
Authentication code: 1152

Re: How do I use ImageMagick to warp image on a baseball cap

Post 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,...
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How do I use ImageMagick to warp image on a baseball cap

Post 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.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How do I use ImageMagick to warp image on a baseball cap

Post 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?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How do I use ImageMagick to warp image on a baseball cap

Post 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?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How do I use ImageMagick to warp image on a baseball cap

Post 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".
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How do I use ImageMagick to warp image on a baseball cap

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How do I use ImageMagick to warp image on a baseball cap

Post 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.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How do I use ImageMagick to warp image on a baseball cap

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How do I use ImageMagick to warp image on a baseball cap

Post 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.
snibgo's IM pages: im.snibgo.com
direction
Posts: 16
Joined: 2019-07-20T21:21:37-07:00
Authentication code: 1152

Re: How do I use ImageMagick to warp image on a baseball cap

Post 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:
Image
displacement map:
Image
expected output:
Image
what I got: out.png
Image
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
Image
Image
Image
Image
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How do I use ImageMagick to warp image on a baseball cap

Post 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.
snibgo's IM pages: im.snibgo.com
Post Reply