To align the RGBA channels in a PIL image from a numpy array, you can use the following code:

import numpy as np
from PIL import Image

# create a numpy array with shape (height, width, 4) for RGBA channels
arr = np.zeros((100, 100, 4), dtype=np.uint8)
arr[:, :, 0] = 255  # set the red channel to 255

# create a PIL image from the numpy array
img = Image.fromarray(arr, mode='RGBA')

# align the RGBA channels
r, g, b, a = img.split()
img = Image.merge('RGBA', (b, g, r, a))

# display the image
img.show()

In this example, we create a numpy array with shape (100, 100, 4) for RGBA channels and set the red channel to 255. Then we create a PIL image from the numpy array and split it into four separate channels. We merge the channels in the order of blue, green, red, and alpha to align them properly. Finally, we display the image using the show() method.

Python PIL Image: Aligning RGBA Channels from NumPy Array

原文地址: https://www.cveoy.top/t/topic/mVvb 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录