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

g6baxovj  于 2022-12-27  发布在  Python
关注(0)|答案(1)|浏览(191)
    • 背景:**我建立了一个小型热成像相机,它可以保存70x70像素到SD卡。这些像素的颜色值范围从0到2^16。(实际上某些颜色,如黑色(值0)永远不会显示)。这个颜色的确定如下所述:c++ defined 16bit (high) color

我想用我的电脑用Python把这些数据转换成图像。
不幸的是,从另一个问题中收集到的一个例子并没有产生令人满意的结果:bad image
正如你所看到的,图像看起来不是很好。我的屏幕显示了这样的东西(注意,这两个例子不是同时捕获的):photo
白色框架不是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文件:Image Data
在这种情况下,31表示蓝色,高值表示更红。
谢谢你的帮助!

以下是关于我的项目的一些附加信息:

Arduino热成像摄像机,支持SD卡并具有图像保存功能,使用Panasonic生产的AMG8833热成像传感器:Datasheet
GitHub (Arduino and Python Code)
3D-printable case used by me and original Arduino code
Schematic with SD card added

nr7wwzry

nr7wwzry1#

我认为它应该是这样的:

#!/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')

相关问题