Use Python Image Library to Isolate a Single Channel

So to speak, I have an image, and I want to make only the red channel appear, and the image looks red, how would I do it with PIL? Thanks.

+4
source share
3 answers

I have found the answer. Instead of using im.split (), which converted the group to grayscale, I had to convert the image to an array, multiply the groups that I don't want by 0, and then returned back to the Image object.

By importing the image and numpy, I did the following:

a = Image.open("image.jpg") a = numpy.array(a) a[:,:,0] *=0 a[:,:,1] *=0 a = Image.fromarray(a) a.show() 

This will display a blue image.

+4
source

You can use the Image.split() operation from PIL to split the image into stripes:

 img = Image.open("image.jpg") red, green, blue = img.split() 

If the image has an alpha channel (RGBA), the split function will bring it back. More info here .

+1
source

Late, but since we are dealing with numpy, we can use logical indexing.

 def channel(img, n): """Isolate the nth channel from the image. n = 0: red, 1: green, 2: blue """ a = np.array(img) a[:,:,(n!=0, n!=1, n!=2)] *= 0 return Image.fromarray(a) 
0
source

Source: https://habr.com/ru/post/1491565/


All Articles