使用python中的cv2模块,将彩色图秒变酷炫黑白图,比PS还好用!
完整代码:
import cv2
#img = cv2.imread('./imgs/2.jpg',cv2.IMREAD_UNCHANGED)
img = cv2.imread('./imgs/black.jpg',cv2.IMREAD_UNCHANGED)
#彩色图像转为灰度图像
img = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
#灰度图像转为彩色图像
#img = cv2.cvtColor(img,cv2.COLOR_GRAY2RGB)
# cv2.COLOR_X2Y,其中X,Y = RGB, BGR, GRAY, HSV, YCrCb, XYZ, Lab, Luv, HLS
cv2.imshow('image',img)
k = cv2.waitKey(0)
if k == ord('s'): # wait for 's' key to save and exit
cv2.imwrite('./output/black.jpg',img)
#cv2.imwrite('./output/colorful.jpg',img)
cv2.destroyAllWindows()
else:
cv2.destroyAllWindows()
效果展示: