简体   繁体   English

如何将字节转换为文本并返回到字节?

[英]How to convert bytes to text and back to bytes?

I want to convert a picture into bytes and place it in a text file and then open that text file and convert it to a picture again.我想将图片转换为字节并将其放入文本文件中,然后打开该文本文件并将其再次转换为图片。

png=open("C:\\Users\\myUser\\Desktop\\n.png","rb")
pngbytes=png.read()
newf=open("C:\\Users\\myUser\\Desktop\\newf.txt","w")
newf.write(str(pngbytes))
newf.close()
newf=open("C:\\Users\\myUser\\Desktop\\newf.txt","r")
newpng=open("C:\\Users\\myUser\\Desktop\\newpng.png","wb")
strNewf=newf.read()
newpng.write(strNewf.encode())
newpng.close()
png.close()
newf.close()

The image is created but can't be displayed.图像已创建但无法显示。

You can get your code to work by replacing strNewf.encode() with eval(strNewf) .您可以通过将strNewf.encode()替换为eval(strNewf)来使您的代码正常工作。

This works because the string you've created with str(pngbytes) gives you the string representation of the bytes, eval simply interprets that representation to give you the bytes again.这是有效的,因为您使用str(pngbytes)创建的字符串为您提供了字节的字符串表示,而eval只是简单地解释该表示以再次为您提供字节。

Why you'd want to do this is entirely unclear however - what are you trying to achieve?然而,你为什么要这样做是完全不清楚的——你想达到什么目的? Because it seems that there's better ways to go about it...因为似乎有更好的方法来解决它......

Here you have a full working example.这里有一个完整的工作示例。

This will: 1) Load an image file into memory.这将: 1) 将图像文件加载到内存中。 2) Convert the image into raw text. 2) 将图像转换为原始文本。 3) Save the image as text into a different file. 3) 将图像作为文本保存到不同的文件中。 4) Read the text file and convert it to the original image. 4) 读取文本文件并将其转换为原始图像。

import base64

# Open image as bytes.
with open("8000_loss.png", "rb") as f:
    png_bytes = f.read()

# Convert bytes to text.
type(png_bytes)  # bytes
png_str = base64.b64encode(png_bytes).decode()
type(png_str)  # string

# Save text to file.
with open("wolverine.txt", "w") as f:
    f.write(png_str)

# Read file as text.
with open("wolverine.txt", "r") as f:
    png_str2 = f.read()

# Convert text to bytes.
type(png_str2)  # string
png_bytes2 = base64.b64decode(png_str2.encode())
type(png_bytes2)  # bytes

# Validate the image has not been modified
assert png_bytes == png_bytes2

你不清楚内置函数'str'的结果

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

相关问题 如何将字节转换为二进制并返回字节 - how to convert bytes to binary and return back the bytes 如何将带有字节值的字符串转换回字节? - How to convert string with bytes value back to bytes? 如何将字符串表示形式的字节转换回字节? - how to convert string representation bytes back to bytes? 如何将字符串中的字节转换回字节 - How to convert bytes in a string back to bytes 如何将字节转换为字符串,然后将相同的字符串转换回字节 - How to convert Bytes to String and then convert the same string back to bytes 如何将这个简单的5个字节变回4个字节? (将4字节转换为5字节的算法是已知的) - How to turn this simple 5 bytes back into 4 bytes ? (algorithm to convert 4 bytes into 5 bytes is known) 如何将字节元组转换为整数并返回? - How to convert a tuple of bytes to an integer and back? 如何将字节转换回元组列表? - How to convert bytes back to list of tuples? 如何将Excel或类似文件(不仅是纯文本文件)转换为字节或二进制,然后再次返回 - How do I convert an Excel or similar (not just a plain text file) to bytes or binary then back again 如何将字节转换为 int 并返回到相同的字节? - How to cast bytes to an int and back to the same bytes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM