【问题标题】:How can I convert an image to grey scale while keeping transparency using cv2?如何在使用 cv2 保持透明度的同时将图像转换为灰度?
【发布时间】:2018-12-01 04:03:15
【问题描述】:

目前,我的代码采用彩色图像并将其转换为灰度图像。问题是它使透明像素变成白色或黑色。

这是我目前所拥有的:

import cv2

img = cv2.imread("watch.png",cv2.IMREAD_GRAYSCALE)
cv2.imwrite("gray_watch.png",img)

这是供参考的图片: Watch.png

【问题讨论】:

  • 感谢您的评论!我是新手,所以请原谅我有这么多的绒毛。我已经把它编辑到了它的骨架。让我知道你想出了什么。再次感谢!

标签: python transparency transparent grayscale cv2


【解决方案1】:

我发现只使用 PIL 可以做到这一点:

from PIL import Image

image_file = Image.open("convert_image.png") # opens image  
image_file = image_file.convert('LA') # converts to grayscale w/ alpha
image_file.save('output_image.png') # saves image result into new file

这也保留了图像的透明度。 “LA”是一种色彩模式。您可以在这里找到其他模式:https://pillow.readthedocs.io/en/3.1.x/handbook/concepts.html#concept-modes

【讨论】:

    猜你喜欢
    • 2011-12-26
    • 2012-11-23
    • 2011-01-25
    • 2014-08-22
    • 1970-01-01
    • 1970-01-01
    • 2015-04-17
    • 2018-12-28
    • 2017-12-27
    相关资源
    最近更新 更多