Skip to content

Displacement Maps for Easy Image Transformations with Cloudinary

With Cloudinary’s transformation options, you as web or mobile developers can transform images in real time through dynamic URLs, from optimization and responsive delivery to other tasks, complete with cool effects. For example, a robust transformation applies another image to displace your base image with a mapping algorithm that evaluates the base image’s pixels, displacing them according to the intensity of the corresponding image’s map pixels.

Specifically, the algorithm appraises the map image’s red and green channels: the more intense the red color in the displacement map, the more the horizontal displacement of the corresponding pixel in the base image. Similarly, the more intense the green color in the displacement map, the larger the vertical displacement of the corresponding pixel in the base image.

The following example demonstrates a base image displaced by an image map, creating a zoom effect:

displacement map for a zoom effect

The next example uses a displacement map to warp the perspective of an overlay image for final placement as an overlay on another image:

displacement map for image overlay

Using this overlay transformation for placement on a mug:

Zoom displacement map

The displacement effect is controlled by a few configurable parameters that allow you to fine tune the desired result. The dynamic URL for the final image includes:

  • The public_id of the base image.
  • The public_id of the image map to use as a displacement template, added using the overlay parameter (l in URLs).
  • The effect parameter set to displace (e_displace in URLs), which tells Cloudinary to use the image map as a displacement effect rather than adding it as an overlay.
  • The x and y parameters to configure the maximum allowed displacement in the horizontal and vertical directions (negative values for left and up respectively, 0 for no displacement).

The final displacement of each pixel in the base image is determined by a combination of the red and green color channels, together with the configured x and y parameters. For example, for a given x value of 20 and a pixel with a red value of 255 (highest), the corresponding base image pixel would be displaced by 20 pixels horizontally, while a red value of 25 (10%) would only displace the base image pixel by 2 pixels horizontally.

The following example demonstrates the zoom displacement map shown above (along with an overlay of a magnifying glass) to “magnify” the base image.

zoomed in image

Any image can be used as a displacement map, even if it hasn’t been specifically created for that purpose. Most of the time, using a photograph as a map will give some surreal effects, but if you use a photograph with a (semi) repeating pattern, you can actually end up with some nice new texture effects. The following interactive example allows you to combine a base picture with various image “textures”:

1  Select the strength of the distortion effect:    
2  Pick an image to use as a distortion effect
water pebbles concrete
3  Press the button!

Another more practical case for using the displacement effect is to make an overlay appear more realistic when placing it over an object with its own texture, making the overlay seem like its actually part of that object itself. This can be very useful for generating customized eCommerce images.

This can be achieved by using the base image itself as a displacement map for the overlay. For example, below we’ve added an overlay to a sweater, using the sweater itself as a displacement map for the overlay:

Adapting overlay to a surface

Displacement maps can be a useful addition to your arsenal of image transformations. They allow you to customize your images in interesting ways and add realistic effects. This capability is already being used by our customers to displace images overlays, and as shown in this post, can also be used in multiple, perhaps unexpected ways. The feature is available for use with all Cloudinary accounts, including the free tier.

Back to top

Featured Post