如何将字符串字节转换为PNG文件?
How to convert string bytes to a PNG file?
我正在尝试将 SVG 转换为 PNG。我正在使用名为 cloudmersive 的服务。您可以使用它将一种图像格式(例如 SVG)转换为 PNG。
问题是我不知道如何使用 API returns,这是一个像这样的字符串字节:
'b\'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00d\x00\x00\x00d\x08\x03\x00\x00\x00G<ef\x00\x00\x00\x04gAMA\x00\x00\xb1\x8f\x0b\xfca\x05\x00\x00\x00 cHRM\x00\x00z&\x00\x00\x80\x84\x00\x00\xfa\x00\x00\x00\x80\xe8\x00\x00u0\x00\x00\xea`\x00\x00:\x98\x00\x00\x17p\x9c\xbaQ<\x00\x00\x01\xf5PLTE\xff\xff\xff\xff\xf9\xf7\xff\xeb\xe3\xff\xdd\xcf\xff\xcf\xbc\xff\xc8\xb2\xff\xc4\xac\xff\xc0\xa6\xff\xc5\xae\xff\xca\xb4\xff\xdd\xd0\xff\xed\xe6\......\xaeB`\x82\''
我不知道如何将其写入 PNG 文件。
您需要对其调用eval
,然后将其写入文件
string = r"b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x10\x00\x00\x00\x00j\xeeG\x16\x00\x00\x00\x0bIDATx\x9cc``\x00\x00\x00\x03\x00\x01\xb8\xad:c\x00\x00\x00\x00IEND\xaeB`\x82'"
with open("image.png","wb") as file:
file.write(eval(string))
我正在尝试将 SVG 转换为 PNG。我正在使用名为 cloudmersive 的服务。您可以使用它将一种图像格式(例如 SVG)转换为 PNG。
问题是我不知道如何使用 API returns,这是一个像这样的字符串字节:
'b\'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00d\x00\x00\x00d\x08\x03\x00\x00\x00G<ef\x00\x00\x00\x04gAMA\x00\x00\xb1\x8f\x0b\xfca\x05\x00\x00\x00 cHRM\x00\x00z&\x00\x00\x80\x84\x00\x00\xfa\x00\x00\x00\x80\xe8\x00\x00u0\x00\x00\xea`\x00\x00:\x98\x00\x00\x17p\x9c\xbaQ<\x00\x00\x01\xf5PLTE\xff\xff\xff\xff\xf9\xf7\xff\xeb\xe3\xff\xdd\xcf\xff\xcf\xbc\xff\xc8\xb2\xff\xc4\xac\xff\xc0\xa6\xff\xc5\xae\xff\xca\xb4\xff\xdd\xd0\xff\xed\xe6\......\xaeB`\x82\''
我不知道如何将其写入 PNG 文件。
您需要对其调用eval
,然后将其写入文件
string = r"b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x10\x00\x00\x00\x00j\xeeG\x16\x00\x00\x00\x0bIDATx\x9cc``\x00\x00\x00\x03\x00\x01\xb8\xad:c\x00\x00\x00\x00IEND\xaeB`\x82'"
with open("image.png","wb") as file:
file.write(eval(string))