【问题标题】:Removing alpha channels from grayscale images从灰度图像中删除 Alpha 通道
【发布时间】:2021-08-30 22:40:49
【问题描述】:

我有一些黑白图像,但在 RGBa 中。我使用 skimage rgb2gray(inp_image) 将它们转换为灰度。然而,它们变成了带有 alpha 通道的灰度图像。

如果我想将这些 RGBa 转换为没有 Alpha 通道的灰度,我该怎么办?

【问题讨论】:

  • 你可以先把你的RGBa图片转成RBG,再转成灰度图

标签: python image-processing cv2 scikit-image grayscale


【解决方案1】:

你可以试试这个。

import cv2
  
image = cv2.imread('path to your image')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  
cv2.imshow('Gray image', gray)
  
cv2.waitKey(0)
cv2.destroyAllWindows()

多张图片

import cv2
from os import listdir,makedirs
from os.path import isfile,join

source = r'path to source folder'
destination = r'path where you want to save'

files = [f for f in listdir(source) if isfile(join(source,f))] 

for image in files:
    try:
        img = cv2.imread(os.path.join(source,image))
        gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
        dstPath = join(destination,image)
        cv2.imwrite(destination,gray)
    except:
        print ("{} is not converted".format(image))

【讨论】:

  • 非常感谢。但是有什么方法可以保存图像而不是让它们显示在新窗口中?以及如何同时转换同一文件夹中的多个图像?对不起,我真的不熟悉编程。
  • 图像文件为“灰色”,您可以使用img.save('path where you want to save/gray.jpg', 'JPEG') 保存它。对于多个图像,您可以使用 for 循环
  • 不客气。如果您觉得此答案有用,请将其标记为有用。这样,当其他人也有同样的问题时,答案将更加优先。
猜你喜欢
  • 2015-02-13
  • 2023-04-02
  • 2014-11-28
  • 2017-10-11
  • 2022-12-20
  • 2014-02-20
  • 2017-01-23
  • 1970-01-01
  • 2016-07-02
相关资源
最近更新 更多