python - 为什么 Pillow 通过 ImageDraw 绘图时会反转颜色?
问题描述
我正在尝试将图像粘贴到另一个 8 位/像素(L
模式)上,但颜色会反转。如何解决这个问题?示例代码:
icon_bmp2 = Image.open("assets/test_penguin.png")
icon_bmp2 = icon_bmp2.convert("L")
im2 = Image.new('L', (650, 500), 0xFF) # this line cannot be changed
image_draw2 = ImageDraw.Draw(im2)
image_draw2.bitmap((0, 0), icon_bmp2)
im2.save('test.png') # inverted
测试图像来自这里:https ://github.com/GregDMeyer/IT8951/blob/master/test/integration/images/sleeping_penguin.png ,我使用的是 Python 3.7 和 Pillow 7.2.0
解决方案
您应该使用该Image.paste()
函数而不是ImageDraw.bitmap()
将一个图像的像素粘贴到另一个图像上。它也更易于使用,因为无需创建ImageDraw
第一个即可使用它。
from PIL import Image, ImageDraw
icon_bmp2 = Image.open("sleeping_penguin.png")
icon_bmp2 = icon_bmp2.convert("L")
im2 = Image.new('L', (650, 500), 0xFF) # This line cannot be changed.
im2.paste(icon_bmp2)
im2.save('test2.png')
非反转结果
推荐阅读
- javascript - Vue.js - 在 v-for 生成列表中切换单击图标
- java - 参数,静态 ServiceLoader.load() 方法的返回值?
- python - 在 python 中使用大数据进行模糊名称匹配
- python - 创建具有时差的新列(Pandas 数据框)
- javascript - 点击和滚动条件不在同一时间工作
- javascript - 如何在任何点击时关闭此菜单?
- java - 错误:在 Windows 10 IntelliJ JDK-11 中发生了 JNI 错误
- ios - ios 13上的导航栏标题字体问题
- python - 在 OpenCV Python 中查找几帧的模式
- kotlin - 如果其中之一为空,则发布者串联