-
2021-08-10 10:55:10
import numpy as np import cv2 # 如果没有安装 cv2 模块,可以使用命令 # pip install opencv-python==4.4.0.46 来安装 # 其中 4.4.0.46 是 Opencv 的版本 listdata = [[1, 0, 1], [0, 1, 0], [0, 0, 1]] image_arr = np.array(listdata) cv2.imwrite("filename.png", image_arr)
更多相关内容 -
如何在python中将字节数组转换为图像
2021-07-16 16:09:20I am trying to convert raw data of length 64372(w= 242, h=266) to the image, the raw data is in the format of the byte array, I want to convert this byte array into image, can you help me with that?...I am trying to convert raw data of length 64372(w= 242, h=266) to the image, the raw data is in the format of the byte array, I want to convert this byte array into image, can you help me with that?
解决方案
You can generate a bytearray of dummy data representing a gradient like this:
import numpy as np
# Generate a left-right gradient 242 px wide by 266 pixels tall
ba = bytearray((np.arange(242) + np.zeros((266,1))).astype(np.uint8))
For reference, that array will contain data like this:
array([[ 0., 1., 2., ..., 239., 240., 241.],
[ 0., 1., 2., ..., 239., 240., 241.],
[ 0., 1., 2., ..., 239., 240., 241.],
...,
[ 0., 1., 2., ..., 239., 240., 241.],
[ 0., 1., 2., ..., 239., 240., 241.],
[ 0., 1., 2., ..., 239., 240., 241.]])
And then make into a PIL/Pillow image like this:
from PIL import Image
# Convert bytearray "ba" to PIL Image, 'L' just means greyscale/lightness
im = Image.frombuffer('L', (242,266), ba, 'raw', 'L', 0, 1)
Then you can save the image like this:
im.save('result.png')
Documentation is here.
-
Python 实现将数组/矩阵转换成Image类
2020-09-18 03:24:48今天小编就为大家分享一篇Python 实现将数组/矩阵转换成Image类,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧 -
Python实现二维数组输出为图片
2020-09-20 16:04:31下面小编就为大家分享一篇Python实现二维数组输出为图片,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧 -
python中将数组转化成列表
2019-11-20 15:03:591、创建一个数组,如下图 2、查看类型,如下图 3、转化成list 4、查看转化后的类型 5、完整代码如图1、创建一个数组,如下图
2、查看类型,如下图
3、转化成list
4、查看转化后的类型
5、完整代码如图
-
python图片数组格式转化
2021-01-12 00:54:082014-10-12 15:50 10422人阅读 评论(0) 收藏 举报 分类:python(1)computer vision(5)Conversions between Various FormatsConvert between Python tuple and lista = (1, 2) # a is a tuple b = list(a) # b is a ...2014-10-12 15:50 10422人阅读 评论(0) 收藏 举报
分类:
python(1)
computer vision(5)
Conversions between Various Formats
Convert between Python tuple and lista = (1, 2) # a is a tuple b = list(a) # b is a list c = tuple(b) # c is a tuple Convert between Python tuple, list and NumPy 1D array a = (1, 2) # a is a tuple b = np.array(a) # b is an numpy array c = tuple(b) # c is a tuple a = [1, 2] # a is a python array b = np.array(a) # b is a numpy array c = list(b) # c is a python list Convert between NumPy 2D array and NumPy matrixa = numpy.ndarray([2,3]) # create 2x3 arraym1 = numpy.asmatrix(a) # does not create new matrix, m1 refers to the same memory as a m2 = numpy.matrix(a) # creates new matrix and copies content b1 = numpy.asarray(m2) # does not create array, b1 refers to the same memory as m2b2 = numpy.array(m2) # creates new array and copies content
Read/Write Images with OpenCVimage = cv.LoadImage(“ponzo.jpg”)cv.SaveImage(“out.png”, image)Read/Write Images with PILimage = Image.open(“ponzo.jpg”)image.save(“out.jpg”)Read/Write Images with PyOpenCV****mat = pyopencv.imread(“ponzo.jpg”)pyopencv.imwrite(“out.png”, mat)
Convert between OpenCV image and PIL image****# color image cimg = cv.LoadImage("ponzo.jpg", cv.CV_LOAD_IMAGE_COLOR) # cimg is a OpenCV imagepimg = Image.fromstring("RGB", cv.GetSize(cimg), cimg.tostring()) # pimg is a PIL image cimg2 = cv.CreateImageHeader(pimg.size, cv.IPL_DEPTH_8U, 3) # cimg2 is a OpenCV image cv.SetData(cimg2, pimg.tostring())Note: OpenCV stores color image in BGR format. So, the converted PIL image is also in BGR-format. The standard PIL image is stored in RGB format. # gray image cimg = cv.LoadImage("ponzo.jpg", cv.CV_LOAD_IMAGE_GRAYSCALE) # cimg is a OpenCV image pimg = Image.fromstring("L", cv.GetSize(cimg), cimg.tostring()) # pimg is a PIL image cimg2 = cv.CreateImageHeader(pimg.size, cv.IPL_DEPTH_8U, 1) # cimg2 is a OpenCV imagecv.SetData(cimg2, pimg.tostring())
Convert between PIL image and NumPy ndarrayimage = Image.open(“ponzo.jpg”) # image is a PIL image array = numpy.array(image) # array is a numpy array image2 = Image.fromarray(array) # image2 is a PIL image Convert between PIL image and PyOpenCV matriximage = Image.open(“ponzo.jpg”) # image is a PIL imagemat = pyopencv.Mat.from_pil_image(image) # mat is a PyOpenCV matrix image2 = mat.to_pil_image() # image2 is a PIL image
Convert between OpenCV image and NumPy ndarraycimg = cv.LoadImage("ponzo.jpg", cv.CV_LOAD_IMAGE_COLOR) # cimg is a OpenCV image pimg = Image.fromstring("RGB", cv.GetSize(cimg), cimg.tostring()) # pimg is a PIL image array = numpy.array(pimg) # array is a numpy array pimg2 = cv.fromarray(array) # pimg2 is a OpenCV image
-
python图片与数组的转化
2018-06-08 09:05:14自己也是个小白,也在学习的过程,希望大家多多指点。 -
python入门指南(十二)python将图片转化为数组
2020-12-29 17:55:32交流qq群:6515873171、转化为数组-*- coding: utf-8 -*-from PIL import Imagefrom pylab import *#读取图片并转为数组im = array(Image.open("C:\\Users\\Administrator\\Desktop\\22.png"))#输出数组的各维度长度... -
python 将 Numpy 数组保存为图像 - imagio
2021-09-17 19:32:19python 将 Numpy 数组保存为图像 - imagio: import numpy as np import imageio if __name__ == '__main__': np_array = np.zeros((512, 512, 3), dtype=np.uint8) np_array[200:300, 150:200, :] = 255 ... -
python 实现将Numpy数组保存为图像
2020-12-23 10:48:57如果数据应该是精确的灰度级或准确的RGB通道,则解决方案为: import scipy.misc misc.toimage(image_array, cmin=0.0, cmax=...).save('outfile.jpg') 第二种方案 使用PIL。 给定一个numpy数组”A”: from PIL ... -
python将数组转换成csv文件
2021-11-05 23:43:01利用pandas把numpy数组保存为csv文件 1.利用numpy库创建数组 import numpy as np import pandas as pd from numpy.matlib import repmat arra = np.random.randint(1...2.利用pandas将创建好的数组转化为csv文件 .. -
Python数据处理——将数组转化为字符串
2022-05-09 10:37:07简单方法将数组转化为字符串 -
python中数组怎么转换为字符串
2021-01-06 11:26:111、数组转字符串 #方法1 arr = ['a','b'] str1 = ''.join(arr) #方法2 arr = [1,2,3] #str = ''.join(str(i) for i in arr)#此处str命名与str函数冲突! str2 = ''.join(str(i) for i in arr) 2、字符串... -
python numpy 一维数组转变为多维数组的实例
2020-12-23 21:44:44如下所示: import numpy ... 您可能感兴趣的文章:python二维列表一维列表的互相转换实例Python的numpy库中将矩阵转换为列表等函数的方法python的dataframe转换为多维矩阵的方法Python嵌套列表转一维的方法(压 -
在Python中将数组(numpy数据类型)转换为元组(本地数据类型)
2021-01-29 18:46:441., 1., 1.]), array([ 1., 1., 1.])] In [212]: tuple(np.ones((2,3))) Out[212]: (array([ 1., 1., 1.]), array([ 1., 1., 1.])) 没有totuple()方法,也没有任何东西可以快速而简单地将列表的嵌套列表转换为元组的... -
python 将有序数组转换为二叉树的方法
2020-09-19 12:20:56主要介绍了python 将有序数组转换为二叉树的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 -
在python中将数组的字符串表示形式转换为numpy数组
2021-07-16 13:10:31I can convert a string representation of a list to a list with ast.literal_eval. Is there an equivalent for a numpy array?x = arange(4)xs = str(x)xs'[0 1 2 3]'# how do I convert xs back to an arrayUsi... -
python数组变成字符串_在python中将数组的字符串表示形式转换为numpy数组
2020-11-26 11:03:25I can convert a string representation of a list to a list with ast.literal_eval. Is there an equivalent for a numpy array?x = arange(4)xs = str(x)xs'[0 1 2 3]'# how do I convert xs back to an arrayUsi... -
Python | Numpy数组图像转换
2021-04-27 07:20:07您可以将转换矢量化,以便所有R、G和B像素同时变换:def yuv_vec(images):R, G, B = images[:, :, :, 0], images[:, :, :, 1], images[:, :, :, 2]y = (0.299 * R + 0.587 * G + 0.114 * B) / 127.5 - 1u = (0.493 *... -
Python将byte数组转换为int
2020-12-10 13:25:15代码import structimport ctypesdef test_struct(buf, offset):return struct.unpack_from("I", buf, offset)[0]def test_ctypes(buf, offset):return ctypes.c_uint32.from_buffer(buf, offset).valuedef test_... -
Python将数组保存为mat文件
2021-08-10 10:43:43import scipy.io as io trainHist, valHist = training....# 则要先转换为numpy array # data = np.array(data) mat_path = '../losssave/train_loss_N2V.mat' io.savemat(mat_path, {'train_loss': trainHist}). -
python将数组数据转化成0-255的ascii字符串
2022-05-25 11:36:11数据类型转化 ascii -
Python实现字符串与数组相互转换功能示例
2020-12-26 04:53:57本文实例讲述了Python实现字符串与数组相互转换功能。分享给大家供大家参考,具体如下: 字符串转数组 str = '1,2,3' arr = str.split(',') print a 运行结果: 数组转字符串 #方法1 arr = ['a','b'] str1 = ','... -
python opencv数组转图片 并显示
2020-11-04 16:38:47pred是数组类型,先转为8位。 pred = np.array(pred, np.uint8) cv2.imshow(“123”, pred) cv2.waitKey(0) -
Python将多维数组转换为一维
2020-11-04 11:14:53Python将二维数组/多维数组转换为一维 分类专栏:Python文章标签:二维数组转一维 Python将二维数组/多维数组转换为一维 方法1:flatten 方法2:reshape+concatenate 方法3:sum() 方法4:列表推导式 方法5:... -
python 字符串数组转换为浮点数_Numpy将数组从浮点转换为字符串
2021-01-17 19:14:03如果有一个numbers数组,并且需要一个strings数组,则可以编写:strings = ["%.2f" % number for number in numbers]如果您的数字是浮点数,则数组将是一个与带两个小数的字符串具有相同数字的数组。>>> a ...