使用Pillow时出现TypeError: argument must be sequence错误求助
问题解决:TypeError: argument must be sequence
错误根源
触发错误的核心原因是:draw.text()的第一个参数要求传入坐标序列(元组或列表,比如(x, y)),但你的代码里错误地把两个坐标值都塞进了round()函数中,导致最终传入的是单个数值,不是序列。
看出错的代码行:
draw.text(round((template.size[0]/8), round(y-15)), description)
这里round()的语法被误用了——round的第二个参数是小数保留位数,不是另一个需要取整的数值。你原本想对x和y坐标分别取整,结果写成了错误的round调用,导致坐标参数变成单个数字,不符合draw.text的要求。
此外,代码里还有两个潜在问题:
- 依赖全局变量
image.size,但函数参数是pfp,应该改用pfp.size避免全局变量依赖 Image.Image.paste的调用方式冗余,直接用实例方法template.paste()更合理
修正后的完整代码
from PIL import Image, ImageDraw def edit(template, pfp, description): # 计算pfp在template上的居中坐标,改用pfp.size而非全局image.size x = (template.size[0] - pfp.size[0]) / 2 y = (template.size[1] - pfp.size[1]) / 2 # 直接调用实例的paste方法,无需调用Image.Image类方法 template.paste(pfp, (round(x), round(y))) draw = ImageDraw.Draw(template) # 修正坐标参数:分别对x、y取整,组成元组传入 draw.text((round(template.size[0]/8), round(y-15)), description) template.show() return template
关键修正点
- 坐标参数修正:将
draw.text的第一个参数改为(round(template.size[0]/8), round(y-15)),确保是包含x、y的元组 - 变量替换:把
image.size替换为pfp.size,遵循函数参数的作用域规范 - 方法调用简化:用
template.paste()替代Image.Image.paste(template, ...),符合PIL的常规用法
内容的提问来源于stack exchange,提问作者m. lekk
相关产品推荐
相关产品推荐

