python - 如何用枕头中的颜色替换透明
问题描述
我需要用白色替换 png 图像的透明层。我试过这个
from PIL import Image
image = Image.open('test.png')
new_image = image.convert('RGB', colors=255)
new_image.save('test.jpg', quality=75)
但是透明层变黑了。任何人都可以帮助我吗?
解决方案
将图像粘贴到完全白色的 rgba 背景上,然后将其转换为 jpeg。
from PIL import Image
image = Image.open('test.png')
new_image = Image.new("RGBA", image.size, "WHITE") # Create a white rgba background
new_image.paste(image, (0, 0), image) # Paste the image on the background. Go to the links given below for details.
new_image.convert('RGB').save('test.jpg', "JPEG") # Save as JPEG
推荐阅读
- database - 如何在访问数据库之前在 adminMongo 中请求登录
- google-apps-script - 如何解决 Google Apps 脚本中的“今天此服务调用过多:gmail”错误?
- python-3.x - createBackgroundSubtractor opencv python getBackgroundImage 未实现
- javascript - 如何在引导模型的新窗口中显示下载提示?
- javascript - 在浏览器中正确显示“货币”格式的最佳方法是什么?
- android - Android Studio 示例程序错误
- nginx - nGinx,多个位置服务于不同的 SPA
- typescript - Typescript中的铸造选项之间的区别?
- delphi - Delphi 中的 AtomicExchange 记录
- javascript - 自动从 100 中减去两个值