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

回眸只為那壹抹淺笑 提交于 2021-02-09 10:57:53

问题


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?


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/45484599/how-to-create-png-images-with-more-than-72dpi-using-gnuplot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!