OpenCV转换成PIL.Image格式:
import cv2
from PIL import Image
import numpy
img = cv2.imread("plane.jpg")
cv2.imshow("OpenCV",img)
image = Image.fromarray(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))
image.show()
cv2.waitKey()
PIL.Image转换成OpenCV格式:
import cv2
from PIL import Image
import numpy
image = Image.open("plane.jpg")
image.show()
img = cv2.cvtColor(numpy.asarray(image),cv2.COLOR_RGB2BGR)
cv2.imshow("OpenCV",img)
cv2.waitKey()
但是转换过后并不完全一样
<PIL.Image.Image image mode=RGB size=224x224 at 0x7F16515B5940>
[[[ 7 11 0]
[ 7 13 4]
[ 7 13 4]
...
[227 230 223]
[227 230 223]
[231 232 223]]
[[ 10 8 3]
[ 10 12 8]
[ 10 12 8]
...
[229 233 221]
[229 233 221]
[228 235 220]]
[[ 10 8 3]
[ 10 12 8]
[ 10 12 8]
...
[229 233 221]
[229 233 221]
[228 235 220]]
...
[[ 0 0 0]
[ 0 0 0]
[ 0 0 0]
...
[ 0 0 0]
[ 0 0 0]
[ 0 0 0]]
[[ 0 0 0]
[ 0 0 0]
[ 0 0 0]
...
[ 0 0 0]
[ 0 0 0]
[ 0 0 0]]
[[ 0 0 0]
[ 0 0 0]
[ 0 0 0]
...
[ 0 0 0]
[ 0 0 0]
[ 0 0 0]]]
<PIL.Image.Image image mode=RGB size=224x224 at 0x7F169C3F94A8>
[[[ 2 10 3]
[ 7 13 8]
[ 7 13 8]
...
[226 232 221]
[226 232 221]
[227 232 223]]
[[ 1 8 3]
[ 7 13 8]
[ 7 13 8]
...
[227 233 222]
[227 233 222]
[228 233 224]]
[[ 1 8 3]
[ 7 13 8]
[ 7 13 8]
...
[227 233 222]
[227 233 222]
[228 233 224]]
...
[[ 0 1 0]
[ 0 1 0]
[ 0 1 0]
...
[ 0 0 1]
[ 0 0 1]
[ 0 0 3]]
[[ 0 1 0]
[ 0 1 0]
[ 0 1 0]
...
[ 0 0 1]
[ 0 0 1]
[ 0 0 3]]
[[ 0 1 0]
[ 0 1 0]
[ 0 1 0]
...
[ 0 0 1]
[ 0 0 1]
[ 0 0 1]]]