Change background by applying clipping path

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
sven
Posts: 27
Joined: 2003-10-02T02:21:38-07:00

Change background by applying clipping path

Post by sven »

Hi magick,

I would like to change the background of an image by applying its clipping path. That is to say that the current image has a white background and a clipping path which describes a part of this image.
The resulting image should be the same image containing the image part with the clipping part on a blue background.

I've tried many combinations of "-clip", "-clip-path id" etc., but nothing seems to work.

Any help is appreciated!

Thanks a lot!
Sven
masterbp
Posts: 27
Joined: 2007-10-08T06:23:33-07:00

Re: Change background by applying clipping path

Post by masterbp »

Hi Sven

I has a similar problem and solved it using something like:

Code: Select all

+clip-path #1 -fill blue -draw "color 0,0 reset"
I am no expert in ImageMagick, so this is only a qualified guess:)
masterbp
Posts: 27
Joined: 2007-10-08T06:23:33-07:00

Re: Change background by applying clipping path

Post by masterbp »

Hi

I also have a related question which I hope anyone can help me with:

I want to give a picture a white background by using the clipping path.

Code: Select all

"c:\Program Files\ImageMagick-6.3.8-Q16\convert.exe" +clip-path #1 -fill white -draw "color 0,0 reset" 18.tif 18.jpg
This works great if i'm using a RGB image or converting from CMYK to RGB using color profiles, but in this case i only want to make the background white on a CMYK image - nothing else. Using the above command on a CMYK image gives me a black background instead of white

Any help is appreciated.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Change background by applying clipping path

Post by anthony »

The -draw operation and most colors handling is done in RGB space.

However I have found that many other operations also do handle things properly, but not with enough certainty to give a bug report.

For example I have found that using -opaque to replace a particular color with the current -fill color on a CMYK image often results in the color being applied as if, both color specifications were being mis-read as a CMYK color, when the color was given as a RGB color.

For example using the IM examples "colorwheel.png" image, works fine for RGB images.

Code: Select all

 convert colorwheel.png -fuzz 50% -fill red -opaque red replace_red.png
But goes bad when the image is converted to either CMY or CMYK in memory.

Code: Select all

convert colorwheel.png -colorspace CMY -fuzz 50% -fill red -opaque red -colorspace RGB replace_red_2.png
The result looks to be as if the given RGB 'red' color was being looked at as a CMYK cyan (which has the same memory bit pattern). Clearly a bug.

Actually viewing a CMY colorspace image on screen using the special "x:" output format is displays incorrectly!

However back you your problem...

I would first try to switch to using -opaque with a -fuzz factor of more than 100% (all colors, masked by clipping path) to -draw. But as you saw that will also fail, which should be reported as BUG for CMYK images.

Sorry.

Alternative...

The only other solution I can see is to use the clipping path to create a transparency mask. This could then be colorized to white, converted to CMYK and overlaid on the original image, using alpha composition.

There is always more than one way to do anything in image processing.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
masterbp
Posts: 27
Joined: 2007-10-08T06:23:33-07:00

Re: Change background by applying clipping path

Post by masterbp »

Hello anthony

Thank you for your feedback. Could you please give me an example of your alternative solution. I'm not sure i understand how this is done.

Thanks again!
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Change background by applying clipping path

Post by anthony »

I do not have a image with a clipping path!! Making it difficult to test and debug the solution.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Brian P

Re: Change background by applying clipping path

Post by Brian P »

For some reason, the suggested solution:

Code: Select all

convert +clip-path #1 -fill blue -draw "color 0,0 reset" in.jpg out.jpg
only works for me on a windows machine - when I try the same command on Linux (have tried with several different versions of ImageMagick) I get all sorts of errors. Earlier versions won't accept it as a valid command, while later versions create a file named +clip-path.

What am I doing wrong?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Change background by applying clipping path

Post by anthony »

Start a new topic
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply