【发布时间】:2021-03-15 08:07:02
【问题描述】:
所以,首先,我才刚开始我的 IT 学徒生涯的第一年,所以无论如何我都不是专业人士。我目前的项目是一个带有 Python 的 meme 生成器。目标是将顶部文本和底部文本输入到字段中,然后使用 PIL 在 .jpg 或 .png 上“绘制”该文本。
目前,我真的被这个特定的错误消息困住了:
Traceback (most recent call last):
File "C:\Users\erce\AppData\Local\Programs\Python\Python39\
return self.func(*args)
File "c:\Users\erce\Documents\meme-generator\appJar\appjar.
return lambda *args: funcName(param)
File "c:\Users\erce\Documents\meme-generator\memegenerator.
draw = ImageDraw.Draw(im)
draw = ImageDraw.Draw(im)
File "C:\Users\erce\AppData\Local\Programs\Python\Python39\lib\site-packages\PIL\ImageDraw.py", line 684, in Draw
return ImageDraw(im, mode)
File "C:\Users\erce\AppData\Local\Programs\Python\Python39\lib\site-packages\PIL\ImageDraw.py", line 58, in __init__
im.load()
AttributeError: 'tuple' object has no attribute 'load'
具体的代码行如下:
import textwrap
import tkinter as tk
import os
import random
from tkinter import filedialog
from PIL import Image, ImageDraw, ImageFont, ImageFile # Pillow für öffnen, manipulieren und speichern von bildern
from appJar import gui # für das GUI element
def generate_meme(stroke_width=5, font_path="C:/Windows/Fonts/Impact.ttf", font_size=9):
im = filedialog.askopenfilenames(filetypes=[("images", "*.png, *.jpg")])
draw = ImageDraw.Draw(im)
image_width, image_height = im.size
font = ImageFont.truetype(font=font_path, size=int(image_height * font_size) // 100)
toptext = app.getEntry("Top Text") # text aus feldern übernehmen
bottomtext = app.getEntry("Bottom Text")
toptext = toptext.upper() # convert text to uppercase
bottomtext = bottomtext.upper()
char_width, char_height = font.getsize("A") # text wrapping
chars_per_line = image_width // char_width
top_lines = textwrap.wrap(toptext, width=chars_per_line)
bottom_lines = textwrap.wrap(bottomtext, width=chars_per_line)
# draw top lines
y = 10
for line in top_lines:
line_width, line_height = font.getsize(line)
x = (image_width - line_width) / 2
draw.text((x, y), line, fill="white", font=font, stroke_width=stroke_width, stroke_fill="black")
y += line_height
# draw bottom lines
y = image_height - char_height * len(bottom_lines) - 15
for line in bottom_lines:
line_width, line_height = font.getsize(line)
x = (image_width - line_width) / 2
draw.text((x, y), line, fill="white", font=font, stroke_width=stroke_width, stroke_fill="black")
y += line_height
# save meme
im.save("meme-" + im.filename.split("/")[-1])
我希望能提供一些关于哪里出错的提示。我非常依赖 PIL 的文档,并且似乎受到影响的特定行(第 12 行)的编写方式也与文档中的内容完全相同。然而在这里它只是给了我错误。
【问题讨论】:
-
askopenfilenames返回多个文件名,一个;然后,您需要先加载图像,而不仅仅是将文件名传递给 ImageDraw。 -
im = filedialog.askopenfilenames(...)很可能不是正确的Image对象,因此draw = ImageDraw.Draw(im)崩溃。我想,你需要一些Image.open(...)事先。
标签: python tkinter python-imaging-library appjar