Problems creating a png with text and 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
jagku
Posts: 8
Joined: 2013-07-12T04:08:03-07:00
Authentication code: 6789

Problems creating a png with text and transparent background

Post by jagku »

Hello,

I have just started using imagemagick as I need to create very simple png's which contain text (certain font/size/colour - possibly bold/italic/underlined if the font supports it) on a transparent background.

I have tried doing this but when I lay out the PNG on to a coloured background, it just doesn't look very sharp due to the curves around the "e" and "o".

Code: Select all

convert  -density 90 -pointsize 72 -font Gill-Sans-MT -fill black -stroke black -transparent white -colorspace CMYK label:Hello test.png
(I have also tried adding +antialias before the "-font" - but the background becomes white (instead of remaining transparent).

Can anybody see where I am going wrong?

Thanks in advance!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Problems creating a png with text and transparent backgr

Post by snibgo »

"-transparent White" doesn't do what you probably think it does. (It's in the wrong position, and it turns only pixels that are entirely white to transparent. Pixels at the edge of the letters are various shades of gray.) Try "-background None" instead.

"-colorspace CMYK" does nothing here. (And it should be placed after "label:", not before.) If you want a CMYK file, save it as a tiff or jpg.
snibgo's IM pages: im.snibgo.com
jagku
Posts: 8
Joined: 2013-07-12T04:08:03-07:00
Authentication code: 6789

Re: Problems creating a png with text and transparent backgr

Post by jagku »

Thanks alot - ah so the flags have to be in a certain order.
Problem solved!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Problems creating a png with text and transparent backgr

Post by snibgo »

It's always best to put them in logical order. The image isn't created until "label:", so anything that changes the images, such as "-transparent", should come after that. Anything that affects how the image is created, such as "-background", should come before "label:".

However, for reasons lost in the mists of time, putting "-transparent" before "label:" does actually work. But the curved letters have edges that blend with the background, so it doesn't do what you want.

Png files can't store CMYK. So your command tries to create an image, convert it to CMYK, then back to sRGB to store in the file.
snibgo's IM pages: im.snibgo.com
jagku
Posts: 8
Joined: 2013-07-12T04:08:03-07:00
Authentication code: 6789

Re: Problems creating a png with text and transparent backgr

Post by jagku »

Thanks for the explanation.

Yes, PNG files certainly can't be saved as CMYK - I think I was running with my eyes shut!

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

Re: Problems creating a png with text and transparent backgr

Post by anthony »

You may like to look at the examples on converting Postscript pages.
http://www.imagemagick.org/Usage/text/#postscript

Basically a good way of making white transparent, is you use the text image as a transparency mask, such that white is made fully transparent.

Code: Select all

convert text.png  -negate -background black -alpha shape  text_on_transparency.png
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply