Images imwrite PNG with Alpha transparency

296 views
Skip to first unread message

Yakir Gagnon

unread,
Jun 24, 2014, 1:00:44 AM6/24/14
to julia...@googlegroups.com
How, if possible, can I imwrite a PNG image that has transparency in it?
I'm assuming something like:

using Images
imwrite( I,"name.png",alpha=A)

Thanks!

Yakir Gagnon

unread,
Jun 24, 2014, 1:14:29 AM6/24/14
to julia...@googlegroups.com
Just as an alternative, one cold post process the imae with imagemagick. The trick is to code the image with the colormap so that the transparent part will be white and:

run(`convert name.png -transparent white name.png`)

Yakir Gagnon

unread,
Jun 24, 2014, 1:32:43 AM6/24/14
to julia...@googlegroups.com
Right, this isn't ideal because the transparency is discretely expressed as either white or nothing. I'd rather have a gradient of opacity (just like any other color, 256 levels?). 

Tim Holy

unread,
Jun 24, 2014, 7:30:00 AM6/24/14
to julia...@googlegroups.com
If your image has transparency, then imwrite(I, "name.png") should Just Work.

There are many ways to get an image with transparency: for example, you might
load one from disk. If you want to construct one "from scratch," here's a
demo:

A = rand(100, 128, 4); # random colors
A[:,:,1] = linspace(0,1,100) .* ones(1,128); # alpha gradient
img = colorim(A, "ARGB")

This builds an ARGB image. You could alternatively choose an RGBA image if you
prefer (for which A is the 4th channel).

--Tim

Yakir Gagnon

unread,
Jun 24, 2014, 8:13:20 AM6/24/14
to julia...@googlegroups.com
Great! I didn't know it was that simple, but if I have an indexed image I created with ImageCmap, how would I first convert it to a direct image so that I'll be able to concatenate the alpha layer?

Tim Holy

unread,
Jun 24, 2014, 1:10:28 PM6/24/14
to julia...@googlegroups.com
convert(Image, img)

where img is your ImageCmap

--Tim

Yakir Gagnon

unread,
Jun 24, 2014, 11:14:08 PM6/24/14
to julia...@googlegroups.com
Thank you so much!
Reply all
Reply to author
Forward
0 new messages