所以,首先,我只是我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行)的编写也与文档中的完全相同。然而,在这里,它只会给我错误。
发布于 2021-03-15 16:16:25
这里的问题是您不能直接将文件名(或文件名元组,因为您使用的是getopenfilenames,复数)传递给ImageDraw;您需要首先使用Image.open()打开图像。
我建议将你的代码重构成更容易测试的片段,例如:
import textwrap
from PIL import Image, ImageDraw, ImageFont, ImageFile
def generate_meme(image, toptext, bottomtext, stroke_width=5, font_path="C:/Windows/Fonts/Impact.ttf", font_size=9):
draw = ImageDraw.Draw(image)
image_width, image_height = image.size
font = ImageFont.truetype(font=font_path, size=int(image_height * font_size) // 100)
toptext = toptext.upper()
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
return image
def test_generate():
image = Image.open("C:/Users/granapadano/Desktop/cheese.jpg")
image = generate_meme(
image=image,
toptext="This is not",
bottomtext="a meme",
)
image.save("C:/Users/granapadano/Desktop/cheesememe.jpg")
if __name__ == "__main__":
test_generate()您可以按原样运行此脚本,而不必单击鼠标来选择文件或键入文本,一旦它运行得令人满意,就可以在第二个函数中添加GUI内容。
def generate_with_gui():
filename = filedialog.askopenfilename(filetypes=[("images", "*.png, *.jpg")])
image = Image.open(filename)
image = generate_meme(
image=image,
toptext=app.getEntry("Top Text"),
bottomtext=app.getEntry("Bottom Text"),
)
image.save(...)https://stackoverflow.com/questions/66634323
复制相似问题