Marian
Marian

Reputation: 51

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

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?

Upvotes: 5

Views: 8703

Answers (2)

alex
alex

Reputation: 1

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")

Upvotes: 0

user8153
user8153

Reputation: 4095

Simply specify the desired resolution in pixels rather than in centimeters:

set terminal pngcairo enhanced font "Times New Roman,12.0" size 1500,1100

will generate an image that is 1500 pixels wide and 1100 pixels high. See also help pngcairo:

The default size for the output is 640 x 480 pixels. The size option changes this to whatever the user requests. By default the X and Y sizes are taken to be in pixels, but other units are possible (currently cm and inch). A size given in centimeters or inches will be converted into pixels assuming a resolution of 72 dpi.

Upvotes: 5

Related Questions