繁体   English   中英

如何在 Python 中将 csv 文件(16 位(高)颜色)转换为图像?

[英]How do I convert a csv file (16bit (high) color) to image in Python?

背景:我构建了一个小型热像仪,可以将 70x70 像素保存到 SD 卡中。 这些像素的颜色值范围从 0 到 2^16。 (实际上某些颜色,例如黑色(值 0)永远不会显示)。 这种颜色的确定就像这里解释的那样: c++定义的16位(高)颜色

我想用我的电脑使用 Python 将此数据转换为图像。

不幸的是,从另一个问题收集的示例并没有产生令人满意的结果:坏图像

如您所见,图像看起来不太好。 我的屏幕显示如下(请注意,这两个示例不是同时捕获的):照片

白框不是 csv 文件的一部分。

这是我用来生成图像的代码:我已经尝试了color_max值,但没有得到好的结果。

#Python CSV to Image converter
#pip2 install cImage
#pip2 install numpy

from PIL import Image, ImageDraw
from numpy import genfromtxt

color_max = 256
#original 256

g = open('IMAGE_25.TXT','r')
temp = genfromtxt(g, delimiter = ',')
im = Image.fromarray(temp).convert('RGB')
pix = im.load()
rows, cols = im.size
for x in range(cols):
    for y in range(rows):
        #print str(x) + " " + str(y)
        pix[x,y] = (int(temp[y,x] // color_max // color_max % color_max),int(temp[y,x] // color_max  % color_max),int(temp[y,x] % color_max))
im.save(g.name[0:-4] + '.jpeg')

这是 csv 文件: 图像数据

在这种情况下,31 代表蓝色,高值更红。

谢谢你的帮助!


以下是有关我的项目的一些其他信息:

带有 SD 卡支持和图像保存功能的 Arduino 热像仪,使用松下制造的 AMG8833 热成像传感器: 数据表

GitHub(Arduino 和 Python 代码)

我使用的 3D 可打印外壳和原始 Arduino 代码

添加了 SD 卡的示意图

我认为它应该是这样的:

#!/usr/local/bin/python3
import numpy as np
from PIL import Image

# Read 16-bit RGB565 image into array of uint16
with open('IMAGE_25.TXT','r') as f:
    rgb565array = np.genfromtxt(f, delimiter = ',').astype(np.uint16)

# Pick up image dimensions
h, w = rgb565array.shape

# Make a numpy array of matching shape, but allowing for 8-bit/channel for R, G and B
rgb888array = np.zeros([h,w,3], dtype=np.uint8)

for row in range(h):
    for col in range(w):
        # Pick up rgb565 value and split into rgb888
        rgb565 = rgb565array[row,col]
        r = ((rgb565 >> 11 ) & 0x1f ) << 3
        g = ((rgb565 >> 5  ) & 0x3f ) << 2
        b = ((rgb565       ) & 0x1f ) << 3
        # Populate result array
        rgb888array[row,col]=r,g,b

# Save result as PNG
Image.fromarray(rgb888array).save('result.png')

在此处输入图片说明

关键词: Python、numpy、图像、图像处理、热像仪、松下、AMG8833、RGB565、解包、打包、Arduino。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM