使用PIL将画布保存为图像的Python Tkinter 您所在的位置:网站首页 python画布 使用PIL将画布保存为图像的Python Tkinter

使用PIL将画布保存为图像的Python Tkinter

2023-03-15 14:33| 来源: 网络整理| 查看: 265

我有这样一段代码,它让用户在画布上画画,并将其保存为jpeg文件。

正如在this post我试着在画布上和内存中使用PIL并行绘制,这样我就可以把它保存为jpeg而不是postscript。这似乎是有效的,直到我发现我用PIL保存的一些图像与画布上画的不一样。

我认为PIL图像绘制模块中的canvas.create_line和draw.line功能相似,应该会有类似的输出。

以下是出错的原因。

例如,当我画一个 "T "时,似乎没有问题(左边是我画的,右边是保存的图像)。

enter image description hereenter image description here

但当我画一个 "A "时,输出的图像似乎有点奇怪。

enter image description hereenter image description here

This is my current code:

import Tkinter as tk import Image,ImageDraw class ImageGenerator: def __init__(self,parent,posx,posy,*kwargs): self.parent = parent self.posx = posx self.posy = posy self.sizex = 200 self.sizey = 200 self.b1 = "up" self.xold = None self.yold = None self.coords= [] self.drawing_area=tk.Canvas(self.parent,width=self.sizex,height=self.sizey) self.drawing_area.place(x=self.posx,y=self.posy) self.drawing_area.bind("", self.motion) self.drawing_area.bind("", self.b1down) self.drawing_area.bind("", self.b1up) self.button=tk.Button(self.parent,text="Done!",width=10,bg='white',command=self.save) self.button.place(x=self.sizex/7,y=self.sizey+20) self.button1=tk.Button(self.parent,text="Clear!",width=10,bg='white',command=self.clear) self.button1.place(x=(self.sizex/7)+80,y=self.sizey+20) self.image=Image.new("RGB",(200,200),(255,255,255)) self.draw=ImageDraw.Draw(self.image) def save(self): print self.coords self.draw.line(self.coords,(0,128,0),width=3) filename = "temp.jpg" self.image.save(filename) def clear(self): self.drawing_area.delete("all") self.coords=[] def b1down(self,event): self.b1 = "down" def b1up(self,event): self.b1 = "up" self.xold = None self.yold = None def motion(self,event): if self.b1 == "down": if self.xold is not None and self.yold is not None: event.widget.create_line(self.xold,self.yold,event.x,event.y,smooth='true',width=3,fill='blue') self.coords.append((self.xold,self.yold)) self.xold = event.x self.yold = event.y if __name__ == "__main__": root=tk.Tk() root.wm_geometry("%dx%d+%d+%d" % (400, 400, 10, 10)) root.config(bg='white') ImageGenerator(root,10,10) root.mainloop()

我哪里做错了,我应该怎么做才能把画在画布上的完全相同的图片保存为jpeg图片?



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有