python-OpenCV 实现将数组转换成灰度图和彩图

yipeiwu_com6年前Python基础

主要步骤

1.生成普通python数组(bytearray(),os.urandom())

2.转换成numpy数组(numpy.array())

3.通过reshape将数组转换到所需的维数

4.以图像的形式显示出来(cv.imshow())

代码

import os
 
import cv2 as cv
import numpy as np
 
 
 
# Make an array of 120000 random bytes
randomByteArray = bytearray(os.urandom(120000))
# translate into numpy array
flatNumpyArray = np.array(randomByteArray)
# Convert the array to make a 400*300 grayscale image(灰度图像)
grayImage = flatNumpyArray.reshape(300, 400)
# show gray image
cv.imshow('GrayImage', grayImage)
# print image's array
print(grayImage)
cv.waitKey()
 
# byte array translate into RGB image
randomByteArray1 = bytearray(os.urandom(360000))
flatNumpyArray1 = np.array(randomByteArray1)
BGRimage = flatNumpyArray1.reshape(300,400,3)
cv.imshow('BGRimage', BGRimage)
cv.waitKey()
cv.destroyAllWindows()

效果

以上这篇python-OpenCV 实现将数组转换成灰度图和彩图就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python 将对象设置为可迭代的两种实现方法

1、实现 __getitem__(self) class Library(object): def __init__(self): self.value=['a','b'...

Python中模块与包有相同名字的处理方法

前言 在编程开发中,个人觉得,只要按照规范去做,很少会出问题。刚开始学习一门技术时,的确会遇到很多的坑。踩的坑多了,这是好事,会学到更多东西,也会越来越觉得按照规范做的重要性,规范的制定...

Python collections模块使用方法详解

Python collections模块使用方法详解

一、collections模块 1.函数namedtuple (1)作用:tuple类型,是一个可命名的tuple (2)格式:collections(列表名称,列表) (3)̴...

python给微信好友定时推送消息的示例

如下所示: from __future__ import unicode_literals from threading import Timer from wxpy import...

python3.4+pycharm 环境安装及使用方法

python3.4+pycharm 环境安装及使用方法

遇到很多初学者的盆友,来问python环境安装的问题。。因此,这篇文章就诞生了。。 因个人是windows的环境,所以本文只讲windows环境下的python安装。 作为初用pytho...