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

使用rembg库遇‘无法打开i’及Pickling错误的解决求助

解决rembg在M1 macOS下的CLI与代码调用问题

一、修复CLI命令报错

你遇到的rembg: error: argument input: can't open 'i': [Errno 2] No such file or directory: 'i'错误,是因为命令语法错误——1.0.27版本的rembg CLI不支持用i作为input子命令的缩写,正确命令应该是:

rembg input man.jpeg output.png

如果要使用更简洁的子命令,也可以用process(缩写p):

rembg p man.jpeg output.png

执行前确认当前目录下确实存在man.jpeg,可以用ls命令验证。

二、解决代码调用的_pickle.UnpicklingError错误

这个错误的核心原因是模型文件损坏或下载错误(比如下载到的是HTML页面而非真实模型),即使你手动下载了模型,也可能因为文件不对或路径配置问题导致加载失败,按以下步骤排查:

  1. 删除损坏的模型文件
    找到Python环境中rembg的u2net目录(通常在site-packages/rembg/models/u2net),删除其中所有.pth和.onnx文件。
  2. 重新下载正确的模型
    下载对应模型文件时,确保获取的是真实模型而非网页内容,下载后将文件放到site-packages/rembg/models/u2net目录下,验证文件大小:u2net.pth约176MB,u2net.onnx约44MB。
  3. 代码调用时手动指定模型路径
    如果自动加载仍有问题,在代码中直接指定模型路径:
    from rembg import remove
    from PIL import Image
    
    input_path = 'man.jpeg'
    output_path = 'output.png'
    model_path = '/path/to/site-packages/rembg/models/u2net/u2net.onnx'
    
    with open(input_path, 'rb') as i:
        with open(output_path, 'wb') as o:
            input_data = i.read()
            output_data = remove(input_data, model_path=model_path)
            o.write(output_data)
    
  4. 适配M1芯片的依赖
    M1芯片需要安装适配arm64的onnxruntime版本:
    pip uninstall onnxruntime
    pip install onnxruntime-silicon
    

三、额外排查步骤

  • 确认Python环境是M1原生的arm64版本,避免用x86_64版本通过Rosetta运行,可能引发路径和依赖异常。
  • 检查目录权限,确保rembg有读取模型文件和写入输出文件的权限。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 17:36:23