Gradient instead of solid fill color (draw)

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?".
sandwich
Posts: 7
Joined: 2019-11-09T12:31:46-07:00
Authentication code: 1152

Gradient instead of solid fill color (draw)

Post by sandwich » 2019-11-09T12:56:23-07:00

Code: Select all

convert in.pic -fill blue -draw "rectangle 100,100 300,200" out.pic
results, as expected, in a 200x100 pixel² blue rectangle within the picture (left top corner at position 100,100).
Using

Code: Select all

convert in.jpg -fill gradient:blue -draw "rectangle 100,100 300,200" out.jpg
does not give a rectangle with a blue gradient but simply a white rectangle.
How can a gradient be achieved?

[Version: ImageMagick 6.9.7-4 Q16 i686 20170114 ]

snibgo
Posts: 12436
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Gradient instead of solid fill color (draw)

Post by snibgo » 2019-11-09T13:30:51-07:00

"gradient:" starts a new image. The same is true of anything ending with a colon : such as "rose:" or "xc:".

You can create a new gradient image, and composite that over your input:

Code: Select all

convert toes.png -size 200x100 gradient:blue-white -geometry +50+50 -composite compgradbw.jpg
Image
snibgo's IM pages: im.snibgo.com

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

Re: Gradient instead of solid fill color (draw)

Post by fmw42 » 2019-11-09T13:32:43-07:00

There is not such -fill gradient: as far as I know. You cannot create or add a gradient from -draw in ImageMagick. You need to create a gradient image and overlay it on the background image. See gradient: as a pseudo image at https://imagemagick.org/script/formats.php#pseudo. Also see https://imagemagick.org/discourse-serve ... =4&t=28614

For -draw features see https://imagemagick.org/script/magick-v ... aphics.php and https://imagemagick.org/Usage/draw/

sandwich
Posts: 7
Joined: 2019-11-09T12:31:46-07:00
Authentication code: 1152

Re: Gradient instead of solid fill color (draw)

Post by sandwich » 2019-11-09T13:49:53-07:00

Perfect. Thanks!

Next step: additionally to the (radial and) opaque/transparent gradient I'd like to swirl this 100x100 px² area after composition:

Code: Select all

convert in.jpg -size 100x100 radial-gradient:black-none  -geometry +300+200 -composite out.jpg
Where to put the

Code: Select all

-swirl
?

snibgo
Posts: 12436
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Gradient instead of solid fill color (draw)

Post by snibgo » 2019-11-09T14:19:29-07:00

You want the swirl to apply only to the gradient image? I suppose you mean before composition. Then put swirl after the radial-gradient:, and enclose both in parentheses.
snibgo's IM pages: im.snibgo.com

sandwich
Posts: 7
Joined: 2019-11-09T12:31:46-07:00
Authentication code: 1152

Re: Gradient instead of solid fill color (draw)

Post by sandwich » 2019-11-09T15:04:56-07:00

If I apply the swirl only to the radial-gradient, it has no effect, because the swirled (radial gradient) image looks the same as the unswirled one.
So I want to swirl the area where the radial transparent/opaque image overlays the in.pic after composition, otherwise the swirl is not visible.

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

Re: Gradient instead of solid fill color (draw)

Post by fmw42 » 2019-11-09T15:16:00-07:00

Use parenthesis processing for each image. See https://imagemagick.org/Usage/basics/#parenthesis


input:
Image

Unix Syntax:

Code: Select all

convert \
\( mandril3.jpg -region 100x100+80+80 -swirl 360 +region \) \
\( -size 100x100 radial-gradient:black-none \) \
-geometry +80+80 -compose over -composite mandril3_swirl360.jpg
Windows Syntax:

Code: Select all

convert ^
( mandril3.jpg -region 100x100+80+80 -swirl 360 +region ) ^
( -size 100x100 radial-gradient:black-none ) ^
-geometry +80+80 -compose over -composite mandril3_swirl360.jpg
Image


Please always provide your platform (and IM version) when asking questions, since syntax may vary.

sandwich
Posts: 7
Joined: 2019-11-09T12:31:46-07:00
Authentication code: 1152

Re: Gradient instead of solid fill color (draw)

Post by sandwich » 2019-11-09T16:03:56-07:00

Works cool, thanks.

My platform is Linux/Ubuntu (version mentioned above: ImageMagick 6.9.7-4 Q16 i686 20170114)

Final question (at least I hope it is) : I do not want to use the whole pic.in but only a cropped and scaled part of it. When trying

Code: Select all

convert \
\( in.pic -crop 1000x1000+120+230 -scale 50% -region 100x100+33+77 -swirl 360 +region \) \
\( -size 100x100 radial-gradient:black-none \) \
-geometry +120+230 -compose over -composite out.pic
I get the error message

Code: Select all

convert-im6.q16: geometry does not contain image `in.pic' @ warning/transform.c/CropImage/666.
How can this all be done in one step?

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

Re: Gradient instead of solid fill color (draw)

Post by fmw42 » 2019-11-09T16:31:43-07:00

Not sure I understand. After scaling the input, will the background image still be larger than the gradient? The region will be processed on the scaled image. So the offsets and size will correspond.

Perhaps you should provide links to your in.pic so I can see what you are starting with. Is it larger than 120x1230 (that you crop)? The -geometry will also be relative to the scaled image, not the 1000x1000+120+230 that you crop. So you have to take into account the new size of the image after the crop and scale for your region and for the -geometry.

snibgo
Posts: 12436
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Gradient instead of solid fill color (draw)

Post by snibgo » 2019-11-09T16:47:40-07:00

sandwich wrote:warning/transform.c/CropImage/666.
The warning is from a function called "CropImage". So we can assume the problem is in the crop. I guess your image is too small to get a 1000x1000+120+230 crop, ie less than 1120x1230 pixels.
snibgo's IM pages: im.snibgo.com

sandwich
Posts: 7
Joined: 2019-11-09T12:31:46-07:00
Authentication code: 1152

Re: Gradient instead of solid fill color (draw)

Post by sandwich » 2019-11-09T16:49:39-07:00

Yes the in.pic is large enuogh, I want to extract (crop) a 1000x1000 part (top left corner at +120+230 of in.pic), then scale it to 500x500 px² and within this 500x500 image (position +33+77) I want to do the swirl and the composition with the gradient.
(The -geometry values should be +33+77 instead of +120+230, but this does not change the error message.)

sandwich
Posts: 7
Joined: 2019-11-09T12:31:46-07:00
Authentication code: 1152

Re: Gradient instead of solid fill color (draw)

Post by sandwich » 2019-11-09T17:12:46-07:00

Code: Select all

convert in.pic -crop 1000x1000+120+230 -scale 50% out.pic
works fine.

Code: Select all

convert in.pic -crop 1000x1000+120+230 -scale 50% -region 100x100+33+77 -swirl 360 +region out.pic
results in the above mentioned error.

snibgo
Posts: 12436
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Gradient instead of solid fill color (draw)

Post by snibgo » 2019-11-09T17:29:52-07:00

How large is in.pic? What does "identify in.pic" say?

Why haven't you added "+repage" after the crop?
snibgo's IM pages: im.snibgo.com

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

Re: Gradient instead of solid fill color (draw)

Post by fmw42 » 2019-11-09T17:40:08-07:00

Yes, snibgo is correct.

I forgot to mention that you likely need +repage after the crop so that it removes the virtual canvas. The virtual canvas is likely is confusing the -region

Does this work:

Code: Select all

convert in.pic -crop 1000x1000+120+230 +repage -scale 50% -region 100x100+33+77 -swirl 360 +region out.pic

sandwich
Posts: 7
Joined: 2019-11-09T12:31:46-07:00
Authentication code: 1152

Re: Gradient instead of solid fill color (draw)

Post by sandwich » 2019-11-09T17:51:35-07:00

snibgo wrote:
2019-11-09T17:29:52-07:00
Why haven't you added "+repage" after the crop?
Because I am not that familiar with ImageMagick/convert, but that did the trick!

By the way -compose over seems not to be necessary (same result with and without it).

So finally the command is:

Code: Select all

convert \
\( in.pic -crop 1000x1000+120+230 \
          +repage \
          -scale 50% \
          -region 100x100+33+77 \
                  -swirl 360 \
          +region \
\) \
\( -size 100x100 radial-gradient:black-none \) \
-geometry +33+77 -composite out.pic
Thank you very much everyone!

Post Reply