How can I print / save a plot as a png file with an alpha channel?
I tried Saving a plot in Octave with transparent background
I\'m using Octave 4.2.2, Ubu
It is not clear if you want to simply generate a file with a transparent background, or you want to handle the more general case of an arbitrary alpha channel for your image file.
Fortunately, it seems Octave can handle the second case as well. As pointed out here, transparency is not implemented for image or axes objects, but writing images to file with an alpha channel seems to work fine, at least in version 5.1.0.
So this would produce a transparent alpha channel for white (tcolor = [255 255 255]
)
im = print(gcf, '-RGBImage');
tcolor = [255 255 255];
alpha(:,:) = 255 * ( 1 - (im(:,:,1) == tcolor(1)) .* (im(:,:,2) == tcolor(2)) .* (im(:,:,3) == tcolor(3)) );
imwrite(im, 'myplot.png', 'Alpha', alpha);