基于python3+opencv3图像基础IO操作

环境

Anaconda+python3.5.2+opencv3

1.

先测试一下numpy的矩阵。

import numpy as np
import cv2

img=np.zeros((3,3),dtype=np.uint8)
print(img.shape)
img=cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
print('\n')
print(img.shape)
print('\n')
print(img)

 

 

 

 

2.

import cv2

grayImage=cv2.imread('/home/cisco/Pictures/Webcam/ss.jpg',0)
cv2.imwrite('ssgray.jpg',grayImage)

AnyColor=cv2.imread('/home/cisco/Pictures/Webcam/ss.jpg',4)
cv2.imwrite('ssAnyColor.jpg',AnyColor)

 

src

 

grayImage

 

AnyColor

 

3.

Random Array into PNG

import cv2
import numpy as np
import os

randomByteArray=bytearray(os.urandom(120000))
flatNumpyArray=np.array(randomByteArray)

grayImage =flatNumpyArray.reshape(300,400)
cv2.imwrite('RandomGray.png',grayImage)

bgrImage=flatNumpyArray.reshape(100,400,3)
cv2.imwrite('RandomColor.png',bgrImage)

Random Gray

Random Color

 

posted @ 2016-09-12 23:46  MnsterLu  阅读(1692)  评论(0编辑  收藏  举报