[英]How do I convert a csv file (16bit (high) color) to image in Python?
Context: I built a small thermal camera which can save 70x70 pixels to an SD card.背景:我构建了一个小型热像仪,可以将 70x70 像素保存到 SD 卡中。 These pixels have a color value ranging from 0 to 2^16.
这些像素的颜色值范围从 0 到 2^16。 (Actually certain colors, like black (value 0) are never displayed).
(实际上某些颜色,例如黑色(值 0)永远不会显示)。 This color is determined like explained here: c++ defined 16bit (high) color
这种颜色的确定就像这里解释的那样: c++定义的16位(高)颜色
I want to convert this data into an Image with my computer using Python.我想用我的电脑使用 Python 将此数据转换为图像。
An example gathered from another question unfortunately doesn't yield satisfying results: bad image不幸的是,从另一个问题收集的示例并没有产生令人满意的结果:坏图像
As you can see, the image doesn't look very good.如您所见,图像看起来不太好。 My screen shows something like this (note that these two examples were not captured at the same time): photo
我的屏幕显示如下(请注意,这两个示例不是同时捕获的):照片
The white frame is not part of the csv-file.白框不是 csv 文件的一部分。
This is the code I have used to generate the image: I have experimented with the color_max
value but didn't get good results.这是我用来生成图像的代码:我已经尝试了
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')
This is the csv-file: Image Data这是 csv 文件: 图像数据
31 represents blue in this case, high values are more red.在这种情况下,31 代表蓝色,高值更红。
Thanks for any help!谢谢你的帮助!
Arduino Thermal Camera with SD card support and image saving capability using the AMG8833 Thermal Imaging sensor made by Panasonic: Datasheet带有 SD 卡支持和图像保存功能的 Arduino 热像仪,使用松下制造的 AMG8833 热成像传感器: 数据表
GitHub (Arduino and Python Code) GitHub(Arduino 和 Python 代码)
3D-printable case used by me and original Arduino code我使用的 3D 可打印外壳和原始 Arduino 代码
I think it should look like this:我认为它应该是这样的:
#!/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')
Keywords: Python, numpy, image, image processing, thermal camera, Panasonic, AMG8833, RGB565, unpack, pack, Arduino.关键词: Python、numpy、图像、图像处理、热像仪、松下、AMG8833、RGB565、解包、打包、Arduino。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.