Pillow Equivalent Of The Following OpenCV Code
I am trying to convert the following code module written in openCV to pillow but I am unable to figure out to do it? j is an rgb image img = cv2.imread(j,1) b,g,r = cv2.split(img)
Solution 1:
You can do that using PIL and Numpy like this - I tend to go into Numpy as it is faster and more flexible:
#!/usr/bin/env python3
import numpy as np
from PIL import Image
# Open input image and ensure it is RGB
im = Image.open('start.png').convert('RGB')
# Make into Numpy array
imnp = np.array(im)
# Split into 3 constituent bands
r = imnp[:, :, 0]
g = imnp[:, :, 1]
b = imnp[:, :, 2]
# Process
g = 2*g - r - b
# Recombine to single image and save
merged = np.dstack((r, g, b))
Image.fromarray(merged).save('result.png')
Or you can be less explicit about splitting and do it in-place on the whole image:
#!/usr/bin/env python3
import numpy as np
from PIL import Image
# Open input image and ensure it is RGB
im = Image.open('start.png').convert('RGB')
# Make into Numpy array
imnp = np.array(im)
# Process
imnp[:,:,1] = 2*imnp[:,:,1] - imnp[:,:,0] - imnp[:,:,2]
# Save
Image.fromarray(imnp).save('result2.png')
Keywords: Python, Numpy, PIL, Pillow, color matrix, colour matrix, transform, multiply channel, scale channel, separate, separately, individual channels, bands, components, individually, image, image processing.
Post a Comment for "Pillow Equivalent Of The Following OpenCV Code"