Generate a gradient from a image?

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
Lassar
Posts: 2
Joined: 2014-10-31T15:15:24-07:00
Authentication code: 6789

Generate a gradient from a image?

Post by Lassar »

I would like make a gradient from a color image, and apply it to another black and white image.

How would I get the gradient from the color image?

An example is to get a gradient from a photo of clouds at sunset, and apply it another photo of black and white clouds.

How would you do this with ImageMagick?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Generate a gradient from a image?

Post by fmw42 »

A perfect solution is not possible, because many colors may map to the same gray intensity. There is no way to know which color to use.

My best suggestion is to correlate a few colors from locations in the colored image with shades of gray in your grayscale image. Then build a clut image (what you call the gradient) and apply it using -clut.

For example:

Here is a color image:
Image

Here is a corresponding grayscale image:
Image

My command, where I have selected just a few colors (unix syntax):

Code: Select all

convert gray_image.jpg \
\( xc:black xc:blue xc:green1 xc:red xc:white +append -resize 256x1! \) \
-clut gray_image_colored.jpg
(windows syntax, I think)

Code: Select all

convert gray_image.jpg ^
( xc:black xc:blue xc:green1 xc:red xc:white +append -resize 256x1^! ) ^
-clut gray_image_colored.jpg
Resulting image:
Image
Post Reply