Printing / saving a plot as a png file with an alpha channel issue in Octave

前端 未结 2 1941
失恋的感觉
失恋的感觉 2021-01-27 00:36

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

2条回答
  •  误落风尘
    2021-01-27 01:00

    (update: also see Sancho's nice answer here: Have a transparent background in a plot exported from Octave )


    In general, adding transparency in octave is still not fully supported. My advice would be to produce your images as normal, and use an external tool to add transparency (which you could call from within octave via the system function, if you'd like it to be part of a script).

    The imagemagick suite can do what you want via the convert command, e.g.

    convert  myplot.png  -fuzz 50%  -transparent white  myplot_transparent.png
    

    (taken from here)

    If you'd like to produce many 'layers', some of which have transparency, and then overlay them (which presumably is what you want the transparency for in the first place), you can also do this via imagemagick via:

    convert  bottomlayer.png  toplayer.png  -compose over  -composite out.png
    

    So a complete example in octave might look something like this:

    t = [0:0.01:2*pi];
    
    % Create bottom layer (no transparency needed)
    plot (t, sin(t), 'r', 'linewidth', 3);
    set(gcf, 'Position',  [10, 10, 500, 500]);
    axis([-1, 7, -1, 1]);
    set(gca, 'Position',  [0.1, 0.1, 0.8, 0.8]);
    saveas(gcf, 'bottom.png');
    
    % Create top layer, and make transparent (via imagemagick)
    plot (t, cos(t), 'g', 'linewidth', 3);
    set(gcf, 'Position',  [10, 10, 500, 500]);
    axis([-1, 7, -1, 1]);
    axis off;
    set(gca, 'Position',  [0.1, 0.1, 0.8, 0.8]);
    saveas(gcf, 'top.png');
    system('convert top.png  -fuzz 10% -transparent white top.png');
    
    % Combine layers
    system('convert bottom.png top.png -compose over -composite result.png');
    
    %Visualise result in octave
    imshow result.png
    

    Resulting image:

提交回复
热议问题