How to create PNG images with more than 72dpi using gnuplot?

Viewed 6249

I'd like to create a PNG image in gnuplot and use it in MS-Word. However, if I use

set terminal pngcairo enhanced font "Times New Roman,12.0" size 15cm,11cm  

the quality turns out to be poor. I read that the GNUPLOT uses only 72dpi. Is there a way to increase this number?

2 Answers

I was wondering the same thing, e.g. having a default DPI option for the output (e.g. when you always need to import it in the same size into powerpoint).

I wrote a short python script, which adds the necessary information into the header (probably not very clean, but works fine with python 3). You just need to change the filename and DPI you want. I automatically call this scrip now using the gnuplot system() command to change the DPI after printing the plot.

Cheers, Alex

filename = '/test.png' #path to file
dpi = 300.             #dpi you want

####
import binascii
DpM = dpi/0.0254 #dots per meter
DpM_hex = ('%08x' % (int(DpM)))
chunkType = "70485973" #type: pHYS
length = ('%08x' % (int(9))) #length is 9
str_con = chunkType + DpM_hex + DpM_hex + "01"
crc = ('%08x' % (binascii.crc32(binascii.unhexlify(str_con.encode('utf-8')))))
output_str = length + str_con + crc

try: 
    with open(filename, 'rb') as f:
        content = f.read()
        png_str = (binascii.hexlify(content)).decode('utf-8')
        if png_str.find(length+chunkType)>=0:
            pos1 = png_str.find(length+chunkType)
            pos2 = pos1+len(output_str)
            png_new = png_str[:pos1] + output_str + png_str[pos2:]
        else:
            pos = 66 #position where additional chunk is inserted, = end of mandatory PNG header info
            png_new = png_str[:pos] + output_str + png_str[pos:]
    
    with open(filename, 'w+b') as f:
        f.write(binascii.unhexlify(png_new.encode('utf-8')))
        f.close()
        print("Done")
        print(filename)
    print("Done")
Related