PNG trim to clipping path with transparent 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?".
Post Reply
stevef66
Posts: 11
Joined: 2014-11-11T04:46:07-07:00
Authentication code: 6789

PNG trim to clipping path with transparent background

Post by stevef66 »

Hello

I'm trying to create a PNG with a transparent background from a jpeg, which must be trimmed to the area of the active clipping path.

The command below works fine in 'ImageMagick 6.7.8-9 2016-06-03 Q16', but with 'ImageMagick 7.0.7-3 Q16 x86_64 2017-09-17' I just get a 1x1 pixel file:

/usr/bin/convert -units PixelsPerInch test_file_1.jpg -filter lanczos -density 200 +clip-path "#1" -fuzz %1 +transparent black -trim +repage PNG:crop_test_file_1.png

Hope someone can help?

Thanks


Link to sample file is:
https://www.dropbox.com/sh/51z3ghlp8pcg ... RhkMa?dl=0

Image Magick Version is:
ImageMagick 7.0.7-3 Q16 x86_64 2017-09-17

Linux Version is:
Red Hat Enterprise Linux Server release 7.4 (Maipo)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: PNG trim to clipping path with transparent background

Post by snibgo »

Your command, on your file, works fine for me with IM v6.9.5-3 and v7.0.3-5 (using "magick"). I get the shirt surrounded by a transparent background.

For v7, I suggest you use "magick", not "convert".

If that doesn't help, simplify your command by removing operations to find out where it is going wrong.
snibgo's IM pages: im.snibgo.com
stevef66
Posts: 11
Joined: 2014-11-11T04:46:07-07:00
Authentication code: 6789

Re: PNG trim to clipping path with transparent background

Post by stevef66 »

Thanks for your response.

I forgot to post the error I'm getting before, but here the same message using 'magick' instead of convert:
magick: geometry does not contain image `test_file_1.jpg' @ warning/attribute.c/GetImageBoundingBox/240.

From looking into this before I did this post, this message is caused by the trim option according to the command line option documentation, so it looks like either the '+transparent black' operation or the +clip-path "#1" operation isn't working .
Changing the colour value from 'black' to 'white' stops the error message appearing, but doesn't produce the final result I want, which is if the colour isn't black outside the clipping path make it transparent.

From my understanding of the documentation, this should be working, but isn't.

Can you try using the same version of Image Magick I'm using?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: PNG trim to clipping path with transparent background

Post by snibgo »

Sorry, I don't have any later versions.

An alternative to simplifying your command is: insert "+write XX.png" operations between each of your operations, like this:


-units PixelsPerInch test_file_1.jpg -filter lanczos -density 200 +clip-path "#1" +write x0.png -fuzz %1 +transparent black +write x1.png -trim +write x2.png +repage PNG:crop_test_file_1.png
snibgo's IM pages: im.snibgo.com
stevef66
Posts: 11
Joined: 2014-11-11T04:46:07-07:00
Authentication code: 6789

Re: PNG trim to clipping path with transparent background

Post by stevef66 »

Thanks that's helped.

I've run that with version 6.7.8-9 and 7.0.7-3.

With 6.7.8-9 the x1.png image is correct, the t-shirt surrounded by a transparent background before it gets trimmed, however with version 7.0.7-3 the x1.png image is completely transparent with no sign of the t-shirt.

Is this therefore a bug?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PNG trim to clipping path with transparent background

Post by fmw42 »

try using PNG32: rather than PNG for your output if you want it to have transparency
stevef66
Posts: 11
Joined: 2014-11-11T04:46:07-07:00
Authentication code: 6789

Re: PNG trim to clipping path with transparent background

Post by stevef66 »

Thanks for the suggestion, but that didn't make any difference.

The transparency is being created. It's just that the original image inside the clipping path isn't retained, so I've just got a completely transparent image with no content.
stevef66
Posts: 11
Joined: 2014-11-11T04:46:07-07:00
Authentication code: 6789

Re: PNG trim to clipping path with transparent background

Post by stevef66 »

Hi

Can someone tell me if to get this reported as a bug do I need to repost this in the Bugs forum?

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

Re: PNG trim to clipping path with transparent background

Post by GeeMack »

stevef66 wrote: 2017-09-22T06:16:55-07:00The transparency is being created. It's just that the original image inside the clipping path isn't retained, so I've just got a completely transparent image with no content.
ImageMagick v7 handles alpha operations differently than IMv6. Using IM 7.0.7-3 in Windows 10 I can get the result you want by running this command...

Code: Select all

magick test_file_1.jpg -alpha set -background none ^
   -clip-path "#1" -fuzz 1% -transparent black -trim crop_test_file_1.png
That sets the alpha channel active and sets the background color as "none". Also notice it uses "-clip-path ..." instead of "+clip-path ...", which more or less reverses the result of the transparency. Try it both ways to see the result.

To run it in a *nix shell you'd have to change the continued line caret "^" to a backslash "\" or just run it all on one line.
stevef66
Posts: 11
Joined: 2014-11-11T04:46:07-07:00
Authentication code: 6789

Re: PNG trim to clipping path with transparent background

Post by stevef66 »

Thanks GeeMack, that's very helpful.

The original image works fine now as you say. However I've tried it with a second image and although the background is made transparent, the trim option isn't being applied. I've tried adding +repage after the -trim, but that hasn't made any difference.
Do you have any idea why that would be happening? I've added a test_file_2 image to the drop box link I shared.

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

Re: PNG trim to clipping path with transparent background

Post by fmw42 »

This command works fine for me on IM 7.0.7.3 Q16 Mac OSX

Code: Select all

magick test_file_1.jpg -alpha transparent -clip -alpha opaque +clip -strip -trim +repage test_file_1_clip.png

magick test_file_2.jpg -alpha transparent -clip -alpha opaque +clip -strip -trim +repage test_file_2_clip.png
stevef66
Posts: 11
Joined: 2014-11-11T04:46:07-07:00
Authentication code: 6789

Re: PNG trim to clipping path with transparent background

Post by stevef66 »

Thank you

That works perfectly.

Much appreciated
Post Reply