[英]How to change the color channels of an image in Python?
这个问题可能应该浮在这里的某个地方,但是我似乎找不到。
我有以下代码:
import matplotlib.image as img
img1 = img.imread('./images_32x32/test_1.png')
print("Shape = ", img1.shape) # Output: 32x32x3
img2 = img.imread('./images_32x32/test_2.png')
print("Shape = ", img2.shape) # Output: 32x32x4
我的问题是img2( img2.shape
)的形状,我希望它也是3。
我该如何改变?
您的第一个图像是RGB图像(每个像素三个值,红色,绿色和蓝色),第二个图像是RGBA图像(红色,绿色,蓝色和Alpha,用于像素的透明度)。
它只是一个numpy数组,您可以对其进行切片:
img2_rgb = img2[:, :, :3]
产生没有透明度数据的32x32x3图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.