#! usr/bin/python
# coding=utf-8
import numpy as np
import cv2
import os
# opencv read write image
img = np.zeros((3, 3), dtype=np.uint8)
img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
image = cv2.imread('test.jpg')
print image.shape
print image.size
print image.dtype
cv2.imwrite('test1.png', image)
# IMREAD_UNCHANGED = -1
# IMREAD_GRAYSCALE = 0
# IMREAD_COLOR = 1
# IMREAD_ANYDEPTH = 2
# IMREAD_ANYCOLOR = 4
# IMREAD_LOAD_GDAL = 8
gray_image = cv2.imread('test.jpg', cv2.IMREAD_GRAYSCALE)
cv2.imwrite('IMREAD_GRAYSCALE.png', gray_image)
random_byte_array = bytearray(os.urandom(120000))
flat_numpy_array = np.array(random_byte_array)
gray_image2 = flat_numpy_array.reshape(300, 400)
cv2.imwrite('gray_image2.png', gray_image2)
bgr_image = flat_numpy_array.reshape(100, 400, 3)
cv2.imwrite('color_image.png', bgr_image)
# image.itemset((200, 200, 0), 0)
# print image.item(200, 200, 0)
# image[ : : 2] = 0
my_roi = image[0: 300, 0:300]
image[300:600, 300:600] = my_roi
cv2.imwrite('test2.png', image)
# opencv show image
cv2.imshow('gray-image', gray_image)
cv2.waitKey()
cv2.destroyAllWindows()
网友评论