【问题标题】:Am I using the PIL library in a wrong way?我是否以错误的方式使用 PIL 库?
【发布时间】: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


【解决方案1】:

您的问题是您不能直接将文件名(或文件名元组,因为您使用的是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(...)

【讨论】:

    猜你喜欢
    • 2015-08-05
    • 1970-01-01
    • 2019-12-13
    • 1970-01-01
    • 2019-06-13
    • 2011-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多