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 按字典dict的键排序,并取出相应的键值放于list中的实例

方法一: def dict_to_numpy_method1(dict): dict_sorted=sorted(dict.iteritems(), key=lambda d:d[...

python+mysql实现学生信息查询系统

python+mysql实现学生信息查询系统

本文实例为大家分享了python mysql学生信息查询系统的具体代码,供大家参考,具体内容如下 import pymysql #import redis #pool = redis...

在Linux系统上通过uWSGI配置Nginx+Python环境的教程

1.安装ubuntu有uwsgi的ppa: add-apt-repository ppa:stevecrozz/ppa apt-get update apt-get instal...

如何利用Python分析出微信朋友男女统计图

如何利用Python分析出微信朋友男女统计图

写在前面 现在人人都有微信,一句“咱们加个微信呗”搭载了你我之间的友谊桥梁,浑然不知自己的微信朋友已经四五百了,甚至上千,几千的都有;然而那个是那个,谁是谁,是男是女都分不清楚了,今天...

Python中音频处理库pydub的使用教程

前言 pydub是Python中用户处理音频文件的一个库。本文主要介绍了关于Python音频处理库pydub使用的相关内容,分享出来供大家参考学习,下面来看看详细的介绍: 安装: &n...