怎么了Image.ImageFont.ImageFont.getsize()命令是否有效?

2024-04-26 18:53:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要使用这个命令来选择字体大小最大的文本,但是经过无数次的尝试之后,什么都不起作用

我试过: Image.ImageFont.ImageFont.getsize(text){text是它扫描的文本的变量}

[ERROR]: TypeError: getsize() missing 1 required positional argument: 'text'

{cd2}

[ERROR]: TypeError: getsize() missing 1 required positional argument: 'self'

Image.ImageFont.ImageFont.getsize(self, text = 'Lorem Ipsum')

[ERROR]: NameError: name 'self' is not defined

我不太明白self应该做什么/意味着什么。在

旁注:如果代码应该浏览文本并找到最大的字体大小,为什么我必须选择一个它应该确定字体大小的单词,它不应该在整个文章中这样做吗?在

以下是我的全部代码:

from PIL import Image, ImageFont

image = Image.open('screenshot - copy.bmp') 

fontget = ImageFont.ImageFont.getsize(text = 'food')
print(fontget)

我要它做的是扫描一段文章并返回不同的字体大小。 从那里,我将使它用最大的字体打印文本。在


Tags: 代码textimage文本self文章requirederror
1条回答
网友
1楼 · 发布于 2024-04-26 18:53:20
[ERROR]: TypeError: getsize() missing 1 required positional argument: 'text'

前面的错误意味着您调用了一个类的实例方法,而不是一个类的实例。您必须先加载字体来实例化ImageFont类。在

^{pr2}$

Side Note: If the code is supposed to look through text and find the biggest font size, why do I have to choose a word whose font size it should determine, shouldn't it do that for the whole passage?

这不是getsize()所做的。它返回PIL.ImageDraw.Draw.text()将以选定字体绘制的文本大小。在

>>> font.getsize('h')
(6, 11)
>>> font.getsize('hello')
(30, 11)
>>> font.getsize('hello hello this is long text and you can see x gets bigger because the text gets wider')
(522, 11)

相关问题 更多 >