Clip path problem when saving TIFF as a transparent PNG

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?".
cva
Posts: 13
Joined: 2014-05-18T18:35:13-07:00
Authentication code: 6789

Re: Clip path problem when saving TIFF as a transparent PNG

Post by cva »

Thanks for the further tip. I'll start playing with Inkscape now & see if I can get it working - but hopefully snibgo will be able to share his knowledge in this area.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Clip path problem when saving TIFF as a transparent PNG

Post by fmw42 »

It is interesting that I can access each of the PS paths by using

Code: Select all

identify -format '%[8BIM:1999,2998:#1]' watch.tif > clip1.svg

identify -format '%[8BIM:1999,2998:#2]' watch.tif > clip2.svg

etc.
But the path that is exported for 1 does not seem to match perfectly to the clip path as seen in PS, when convert to png by

Code: Select all

convert clip1.svg -negate clip1.png
Perhaps the code for the SVG is malformed by RSVG standards, or PS has special handling code or PS uses some other path stored elsewhere.

Perhaps Inkscape can handle it better.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Clip path problem when saving TIFF as a transparent PNG

Post by snibgo »

Two major points.

First, the quote.

Code: Select all

identify -format '%[8BIM:1999,2998:#1]' watch.tif > clip1.svg
Unix uses a single-quote character to delineate strings. Windows uses a double-quote character for this purpose.

In Windows, this will output a single quote character, then the SVG, then another quote character. Instead, one of the following should be used:

Code: Select all

identify -format %[8BIM:1999,2998:#1] watch.tif > clip1.svg
identify -format "%[8BIM:1999,2998:#1]" watch.tif > clip1.svg
As a general rule, if Unix/Mac has a single-quote, Windows should have a double-quote.


Second point: looking at "identify -verbose watch.tif", the SVG is bad. It contains the same Y and V problems as the recent thread viewtopic.php?f=3&t=25565&p=111061#p111061 . If your SVG was created by Photoshop, I suggest you report this to Adobe. In fact, report it to whoever created the software.

I don't have Photoshop, so can't experiment to try to reproduce the creation of bad SVG. Inkscape has never created bad SVG, for me.

The only solutions I can see are:

(a) A solution from whoever wrote the software that created the bad SVG. They should provide a fix that converts bad files into good ones.

(b) A script could be written to drive Gimp in batch mode, to re-create the SVG. This will remove the invalid "Y" commands. However, the SVG "V" commands are valid but with wrong data. Photoshop (or whatever) has written bad data then reads it badly, "correcting" the problem. Gimp will read it properly, creating a path that doesn't go where you want it.

(c) Someone might reverse-engineer the bug that creates the bad SVG, and write something that does the same as Photoshop does when it reads the bad SVG. Maybe someone has done this already. If may be worth a web search.
snibgo's IM pages: im.snibgo.com
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Clip path problem when saving TIFF as a transparent PNG

Post by snibgo »

My proposed solution (c) looked difficult because I don't know what Photoshop is doing with "V" and "Y" commands. So let's pretend that these should be treated as "L" commands. We can write a Windows BAT script to re-write the SVG.

Code: Select all

setlocal enabledelayedexpansion

identify -format %%[8BIM:1999,2998:#1] watch.tif > clipBad.svg

del clipGood.svg

for /F "tokens=1,*" %%A in (clipBad.svg) do (
  set COMMAND=%%A
  if "!COMMAND!"=="Y" set COMMAND=L
  if "!COMMAND!"=="V" set COMMAND=L
  echo !COMMAND! %%B>>clipGood.svg
)

convert watch.tif ( clipGood.svg ) -alpha off -compose copy_opacity -composite watch4.png
This works for watch.tif, and the result looks good to me.

For some files, you might need to "-negate" the SVG, ie:

Code: Select all

convert watch.tif ( clipGood.svg -negate ) -alpha off -compose copy_opacity -composite watch4.png
I've tried this on a bad file in the other thread, and that result also seems good.

I suggest you try this technique on a few of your troublesome files.

EDIT: added "setlocal enabledelayedexpansion" at the top of the script.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Clip path problem when saving TIFF as a transparent PNG

Post by fmw42 »

Thanks snibgo. I had a feeling you would know what to do with the SVG file. Editing and replacing Y and V with L seems to work fine for me, too.

Code: Select all

convert watch.tif \
-profile /Users/fred/images/Profiles/USWebCoatedSWOP.icc \
-profile /Users/fred/images/Profiles/sRGB.icc \
\( clip_mod.svg -negate \) -alpha off -compose copy_opacity -composite watch_mod.png
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Clip path problem when saving TIFF as a transparent PNG

Post by snibgo »

Good stuff, fmw. For the benefit of Unix/Mac users, could you supply bash commands that convert "Y" and "V" commands to "L"? My script just looks for those letters at the start of line, followed by at least one space.

Then we can refer any other users with the same problem to this thread.

With luck, Adobe or whoever is responsible will fix the problem.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Clip path problem when saving TIFF as a transparent PNG

Post by fmw42 »

I just did a global search and replace in my text editor with case sensitive and entire word. (using BBEdit)

However, the equivalent unix command using sed would be (searching for V or Y at the beginning with one or more spaces afterwards

Code: Select all

cat clip.svg | sed 's/^[VY] [ ]*\(.*\)$/L \1/g' > clip2.svg
However, this only works if the SVG file has one command per line. I have had some files that are all compressed into one long line, that is, with no line feed at the end of a segment. To make that work, you would have to just search and replace on the V and Y (followed by a space) irrespective of the position in the file, such as with

Code: Select all

cat clip.svg | sed 's/[VY] /L /g' > clip2.svg
cva
Posts: 13
Joined: 2014-05-18T18:35:13-07:00
Authentication code: 6789

Re: Clip path problem when saving TIFF as a transparent PNG

Post by cva »

Many thanks snibgo & fmw42!!! I tried you solution and it works well for ~6 or so files I have tried so far. I'll run it over a good test set of TIFF files and see if the results are consistent. Either way, you have steered me to a solution that I can continue to tweak based on the results of processing the TIFF file set that I have.

As for the author / cause of the bad SVG, I am unsure who is to blame since I have been given the TIFF file set to prepare. I discuss this with the owner of the images to try to determine what software was used to generate the bad SVG in the first place and report the issue so it can be fixed.

Thanks again for your great help! Much appreciated!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Clip path problem when saving TIFF as a transparent PNG

Post by snibgo »

Good stuff. I should add that maybe the "Y" and "V" commands should be converted to "S" instead of "L". Or maybe one should be "S" and the other should be "L". I haven't had time to test this yet, and I don't have a good set of test files. Please update us on any successes (or failures)!
snibgo's IM pages: im.snibgo.com
cva
Posts: 13
Joined: 2014-05-18T18:35:13-07:00
Authentication code: 6789

Re: Clip path problem when saving TIFF as a transparent PNG

Post by cva »

Thanks for the heads-up regarding the "Y" and "V" commands. I'll watch out for that. Back to testing more TIFF files...
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Clip path problem when saving TIFF as a transparent PNG

Post by fmw42 »

Let us know if you find out what tool created these tiff files. It will be helpful to others to know if it is truly from Photoshop or some other tool.
cva
Posts: 13
Joined: 2014-05-18T18:35:13-07:00
Authentication code: 6789

Re: Clip path problem when saving TIFF as a transparent PNG

Post by cva »

Will do, fmw42.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Clip path problem when saving TIFF as a transparent PNG

Post by snibgo »

I've experimented with a number of "bad" files dating back to May 2010, trying four settings:
1. Convert both Y and V to L.
2. Convert Y to S, and V to L.
3. Convert Y to L, and V to S.
4. Convert both Y and V to S.

I don't have images from Photoshop, so I can't tell which (if any) is correct. I can only look at the generated outlines and judge which best fits the photograph.

The best generally seems to be option 3, "Convert Y to L, and V to S".
snibgo's IM pages: im.snibgo.com
cva
Posts: 13
Joined: 2014-05-18T18:35:13-07:00
Authentication code: 6789

Re: Clip path problem when saving TIFF as a transparent PNG

Post by cva »

Many thanks for this information, snibgo. I'll try option 3 and see if I can see problems with the outlined area.
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: Clip path problem when saving TIFF as a transparent PNG

Post by dlemstra »

I just submitted a patch to our SVN repository to fix the SVG that is created. It should create the proper outline in the next release. V has been changed to Q and Y has been changed to S. Can you both run some tests and see if there is a problem with the outlines? I have placed the new output here: https://www.dropbox.com/sh/nm80nplm8obn ... Fcu27cXOga.
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
Post Reply