I want to do detailed processing with OpenCV, but I want to do file input / output with Pillow! Or rather, if you save it with OpenCV, it will be a file ... There are times.
That's why I wrote a sample Pillow-> OpenCV and OpenCV-> Pillow.
from PIL import Image
import cv2
import numpy as np
#Load images with PIL data
im = Image.open('t.jpg')
#Convert to OpenCV data
ocv_im = np.asarray(im)
#Save with OpenCV
cv2.imwrite("t_ocv.jpg ",ocv_im)
#Convert to PIL data
pil_im = Image.fromarray(ocv_im)
#Save as PIL
pil_im.save("t_pil.jpg ")
The file that came out in.
t_ocv.jpg
t_pil.jpg
For some reason t_ocv.jpg is blue as close to blue as possible.
When I looked it up, OpenCV said that the color was BGR instead of RGB.
So if you convert ocv_im to RGB, there is no problem.
cv2.imwrite("t_ocv.jpg ",ocv_im)
↓
cv2.imwrite("t_ocv.jpg ",ocv_im[:, :, ::-1].copy())
Solution. that's all.