从灰度图像中删除alpha通道

2024-09-29 23:30:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些B&;W图像,但在RGBa中。我使用skiliagergb2gray(inp_image)将它们转换为灰度。然而,它们变成了带有alpha通道的灰度图像

如果我想将这些RGBa转换为灰度而不使用alpha通道,我该怎么办


Tags: 图像imagealpha灰度ampinprgbaskiliagergb2gray
1条回答
网友
1楼 · 发布于 2024-09-29 23:30:34

你可以试试这个

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))

相关问题 更多 >

    热门问题