'Exporting' 一个 tkinter canvas 到带有 Pillow 的 .png 并导致极低的分辨率
'Exporting' a tkinter canvas to .png with Pillow and results in extremely low resolution
我正在尝试 'export' tkinter
canvas 到 png
文件,如以下答案所示:
我创建了一个示例 canvas 和一个触发函数以将其导出为 png 的按钮:
import tkinter as tk
from PIL import Image
def save_as_png(canvas, fileName):
canvas.postscript(file='./' + fileName + '.eps', colormode='color')
img = Image.open(fileName + '.eps')
img.convert()
img.save(fileName + '.png', 'png')
root = tk.Tk()
canvas = tk.Canvas(root, width=100, height=100)
text = canvas.create_text(50, 50, anchor='center', text='Some text')
button = tk.Button(root, text='Save canvas', command=lambda: save_as_png(canvas, 'my canvas'))
canvas.pack()
button.pack()
root.mainloop()
不过,生成的图像分辨率极低。我花了一段时间才明白 'Some text' 实际上是这样写的:
我尝试摆弄 open
和 save
方法的参数(dpi
、quality
、compression_level
),但没有成功.
根据 .postscript()
的 pagewidth
or pageheight
上的 tkdoc:
Specifies that the Postscript should be scaled in both x and y so that the printed area is size high on the Postscript page. Size consists of a floating-point number followed by c for centimeters, i for inches, m for millimeters, or p or nothing for printer's points (1/72 inch). Defaults to the height of the printed area on the screen. If both -pageheight and -pagewidth are specified then the scale factor from -pagewidth is used (non-uniform scaling is not implemented).
# set the output width to 1000 pixels
canvas.postscript(file='./'+fileName+'.eps', colormode='color', pagewidth=1000)
我正在尝试 'export' tkinter
canvas 到 png
文件,如以下答案所示:
我创建了一个示例 canvas 和一个触发函数以将其导出为 png 的按钮:
import tkinter as tk
from PIL import Image
def save_as_png(canvas, fileName):
canvas.postscript(file='./' + fileName + '.eps', colormode='color')
img = Image.open(fileName + '.eps')
img.convert()
img.save(fileName + '.png', 'png')
root = tk.Tk()
canvas = tk.Canvas(root, width=100, height=100)
text = canvas.create_text(50, 50, anchor='center', text='Some text')
button = tk.Button(root, text='Save canvas', command=lambda: save_as_png(canvas, 'my canvas'))
canvas.pack()
button.pack()
root.mainloop()
不过,生成的图像分辨率极低。我花了一段时间才明白 'Some text' 实际上是这样写的:
我尝试摆弄 open
和 save
方法的参数(dpi
、quality
、compression_level
),但没有成功.
根据 .postscript()
的 pagewidth
or pageheight
上的 tkdoc:
Specifies that the Postscript should be scaled in both x and y so that the printed area is size high on the Postscript page. Size consists of a floating-point number followed by c for centimeters, i for inches, m for millimeters, or p or nothing for printer's points (1/72 inch). Defaults to the height of the printed area on the screen. If both -pageheight and -pagewidth are specified then the scale factor from -pagewidth is used (non-uniform scaling is not implemented).
# set the output width to 1000 pixels
canvas.postscript(file='./'+fileName+'.eps', colormode='color', pagewidth=1000)