gnuplot: How to display transparent png with pixmap

86 views Asked by At

I am trying to place images (png) on top of a map using the set pixmap command.

pixmap displayed on Map

It does work but the transparency of the png is not getting "displayed", and I get a white background instead.

Can the pixmap feature handle the transparency property of an image?

Here is the script:

$DATA<<EOD
1,"Washington",630.2000,1591.1800
EOD
$DATA01<<EOD
1 "C:\\Temp\\Images\\colors.png" 630.2000 1591.1800
EOD
set datafile separator comma
set style data point
set term windows enhanced size 3000.00,2000.00
unset xtics; unset ytics; unset x2tics; unset y2tics
set grid noxtics
set grid noytics
set border 0
set colorsequence classic
set key off
set for [i=1:1] pixmap i word($DATA01[i], 2) center at word($DATA01[i], 3), word($DATA01[i], 4) width 100 height 100 
plot 'C:\Magic\XPA\46s\Add_On\ChartGenerator\Geography\USA_Curved_Names_Blue.png.jpg' binary filetype=JPG w rgbimage,\
$DATA every ::0::0 using ($3):($4) notitle,\
$DATA using ($3):($4):($2) with labels font "Tahoma,20" textcolor rgb "purple" center offset 0,1 notitle

here is the png file: sample png file

and the map file: enter image description here

Roberto

2

There are 2 answers

4
Ethan On BEST ANSWER

I do not know what is going on here exactly, but it has something to do with the image file itself. If I open that file in GIMP and export it to a new file with no intervening edit/modify operations, then the exported file works correctly as a pixmap with transparent areas.

Comparing the output of identify -verbose original.png and identify -verbose redo.png doesn't show anything that stands out to me except possibly the png:IHDR.color_type entries.

I attach the re-exported file here - try it in place of your original image file and see if that works. enter image description here

For what it's worth, here is the diff between identify run on the two files:

diff -urp original.identify redo.identify 
--- original.identify   2023-10-25 22:38:42.002322272 -0700
+++ redo.identify       2023-10-25 22:38:57.165582322 -0700
@@ -1,5 +1,5 @@
 Image:
-  Filename: original.png
+  Filename: redo.png
   Permissions: rw-r--r--
   Format: PNG (Portable Network Graphics)
   Mime type: image/png
@@ -703,25 +703,26 @@ Image:
   Compression: Zip
   Orientation: Undefined
   Properties:
-    date:create: 2023-10-26T02:58:38+00:00
-    date:modify: 2023-10-26T02:58:34+00:00
-    date:timestamp: 2023-10-26T06:23:13+00:00
+    date:create: 2023-10-26T06:22:24+00:00
+    date:modify: 2023-10-26T06:22:24+00:00
+    date:timestamp: 2023-10-26T06:23:23+00:00
+    png:bKGD: chunk was found (see Background color, above)
     png:IHDR.bit-depth-orig: 8
     png:IHDR.bit_depth: 8
-    png:IHDR.color-type-orig: 2
-    png:IHDR.color_type: 2 (Truecolor)
+    png:IHDR.color-type-orig: 6
+    png:IHDR.color_type: 6 (RGBA)
     png:IHDR.interlace_method: 0 (Not interlaced)
     png:IHDR.width,height: 512, 512
     png:pHYs: x_res=3780, y_res=3780, units=1
-    png:tRNS: chunk was found
+    png:tIME: 2023-10-26T06:22:24Z
     signature: cb4667f54b948708bd7fd893e4668ca843bf816392a4bad8c8917b967cf1f1ea
   Artifacts:
     verbose: true
   Tainted: False
-  Filesize: 11772B
+  Filesize: 11446B
   Number pixels: 262144
   Pixel cache type: Memory
-  Pixels per second: 21.2189MP
+  Pixels per second: 21.0638MP
   User time: 0.010u
   Elapsed time: 0:01.012
   Version: ImageMagick 7.1.1-11 Q16-HDRI x86_64 21206 https://imagemagick.org

If you spot anything relevant there, let me know. If it's a reproducible file property that can be tested for, maybe gnuplot can be taught to fix it or at least emit a warning.

1
theozh On

As Ethan already mentioned, there is something "strange" about the transparency of OP's original PNG, which apparently GIMP and Inkscape know how to handle, but which gnuplot is interpreting as white.

The easiest solution is probably to modify the original PNG (e.g. import/export in GIMP or Inkscape) and use it as pixmap in gnuplot.

The following is not really a good solution, but demonstrating that in principle you can achieve transparency with OP's original PNG file (as is) by using the plotting style with rgbalpha. However with the following drawbacks:

  • all white pixels in OP's PNG are converted to full transparency.
  • some intentional white pixels will be converted to full transparency as well
  • there is no partial transparency
  • scaling/aspect ratio of the original image (512x512 px) apparently depends on the graph x,y ranges. So, you have to fix the ranges, autoscale will mess up your graph. Furthermore, you need to fiddle around with the scaling factors.

Script:

### pixmap/rgbalpha transparency
reset session

FILE  = "SO77361444.png"
FILE2 = "SO77361444_inkscape.png"

set xrange[-10:10]
set yrange[0:3]
set key noautotitle
set samples 500

set pixmap 1 FILE  at -8,1 width 4
set pixmap 2 FILE2 at -2,1 width 4
set label 1 at -6,0.75 "OP's original PNG\nas pixmap" center
set label 2 at  0,0.75 "OP's PNG im-/exported\nvia Inkscape\nas pixmap" center
set label 3 at  6,0.75 "OP's original PNG\nas binary w rgbalpha\nand white-->transparent" center

plot for [i=10:30] i/10., \
     FILE binary filetype=png dx=0.0075 dy=0.002 \
       origin=(4,1) u (r=$1):(g=$2):(b=$3):(r*g*b==255**3?0:255) w rgbalpha
### end of script

Result:

enter image description here