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

使用Pillow做图像变换时遇AttributeError:模块无Transpose属性

解决Pillow的AttributeError: module 'PIL.Image' has no attribute 'Transpose'错误

错误原因

Image.Transpose是Pillow 9.1.0版本才新增的枚举类,若你的Pillow版本低于该版本,就会触发这个错误。

两种解决方法

  • 方法一:升级Pillow到最新版本
    在终端执行以下命令:

    pip install --upgrade pillow
    

    升级后原有代码可正常运行,无需修改。

  • 方法二:使用旧版本兼容的常量写法
    若不想升级Pillow,直接使用PIL.Image下的直接常量即可,修改后的代码如下:

    # To use 90 Degrees Rotation, use:
    # rot_90 = pic.transpose(Image.ROTATE_90)
    
    # Horizontal Flip with New File Names
    hoz_flip = pic.transpose(Image.FLIP_LEFT_RIGHT)
    hoz_flip.save(dest_path + "flr_" + str(hf) + ".jpg")
    hf += 1
    
    # Vertical Flipping with New File Names
    ver_flip = pic.transpose(Image.FLIP_TOP_BOTTOM)
    ver_flip.save(dest_path + "ftb_" + str(vf) + ".jpg")
    vf += 1
    

    额外提示:save()方法返回None,无需重新赋值给变量,去掉hoz_flip =和ver_flip =这类赋值,可避免后续使用变量时出现异常。

内容的提问来源于stack exchange,提问作者Hesham Saber

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 19:50:21