Python Tkinter使用PIL将画布另存为图像 [英] Python Tkinter save canvas as image using PIL

查看:230
本文介绍了Python Tkinter使用PIL将画布另存为图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码,可以让用户在画布上绘制并将其另存为jpeg文件.

I have this code which lets the user draw on the canvas and save it as a jpeg file.

这篇文章中所述,我尝试使用PIL在画布上和内存中并行绘制,以便我可以将其另存为jpeg而不是postscript.直到我发现用PIL保存的某些图像与在画布上绘制的图像不同之前,这种方法似乎一直有效.

As mentioned in this post, I tried to draw in parallel on the canvas and in memory using the PIL so that I can save it as a jpeg instead of postscript. It seemed to be working until I found out that some of the images I saved with PIL are not the same as what was drawn on the canvas.

我假设PIL图像绘制模块中的canvas.create_linedraw.line具有相似的功能,并且应该提供相似的输出.

I assume canvas.create_line and draw.line from the PIL image draw module function similarly and should give similar output.

以下是哪里出了问题:

例如,当我绘制"T"时,看起来还不错(左是我的图,右是保存的图像).

For example, when I draw a "T" it seems alright (Left is my drawing, right is the saved image).

但是当我画一个"A"时,输出图像看起来有点奇怪.

But when I draw an "A" , the output image seems a bit weird.

这是我当前的代码:

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("<Motion>", self.motion)
        self.drawing_area.bind("<ButtonPress-1>", self.b1down)
        self.drawing_area.bind("<ButtonRelease-1>", 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图像?

Where did I go wrong and what should I do to save the exact same picture that is drawn on the canvas as a jpeg image?

推荐答案

我设法解决了我的问题.实际上我只是傻瓜.即使canvas.create_linedraw.line具有相似的功能,我也没有使用相同的确切数据绘制两个图像.进行更改后,这是我的工作代码.

I managed to solved my problem.In fact i was just being dumb. Even though canvas.create_line and draw.line have the similar function, i didn't use the same exact data to draw out both images. after making changes, this is my working 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.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("<Motion>", self.motion)
        self.drawing_area.bind("<ButtonPress-1>", self.b1down)
        self.drawing_area.bind("<ButtonRelease-1>", 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):
        filename = "temp.jpg"
        self.image.save(filename)

    def clear(self):
        self.drawing_area.delete("all")
        self.image=Image.new("RGB",(200,200),(255,255,255))
        self.draw=ImageDraw.Draw(self.image)

    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.draw.line(((self.xold,self.yold),(event.x,event.y)),(0,128,0),width=3)

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

这篇关于Python Tkinter使用PIL将画布另存为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆