python - 有没有办法在现有变量上使用 Pillows "Image.convert()"?

标签 python image python-imaging-library grayscale

你好,

也许这个问题看起来很愚蠢,但我尝试使用 Pillows Image.convert() 将图像转换为灰度。该图像我已存储在变量 img 中,因为我已经对其进行了预处理,但没有使用 Pillow(类型:numpy.ndarray)进行预处理。所以我输入:

img = Image.convert('LA')

但它似乎不起作用,正如它所说:

AttributeError: module 'PIL.Image' has no attribute 'convert'

如果我输入 img = Image.open("picture.jpg").convert('LA') 它可以工作,但我想在已经存在的变量上使用它。我也不想保存预处理的图像只是为了使用前面的命令打开和转换它,因为这样效率更低(在速度和 CPU 功率方面)。 那么:有没有正确的方法来做到这一点?

提前感谢您的帮助!

最佳答案

而不是说Image.convert() 使用你的图像变量: 图像 例如 img = img.convert('') 在这种情况下:

img = img.convert('LA')

关于python - 有没有办法在现有变量上使用 Pillows "Image.convert()"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58526520/

相关文章:

android:带缩略图的 ListView

python - 如何使用python对图像进行base64编码

python - Pillow (PIL) 到 QImage 的转换 -> python.exe 已停止工作

python - Python 中的上下文相关方法 - 我做错了什么?

python - Pyqtgraph - 导入语句的正确顺序?

python - 无法从 opencv 读取 .mp4

python - 在 Python 中的二维数组上的滑动窗口中计算模式的最有效方法是什么?

python - 扭曲: `defer.execute` 和 `threads.deferToThread` 之间的区别

python - 性能:Pandas index.intersection() 与集合交集

css - 使 2 个背景图像相等