如何 'save as' 使用 tkinter 中的文件对话框和 Python 中的 Pil 编辑图像 (png)
How to 'save as' an edited image (png) using a file dialog in tkinter and Pil in Python
我正在尝试创建一个图像编辑器,使用 pillow 向图像添加文本。我的问题是保存编辑后的图像,以便用户可以通过打开另存为对话框来选择保存文件的名称。看了其他问答,我想到了这个:
def onOpen(self):
im = Image.open(askopenfilename())
caption = simpledialog.askstring("Label", "What would you like the label on your picture to say?")
fontsize = 15
if im.mode != "RGB":
im = im.convert("RGB")
draw = ImageDraw.Draw(im)
font = ImageFont.truetype("arial.ttf", fontsize)
draw.text((0, 0),str(caption),(255,0,0),font=font)
file = filedialog.asksaveasfile(mode='w', defaultextension=".png")
if file:
file.write(im)
file.close()
但是,当 运行 它时,我得到以下错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Renee\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1550, in __call__
return self.func(*args)
File "C:\Users\Renee\AppData\Local\Programs\Python\Python35-32\tkinterguitest.py", line 52, in onOpen
file.write(im)
TypeError: write() argument must be str, not Image
我知道问题是 write 只能用于字符串,所以有没有类似 file.write 但用于图像的命令?
谢谢!
您应该通过Image对象中的save方法保存图片:
file = filedialog.asksaveasfile(mode='w', defaultextension=".png")
if file:
im.save(file) # saves the image to the input file name.
我终于明白了。我最终分别创建了每个组件(图像和文本),然后将最终图像保存为 composite.Here 是最终代码:
def onOpen(self):
im = Image.open(askopenfilename())
caption = simpledialog.askstring("Label", "What would you like the label on your picture to say?")
fontsize = 30
if im.mode != "RGBA":
im = im.convert("RGBA")
txt = Image.new('RGBA', im.size, (255,255,255,0))
draw = ImageDraw.Draw(txt)
font = ImageFont.truetype("arial.ttf", fontsize)
draw.text((0, 0),caption,(255,0,0),font=font)
file = filedialog.asksaveasfile(mode='w', defaultextension=".png", filetypes=(("PNG file", "*.png"),("All Files", "*.*") ))
if file:
abs_path = os.path.abspath(file.name)
out = Image.alpha_composite(im, txt)
out.save(abs_path) # saves the image to the input file name.
我正在尝试创建一个图像编辑器,使用 pillow 向图像添加文本。我的问题是保存编辑后的图像,以便用户可以通过打开另存为对话框来选择保存文件的名称。看了其他问答,我想到了这个:
def onOpen(self):
im = Image.open(askopenfilename())
caption = simpledialog.askstring("Label", "What would you like the label on your picture to say?")
fontsize = 15
if im.mode != "RGB":
im = im.convert("RGB")
draw = ImageDraw.Draw(im)
font = ImageFont.truetype("arial.ttf", fontsize)
draw.text((0, 0),str(caption),(255,0,0),font=font)
file = filedialog.asksaveasfile(mode='w', defaultextension=".png")
if file:
file.write(im)
file.close()
但是,当 运行 它时,我得到以下错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Renee\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1550, in __call__
return self.func(*args)
File "C:\Users\Renee\AppData\Local\Programs\Python\Python35-32\tkinterguitest.py", line 52, in onOpen
file.write(im)
TypeError: write() argument must be str, not Image
我知道问题是 write 只能用于字符串,所以有没有类似 file.write 但用于图像的命令? 谢谢!
您应该通过Image对象中的save方法保存图片:
file = filedialog.asksaveasfile(mode='w', defaultextension=".png")
if file:
im.save(file) # saves the image to the input file name.
我终于明白了。我最终分别创建了每个组件(图像和文本),然后将最终图像保存为 composite.Here 是最终代码:
def onOpen(self):
im = Image.open(askopenfilename())
caption = simpledialog.askstring("Label", "What would you like the label on your picture to say?")
fontsize = 30
if im.mode != "RGBA":
im = im.convert("RGBA")
txt = Image.new('RGBA', im.size, (255,255,255,0))
draw = ImageDraw.Draw(txt)
font = ImageFont.truetype("arial.ttf", fontsize)
draw.text((0, 0),caption,(255,0,0),font=font)
file = filedialog.asksaveasfile(mode='w', defaultextension=".png", filetypes=(("PNG file", "*.png"),("All Files", "*.*") ))
if file:
abs_path = os.path.abspath(file.name)
out = Image.alpha_composite(im, txt)
out.save(abs_path) # saves the image to the input file name.