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

作者:li_il 时间:2023-07-22 11:22:19 

主要步骤

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 实现将数组转换成灰度图和彩图

来源:https://blog.csdn.net/li_l_il/article/details/83214114

标签:python,OpenCV,数组,灰度图,彩图
0
投稿

猜你喜欢

  • sqlserver 巧妙的自关联运用

    2012-07-21 14:55:12
  • 详解php处理大并发大流量大存储

    2023-07-21 13:11:02
  • 详解python学习笔记之解释器

    2023-09-15 15:28:26
  • Go语言使用select{}阻塞main函数介绍

    2024-04-30 10:06:32
  • python入门之井字棋小游戏

    2021-12-11 11:35:36
  • 在Python 的线程中运行协程的方法

    2021-03-10 18:06:38
  • python错误处理详解

    2023-04-24 11:10:30
  • Python基础必备之语法结构详解

    2023-12-07 05:29:06
  • python中使用pyhook实现键盘监控的例子

    2023-08-17 10:21:52
  • 在Linux命令行终端中使用python的简单方法(推荐)

    2021-10-27 23:29:21
  • mysql5存储过程编写实践

    2008-12-24 16:32:00
  • 解决seaborn在pycharm中绘图不出图的问题

    2023-11-29 02:22:52
  • Docker 安装 MySQL(8和5.7)

    2024-01-26 06:49:48
  • python3实现点餐系统

    2023-04-30 19:06:55
  • python动态参数用法实例分析

    2021-03-02 06:27:51
  • MySQL内外连接的具体使用

    2024-01-17 13:52:49
  • 谈谈图片如何影响转换率

    2011-08-10 19:14:08
  • sql语句优化之SQL Server(详细整理)

    2024-01-15 14:07:08
  • python实现对excel进行数据剔除操作实例

    2022-09-28 13:53:22
  • Python Opencv基于透视变换的图像矫正

    2022-03-13 16:47:14
  • asp之家 网络编程 m.aspxhome.com