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

PySide2中拆分QImage为子图像的问题及报错咨询

PySide2中从大图裁剪子图像的内存操作问题

我有一张由其他程序生成的包含多个子图像的大图,最终需要将这些子图像用作按钮图标。我理解访问QImage位和字节的数学逻辑,但不清楚如何在PySide2中获取偏移后的位/内存地址。

我尝试将C++版本的裁剪代码转换为Python代码:

def myCreateSubImage(largeImage, subRect):
    nOffset = subRect.x() * largeImage.depth() / 8 + subRect.y() * largeImage.bytesPerLine()
    return QImage(largeImage.bits() + nOffset, subRect.width(), subRect.height(), largeImage.bytesPerLine(), subRect.format())

但遇到以下问题:

  • 使用largeImage.bits() + nOffset时,报错:No ""+"" function for <memory at 0x0000021EF8ABC700>
  • 尝试将其视为Python memoryview对象,使用(largeImage.bits())[nOffset]时,报错:TypeError: a bytes-like object is required, not 'NoneType'
  • 尝试将图像数据转换为QByteArray未成功:使用构造函数QByteArray(largeImage.bits())提示无对应构造函数;使用QByteArray.fromRawData(largeImage.bits())也提示相同错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 09:30:57