[SOLVED] Cut along path, fill background

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?".
Locked
User avatar
zappzerapp
Posts: 9
Joined: 2018-08-08T07:48:55-07:00
Authentication code: 1152

[SOLVED] Cut along path, fill background

Post by zappzerapp »

Hi everybody!

I have a JPG image which contains a path and would like to cut along the path, fill the background with #ff6 and then multiply it with the original image to match the shadow.
I am pretty sure that such a thing is possible, but after 3 hours I still haven't found a working solution.

Original File: (http://img.buzzram.de/uploads/1581506712.jpg)

Input:Image Should: Image

Thanks a lot!

Greetings Robin :-)
Last edited by zappzerapp on 2020-02-16T05:49:38-07:00, edited 1 time in total.

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

Re: [HELP!] Cut along path, fill background

Post by fmw42 »

Unix syntax.

Code: Select all

First clip the image so that the outside is transparent.
Then pipe to another convert where the alpha channel is extracted and then negated.
Then use that as a mask to multiply the original image with a yellow image to make the output.

Code: Select all

convert 1581506712.jpg -alpha transparent -clip -alpha opaque -strip miff:- |\
convert 1581506712.jpg \
\( -clone 0 -fill "#ff6" -colorize 100 \) \
\( - -alpha extract -negate \) \
-compose multiply -composite \
1581506712_clip.png

Alternately without the pipe:

Code: Select all

convert 1581506712.jpg \
\( -clone 0 -alpha off -fill "#ff6" -colorize 100 \) \
\( -clone 0 -alpha transparent -clip -alpha opaque +clip -strip -alpha extract -alpha set -negate \) \
-compose multiply -composite \
1581506712_clip.png

Or one more approach:

Code: Select all

convert 1581506712.jpg \
\( +clone -alpha transparent -clip -alpha opaque +clip -strip \
-channel a -negate +channel \
-fill "#ff6" -colorize 100 \) \
-compose multiply -composite 1581506712_clip.png
See https://imagemagick.org/Usage/masking/#clip_mask

User avatar
GeeMack
Posts: 757
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: [HELP!] Cut along path, fill background

Post by GeeMack »

zappzerapp wrote:
2020-02-12T04:36:21-07:00
I have a JPG image which contains a path and would like to cut along the path, fill the background with #ff6 and then multiply it with the original image to match the shadow.
Edited – Thu Feb 13 01:37:57 UTC 2020 – I posted a workable solution here several hours ago, but have since come up with a much simpler and more versatile method.

Since the clipping path designates the subject of the image, and we want to isolate and modify the background, we have to more or less reverse the effect of the path. This command will produce the result in your example image...

Code: Select all

convert 1581506712.jpg -write mpr:in \( +clone -fill "#ff6" -colorize 100 \) \
   -compose multiply -composite mpr:in -clip -compose over -composite result.png
That starts by reading in the JPG image, then writes a copy of it to a memory register to save for later in the command. Then inside the parentheses it makes a clone and colorizes it entirely to "#FF6". After the parentheses it uses "-compose multiply" to composite that yellow image onto the original input.

So far so good, but multiplying that yellow tinting has also affected the car, which we don't want. So then we bring that copy "mpr:in" back into the command, and by setting the "-clip" and following with "-compose over -composite" it will composite only the inside of the clipping path, the car part of the input image, over the yellow tinted image we just created. Write the output file and it's done.

This command works for me in IM v6.8.9 in a bash shell, and by eliminating the backslashes "\" and changing that continued-line backslash "\" to a caret "^" it works in v6.9.10 at a Windows command prompt. Change "convert" to "magick" and it works in IMv7.

User avatar
zappzerapp
Posts: 9
Joined: 2018-08-08T07:48:55-07:00
Authentication code: 1152

Re: [SOLVED] Cut along path, fill background

Post by zappzerapp »

Thank you both!

I made a mistake in my example picture: it should actually be CMYK, which was why the colours were reversed in my attempts.

My command now looks like this and works as intended:

Code: Select all

convert 1581506712.jpg -profile ISOcoated_v2_300_eci.icc -profile sRGB.icm \( -clone 0 -alpha off -fill "#f5f5f5" -colorize 100 \) \( -clone 0 -alpha transparent -clip -alpha opaque +clip -strip -alpha extract -alpha set -negate \) -compose multiply -composite -profile ISOcoated_v2_300_eci.icc result.jpg

Locked