Page 1 of 1

Clip an object out of PSD Image (Background: white)

Posted: 2012-10-15T04:18:33-07:00
by Deathdragon
Hello,

I am trying to clip an object out of an Photoshop Image, expecting to have a white Background in the end result.

The PSD Images have one or more paths defined and I tried a few different Solutions.

What am I doing wrong?

System Specs:
Windows 7 Enterprise x64 (ImageMagick: 6.7.8-10 2012-08-10 Q16)

Tried Commands:

Code: Select all

convert 1.psd -flatten -clip 1.jpg
- Nothing Happened

Code: Select all

convert 1.psd -clip -flatten 2.jpg
- Black Background

Code: Select all

convert 1.psd -clip -background white -flatten 3.jpg
- Black Background Again

Code: Select all

convert 1.psd -clip -fill white -draw "color 0,0 reset" 4.jpg
- White Path

Code: Select all

convert 1.psd +clip -fill white -draw "color 0,0 reset" 5.jpg
- Full White Image

Source Image:
http://www.dragoniadev.com/download/1.psd

Result Images:
http://www.dragoniadev.com/download/1.jpg
http://www.dragoniadev.com/download/2.jpg
http://www.dragoniadev.com/download/3.jpg
http://www.dragoniadev.com/download/4.jpg
http://www.dragoniadev.com/download/5.jpg

Re: Clip an object out of PSD Image (Background: white)

Posted: 2012-10-15T11:19:53-07:00
by fmw42
Note several things.

1) -clip only processes the inside of the clip path.
2) -clip must come before the input ( or would appear to be the case from the documentation)
3) It requires the XML delegate library

see
http://www.imagemagick.org/script/comma ... s.php#clip

So to make this work, we have to use the clip path to make a mask. This may not be the most efficient way, but it works to make the outside completely white.

convert 1.psd \
\( -clone 0 -fill white -colorize 100 \) \
\( -clip -clone 0 -fill black -colorize 100 -fill white +opaque black \) \
-compose over -composite 1a.jpg

P.S.

Upon further testing, it would appear that -clip does not have to come before the input image. This also works:


convert 1.psd \
\( -clone 0 -fill white -colorize 100 \) \
\( -clone 0 -clip -fill black -colorize 100 -fill white +opaque black \) \
-compose over -composite 1b.jpg

Re: Clip an object out of PSD Image (Background: white)

Posted: 2012-10-15T11:51:47-07:00
by Bonzo
Interesting notes fmw42.

Re: Clip an object out of PSD Image (Background: white)

Posted: 2012-10-15T12:11:27-07:00
by fmw42
Bonzo wrote:Interesting notes fmw42.
How so? I knew nothing about -clip. I just read the docs and did some testing.

Fred

Re: Clip an object out of PSD Image (Background: white)

Posted: 2012-10-15T12:21:27-07:00
by Bonzo
I tried this test and it negated the inside although you say -clip should have come first. I have only just noticed you have added a P.S.

Code: Select all

convert 1.psd -clip -negate 1.jpg
I wondered how to modify the outside rather than the inside and thought there may have been an "invert" type option somewhere.

Re: Clip an object out of PSD Image (Background: white)

Posted: 2012-10-15T12:37:50-07:00
by fmw42
Bonzo wrote:I tried this test and it negated the inside although you say -clip should have come first. I have only just noticed you have added a P.S.

Code: Select all

convert 1.psd -clip -negate 1.jpg
I wondered how to modify the outside rather than the inside and thought there may have been an "invert" type option somewhere.

It seems that -clip and clip-path deal only with doing something inside the clip region. Though -clip-mask and -mask seem to preserve the inside and modify the outside.

As far as I can tell from testing, there is no +clip that might process the outside rather than the inside. But I would defer to Magick or Anthony for that.

Re: Clip an object out of PSD Image (Background: white)

Posted: 2012-10-15T12:45:29-07:00
by Bonzo
According to: http://www.imagemagick.org/Usage/masking/#clip
Note that the "+clip" operator negates the mask, it does not turn off and remove the clip mask. To turn it off you need to use "+clip_mask" instead.

Re: Clip an object out of PSD Image (Background: white)

Posted: 2012-10-15T14:18:50-07:00
by Deathdragon
fmw42 wrote:Note several things.

1) -clip only processes the inside of the clip path.
2) -clip must come before the input ( or would appear to be the case from the documentation)
3) It requires the XML delegate library
1) I know that and because of that I tried with +clip, which doesn't seem to work like I would expect: only selecting the area OUTSIDE the clipping-path and only enabling you to write to that area.
2) - According to your PS note, this is not really necessary. -
3) I didn't install any XML library, because I tried installing libxml2 on Windows which meant copying the *.dll files to a folder within the system path (e.g. C:/Windows) but it didn't change the behavior, so I undid it again. Also according to the documentation the -clip command would be ignored completely, if it can't read the SVG-path-data because of a missing XML Library, but since the results are differing whether I use -clip / +clip or not, it must mean that it can process the SVG-path-data.
Bonzo wrote:According to: http://www.imagemagick.org/Usage/masking/#clip
Note that the "+clip" operator negates the mask, it does not turn off and remove the clip mask. To turn it off you need to use "+clip_mask" instead.
I also found that quote, so my guess was, referring to a few other threads, that the last command I posted should do the trick.

This one:

Code: Select all

convert 1.psd +clip -fill white -draw "color 0,0 reset" 5.jpg
Threads, which are about this or similiar topics:
http://studio.imagemagick.org/discourse ... 90&p=36861
viewtopic.php?f=1&t=17874
http://www.wizards-toolkit.org/discours ... 89&start=0

Re: Clip an object out of PSD Image (Background: white)

Posted: 2012-10-15T14:39:46-07:00
by fmw42
Bonzo wrote:According to: http://www.imagemagick.org/Usage/masking/#clip
Note that the "+clip" operator negates the mask, it does not turn off and remove the clip mask. To turn it off you need to use "+clip_mask" instead.

Sorry I had not seen that post in Anthony's notes. But it was not there in the Options page. I had tried it, but was not successful.

If XML is not enabled then I doubt that either form of clip will do anything.

Re: Clip an object out of PSD Image (Background: white)

Posted: 2012-10-15T14:42:04-07:00
by fmw42
The discussion about extracting the clip path from the TIFF has to do with the 8bim version. I believe that -clip uses the actually clipping data in the verbose information in the form of xml tags rather than from the 8bim profile for which you would need to use -clip-path ID as per http://www.imagemagick.org/script/comma ... #clip-path. No ID was specified with -clip.

Presumably you can extract the clip path from the 8bim data and create your own svg file and then make a mask from it. Alternately, you can copy and paste the xml clip file into an svg file and use that I presume to create a mask file and then use the mask file to process your image.

I do not know if you need xml to process the svg file in IM. But you could download the RSVG delegate and process the svg file directly with that. I do not know if RSVG requires a dependency on XML.

Re: Clip an object out of PSD Image (Background: white)

Posted: 2012-10-15T15:03:16-07:00
by fmw42
IM 6.8.0.0 Q16 Mac OSX Snow Leopard

It is interesting that Anthony's example at http://www.imagemagick.org/Usage/masking/#clip does indeed produce an image with a transparent background around the furniture as per the following:

convert 1.psd -alpha transparent -clip -alpha opaque +strip 1test1.png

But try as I might, I cannot seem to be able to flatten it to a white background without extracting the mask first and reapplying it in a composite. That is this does not seem to work

convert 1.psd -alpha transparent -clip -alpha opaque -strip -background white -flatten 1test2.png (or .jpg)

Nor this

convert -respect-parenthesis \( 1.psd -alpha transparent -clip -alpha opaque -strip +clip-mask +mask \) \
-channel rgba -alpha on -background white -alpha background -compose over +geometry -flatten 1test2.png (or .jpg)


Both end up with a black background around the furniture. This would appear to be a bug to me. Possibly the mask is never disabled

P.S.
Though perhaps I misunderstand about the use of these functions. This seems to imply that the mask is never disabled as the following does work correctly.

convert 1.psd -alpha transparent -clip -alpha opaque miff:- | convert - -background white -flatten 1test8.png


I have posted a bug report at viewtopic.php?f=3&t=22074

Re: Clip an object out of PSD Image (Background: white)

Posted: 2012-10-16T02:54:05-07:00
by Deathdragon
Hello!

Thanks for your help so far.

But it's getting a little bit complicated by now. :)

So let me answer to one post after another:
The discussion about extracting the clip path from the TIFF has to do with the 8bim version. I believe that -clip uses the actually clipping data in the verbose information in the form of xml tags rather than from the 8bim profile for which you would need to use -clip-path ID as per http://www.imagemagick.org/script/comma ... #clip-path. No ID was specified with -clip.
These Photoshop Files also have 8BIM profiles and -clip uses the stored clip-paths from these profiles, according to the documentation the only difference between -clip and -clip-path #id is that, if there are more than one clip-path, you can select which one you want to use, where clip uses the first it finds, if I remember correctly.
Documentation of -clip:
-clip
Apply the clipping path if one is present.
If a clipping path is present, it is applied to subsequent operations.
Documentation of clip-path #id:
This is almost identical to -clip.
Documentation-Example of -clip:
For example, in the command
$ convert -clip -negate cockatoo.tif negated.tif
only the pixels within the clipping path are negated.
So my idea for only editing the area, which is not within the clip-path, is using +clip and my question is if that is a wrong assumption or if its probably not working because of the reported bug?!
Or does "+clip" only work if you did "-clip" before and just inverts correctly if you first selected the clipping-path with "-clip"?


This command:

Code: Select all

convert 1.psd +clip -fill white -draw "color 0,0 reset" 5.jpg
Though it seems with im4java (which I am using I won't even be able to use +clip, because it only enables you to add IMOperation options with "-option" not "+option", so I can't use "+clip" anyway.
I now also understood your last Command:
convert 1.psd -alpha transparent -clip -alpha opaque miff:- | convert - -background white -flatten 1test8.png
Which does:
1. Setting the whole Image to Transparent
2. Selecting the Pixels within the Clipping-Path
3. Setting the Pixels within the Clipping-Path to Opaque
- BREAK: Because we can't deactivate the mask (because of the reported bug, or in my case also because of the use of im4java which seems to not support "+option" commands)
1. Setting Background-Color of the Image White so that flatten will fill the transparent Pixels with a white BG-Color
2. Reducing the Layers to one and filling out the Background

Is my explanation correct?

Also I got a few other problems understanding your commands:
- What means "miff:- | convert - " ?
convert 1.psd -alpha transparent -clip -alpha opaque miff:- | convert - -background white -flatten 1test8.png
What does the Notation "\ \( .... \) \" mean?
convert 1.psd \
\( -clone 0 -fill white -colorize 100 \) \
\( -clone 0 -clip -fill black -colorize 100 -fill white +opaque black \) \
-compose over -composite 1b.jpg

Re: Clip an object out of PSD Image (Background: white)

Posted: 2012-10-16T10:49:53-07:00
by fmw42
Also I got a few other problems understanding your commands:
- What means "miff:- | convert - " ?
convert 1.psd -alpha transparent -clip -alpha opaque miff:- | convert - -background white -flatten 1test8.png
Miff is one of the IM internal file formats. image.miff if you want a name, otherwise, if temporary, just miff:-. The - means standard output, so that pipe can read it as input

What does the Notation "\ \( .... \) \" mean?
convert 1.psd \
\( -clone 0 -fill white -colorize 100 \) \
\( -clone 0 -clip -fill black -colorize 100 -fill white +opaque black \) \
-compose over -composite 1b.jpg

\( ... \) is unix syntax for multiple processing in the same command. The ending \ is a line continuation.

In windows you would use ( ... ) without the \ and the line ending \ would be ^

See
http://www.imagemagick.org/Usage/basics/#parenthesis
http://www.imagemagick.org/Usage/windows/

P.S., I have not resolved, yet, whether +clip is working properly. It is supposed to negate the mask, but in one test I made the whole image became white. I am waiting to hear about the other bug to be sure I understand how -clip works.

Re: Clip an object out of PSD Image (Background: white)

Posted: 2012-10-16T11:07:26-07:00
by fmw42
These Photoshop Files also have 8BIM profiles and -clip uses the stored clip-paths from these profiles
I thought that -clip took the clip path from the clipping path xml data and -clip-path ID took it from the 8bim profile. I did the following tests.

This of course works:

convert 1.psd -alpha transparent -clip -alpha opaque miff:- | convert - -background white -flatten 1test8.png

I then "tried" to remove the 8bim profle and this failed:

convert 1.psd +profile "8bim" -alpha transparent -clip -alpha opaque miff:- | convert - -background white -flatten 1test9.png
convert: no clip path defined `1.psd': No such file or directory @ error/image.c/ClipImagePath/716.


However I then looked at the verbose info after removing the profile and it removed the clipping path XML data, but left the 8bim profile. So I am certainly confused about this.

Before the profile removed:


Image: 1.psd

Code: Select all

  Clipping path: 
<?xml version="1.0" encoding="iso-8859-1"?>
<svg width="1772" height="831">
<g>
<path style="fill:#00000000;stroke:#00000000;stroke-width:0;stroke-antialiasing:false" d="
M 1508.33,704.833
C 1508.33,704.833 1460.5,705 1453.5,705
C 1446.5,705 1445,705.5 1441.5,706
C 1438,706.5 1436,704.5 1427.5,704.5
C 1419,704.5 1389,703.5 1382.5,703.5
C 1376,703.5 1362,703 1350.5,703
C 1339,703 1162,701.5 1124,702
C 1086,702.5 935.5,703 890,703
C 844.5,703 647.5,704.5 604,704.5
C 560.5,704.5 357.5,704 340.5,705.5
C 323.5,707 285.5,707 285.5,707
C 285.5,707 271,706.5 262.5,706
C 259.398,705.818 252.169,705.401 243.813,705.581
C 229.273,705.895 212.667,706.25 212.667,706.25
L 211.333,793.333
C 211.333,793.333 215.25,802.5 189,802.25
C 170.249,802.071 165.333,796 165.333,793.333
C 165.333,790.667 166.333,706.333 166.333,706.333
C 166.333,706.333 136.5,706.5 124.5,706.5
C 112.5,706.5 102,707.5 99.9999,708
C 97.9999,708.5 86,711 85,701
C 84.5506,696.506 83.2983,682.321 82.5155,665.022
C 81.5561,643.823 81.0506,617.947 80.5,599.5
C 79.5,566 78.5,492.5 77.4999,447
C 76.4999,401.5 75.4999,356.5 75.4999,342.5
C 75.4999,328.5 77.4999,325 85,314
C 92.4999,303 109,283 114.5,277.5
C 120,272 119,271 124.5,266.5
C 130,262 135.5,254.5 138.5,252.5
C 141.5,250.5 147,245 150,240.5
C 153,236 162,227.5 173,224.5
C 184,221.5 192.5,219.5 204,219.5
C 215.5,219.5 264,220 264,220
L 270.5,219.5
C 270.5,219.5 271.5,208.5 270,194
C 268.5,179.5 267.5,167 269.5,148.5
C 271.5,130 272.5,120.5 277.5,112.5
C 282.5,104.5 291,98.5 294.5,98
C 298,97.5 300.5,96.5 303,97
C 305.5,97.5 307.5,97.5 310.5,95.5
C 313.5,93.5 317,92.5 338,92
C 345.503,91.8213 363.536,91.398 384.508,91.5703
C 422.234,91.8803 469.467,93 482,93
C 501.5,93 568,93 594,94.5
C 620,96 707.5,98 745,98.5
C 782.5,99 822.5,99 836.5,101.5
C 850.5,104 859.5,107 862,116
C 864.5,125 865,136.5 865,136.5
C 865,136.5 868,133 868.5,128
C 869,123 872.5,114 878,110.5
C 883.5,107 888.5,104.5 898,104
C 907.5,103.5 1012.5,102.5 1032.5,102.5
C 1052.5,102.5 1115.5,99.5 1158.5,100
C 1201.5,100.5 1255,101.5 1294,101.5
C 1333,101.5 1361.5,105 1375.5,105
C 1389.5,105 1419,105.5 1427,108.5
C 1435,111.5 1442.5,117.5 1446,128.5
C 1449.5,139.5 1449.5,142.5 1450.5,148.5
C 1451.5,154.5 1452.5,159.5 1454,166
C 1455.5,172.5 1455.5,180.5 1454.5,186
C 1453.5,191.5 1454,203 1455,208.5
C 1456,214 1453.75,220 1453.75,220
L 1454,223
C 1454,223 1506,223 1516,224
C 1526,225 1537,228.5 1543,233
C 1549,237.5 1551.5,241 1557.5,244.5
C 1563.5,248 1572,255 1586,268
C 1600,281 1609.5,287 1613,290.5
C 1616.5,294 1623.5,304 1629.5,308.5
C 1635.5,313 1648,328 1649,335.5
C 1649.36,338.224 1650,333.541 1649.39,353.527
C 1648.33,388.577 1645.5,457.984 1645.5,479
C 1645.5,512 1643.5,549.5 1643,568.5
C 1642.5,587.5 1643,610 1642.5,634.5
C 1642,659 1639.5,698.5 1639.5,698.5
C 1639.5,698.5 1640,709.5 1629.5,708.5
C 1625.22,708.092 1616.29,707.268 1606.5,707.026
C 1592.27,706.675 1576.22,707 1570,707
C 1559.5,707 1555,706.25 1555,706.25
L 1553.25,795.25
C 1553.25,795.25 1548.75,800.25 1530,800.25
C 1511.25,800.25 1507.5,791.75 1507.5,791.75
"/>
</g>
</svg>

  Profiles:
    Profile-8bim: 36664 bytes
After removing the 8bim profile


convert 1.psd +profile "8bim" 1tmp.psd

Image: 1tmp.psd

Code: Select all

 Profiles:
    Profile-8bim: 600 bytes
I am not sure what 600 bytes of the 8bim originally 36664 bytes is left. But it seems to have removed both the XML data and most of the relevant 8bim profile.

Re: Clip an object out of PSD Image (Background: white)

Posted: 2012-10-23T02:35:43-07:00
by Deathdragon
When I compared the images created with the above commands I found differences in the colors in comparison with the original images and with manually edited images (using the clipping path in Photoshop to extract the object within the clipping.path).

Is it possible that the commands are changing the contrast / colors a little bit?

Example:
http://www.dragoniadev.com/download/x2.psd - unconverted PSD
http://www.dragoniadev.com/download/x2.jpg - converted JPG

The Image was converted using this command pipeline:
convert 1.psd -alpha transparent -clip alpha opaque temp
convert temp -background white -flatten -resize 1000 -strip result.jpg