当前位置:首页 » python教程 » 正文

python 利用 PIL 将数组值转成图片的实现

看: 760次  时间:2021-04-28  分类 : python教程

python PIL 将数组值转成图片

安装 PIL 包

pip install pillow

将二维数据转换成单通道图片

from PIL import Image
arr=numpy.asarray([[1,2,3,4,5],[2,3,4,5,6],[7,8,9,0,12]])
image = Image.fromarray(arr).convert("L")# L为模式
image.save("out.jpg")#输出图片格式可以自己选择

1 -> 1位像素,黑和白,存成8位的像素
L -> 8位像素,黑白
P -> 8位像素,使用调色板映射到任何其他模式
RGB -> 3×8位像素,真彩
RGBA -> 4×8位像素,真彩+透明通道
CMYK -> 4×8位像素,颜色隔离
YCbCr -> 3×8位像素,彩色视频格式
I -> 32位整型像素
F -> 32位浮点型像素

将三维数据转换成RGB图片

from PIL import Image

a=numpy.asarray([[1,2,3,4,5],[2,3,4,5,6],[7,8,9,0,12]])
b=numpy.asarray([[1,2,3,4,5],[2,3,4,5,6],[7,8,9,0,12]])
c=numpy.asarray([[1,2,3,4,5],[2,3,4,5,6],[7,8,9,0,12]])

r = Image.fromarray(a).convert('L')
g = Image.fromarray(b).convert('L')
b = Image.fromarray(c).convert('L')

image = Image.merge('RGB',(r,g,b))
image.save("out.jpg")

读取h5数据

h5py安装

conda install h5py

数据读取

示例文件:

在这里插入图片描述

在这里插入图片描述

import h5py
openFileName = h5py.File(fileName)
EASE_column_index_1km=openFileName['Soil_Moisture_Retrieval_Data_1km'['EASE_column_index_1km'][:]#得到Dataset的数据
b=openFileName['a']['rangeEndingDateTime'].value#得到rangeEndingDateTime里面的文本值

读取hdf数据

pyhdf 安装

conda install -c conda-forge pyhdf 

数据读取

示例文件:

在这里插入图片描述

from pyhdf.SD import SD, SDC
openFileName = SD(filename, SDC.READ)
a = j.select('Night_view_angl')[:]#获得Night_view_angl  Dataset的值

到此这篇关于python 利用 PIL 将数组值转成图片的实现的文章就介绍到这了,更多相关python PIL 将数组值转成图片内容请搜索python博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持python博客!

标签:numpy  

<< 上一篇 下一篇 >>

搜索

推荐资源

  Powered By python教程网   鲁ICP备18013710号
python博客 - 小白学python最友好的网站!