How to convert a transparent PNG to black & white?

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
etrader
Posts: 21
Joined: 2012-09-29T06:53:04-07:00
Authentication code: 67789

How to convert a transparent PNG to black & white?

Post by etrader »

I want to convert a transparent PNG to a black silhouette. This means the transparent part should remain transparent (or white), the non-transparent part should be transformed into pure black by losing all textures.

I tried -threshold 0%, but it retains some texture, as it turns the non-transparent to white.

snibgo
Posts: 13034
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to convert a transparent PNG to black & white?

Post by snibgo »

What version of IM, on what platform? Please provide a sample input.

What does "by losing all textures" mean?
snibgo's IM pages: im.snibgo.com

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

Re: How to convert a transparent PNG to black & white?

Post by fmw42 »

Post an example input and a mockup of your desired output. What version of ImageMagick and what platform/OS are you using?

Try (IM 6)

Code: Select all

convert transparent_image.png -fill black +opaque none result.jpg
If on IM 7, replace convert with magick.

etrader
Posts: 21
Joined: 2012-09-29T06:53:04-07:00
Authentication code: 67789

Re: How to convert a transparent PNG to black & white?

Post by etrader »

snibgo wrote:
2020-05-02T13:42:42-07:00
What version of IM, on what platform? Please provide a sample input.

What does "by losing all textures" mean?
Sorry for not providing my system information.
Version: ImageMagick 7.0.8-34 Q16 x86_64 2019-03-18
Ubuntu 18.04

I want to turn the non-transparent part of PNG to pitch black with no texture (not grey).

It is not about a specific input. Take any transparent PNG for example.
fmw42 wrote:
2020-05-02T13:48:35-07:00
Post an example input and a mockup of your desired output. What version of ImageMagick and what platform/OS are you using?

Try (IM 6)

Code: Select all

convert transparent_image.png -fill black +opaque none result.jpg
If on IM 7, replace convert with magick.
It did not work. It turns the whole picture to black (both transparent and non-transparent part).

By the way, what is the different/advantage of magick? I always use convert as an old habit.

snibgo
Posts: 13034
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to convert a transparent PNG to black & white?

Post by snibgo »

Code: Select all

magick in.png -channel RGB -fill Black -colorize 100 +channel out.png
"magick" uses v7 syntax. "convert" uses v6 syntax.

V7 is better than v6.
snibgo's IM pages: im.snibgo.com

etrader
Posts: 21
Joined: 2012-09-29T06:53:04-07:00
Authentication code: 67789

Re: How to convert a transparent PNG to black & white?

Post by etrader »

snibgo wrote:
2020-05-02T14:27:08-07:00

Code: Select all

magick in.png -channel RGB -fill Black -colorize 100 +channel out.png
it works beautifully.

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

Re: How to convert a transparent PNG to black & white?

Post by fmw42 »

This works fine for me on IM 7.0.10.10 Q16 Mac OSX

# create transparent image:

Code: Select all

magick logo: -transparent white logot.png
# process it:

Code: Select all

magick logot.png -fill black +opaque none logot_result.png

Locked