如何在python中将十六进制字符串转换为彩色图像? [英] How to convert hex string to color image in python?
问题描述
我是编程的新手,所以我对将字符串转换为彩色图像有一些疑问.
i'm new in programming so i have some question about converting string to color image.
我有一个数据,它由十六进制字符串组成,就像fff2f3 ..... 我想像这样将文件转换为png.
i have one data , it consists of Hex String, like a fff2f3..... i want to convert this file to png like this.
我可以通过此站点将十六进制数据转换为png图像.
但我不知道如何使用python代码将十六进制数据转换为png图像
但我尝试使用Image.frombytes('RGB',(1600,1059),hex_str)
但
我不知道图像大小,所以我无法使用此方法.
i can convert the hex data to png image through this site
but i don't know how to convert the hex data to png image using python code
but i tried to use Image.frombytes('RGB',(1600,1059),hex_str)
but
i don't know image size , so i cannot use this method.
所以我的问题是我该如何将十六进制数据转换为图像使用python代码
so My question is how can i convert this hex data to image using python code
请给我一些建议,谢谢:)
please give me some advise , thank you :)
推荐答案
可以将十六进制字符串读入bytes对象,然后将该二进制文件写入.png
文件中,如下所示:
Reading the hexadecimal string into a bytes object, and then writing that binary into a .png
file can be done like this:
with open('binary_file') as file:
data = file.read()
data = bytes.fromhex(data[2:])
with open('image.png', 'wb') as file:
file.write(data)
并产生此结果,请记住它已损坏:
And produces this result, keep in mind it is corrupted:
这篇关于如何在python中将十六进制字符串转换为彩色图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!