You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用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

关键修正点

  1. 坐标参数修正:将draw.text的第一个参数改为(round(template.size[0]/8), round(y-15)),确保是包含x、y的元组
  2. 变量替换:把image.size替换为pfp.size,遵循函数参数的作用域规范
  3. 方法调用简化:用template.paste()替代Image.Image.paste(template, ...),符合PIL的常规用法

内容的提问来源于stack exchange,提问作者m. lekk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 02:50:33