Python 将图像转换为字符串以及反转
当我们谈论“将图像转换为字符串格式,然后在Python中反转”时,这指的是将图像文件转换为可以使用字符字符串存储和管理的内容,然后最终将其返回到其原始图像格式。
转换图像为字符串 - 将图像转换为字符串涉及将二进制数据转录为文本格式,以便于需要基于文本的图像的网络协议进行传输或存储。该转换方法可以在使用API、数据库或传输或存储图像所需的文本数据传输或存储时更有效地传输或存储图像。
生成的字符串通常由特定的编码方案(如base64编码)使用图像的二进制数据进行表示。该方法确保每个二进制信息都使用ASCII字符表示为易于作为字符串进行传输或存储。
将字符串转换为图像 - 将字符串转换回图像需要将编码字符串解码回其二进制数据表示,从而能够使用其编码表示重构图像文件。
对于这个例子,我们将转换Tutorialspoint的标识符的JPEG格式转换为字符串形式,并使用该字符串形式将其还原为图像格式。我们将使用以下图像作为输入文件:
方法1:Base64编码/解码
Base64编码是一种越来越流行的方法,用于将二进制数据(如图像)表示为字符串格式。Python提供了base64模块来使用该算法进行编码和解码。
将图像转换为字符串
下面的代码以二进制模式读取名为“tpt_logo.jpg”的图像文件,并使用base64.b64encode()函数对其内容进行编码,使用write()方法将其保存为名为encoded_image.txt的文本文件中的base64字符串形式 – 之后通过打印消息来确认将其存储在文本文件中以备将来使用或传输。本质上,此代码将图像转换为base64编码的字符串表示,然后保存到文本文件中以备将来使用或传输。
示例
import base64
# Read image file
with open('tpt_logo.jpg', 'rb') as image_file:
encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
# Save the encoded string in a text file
with open('encoded_image.txt', 'w') as file:
file.write(encoded_string)
print("Encoded string saved in 'encoded_image.txt'.")
输出
Encoded string saved in 'encoded_image.txt'.
将字符串转换回图像
下面的代码从”encoded_image.txt”中读取一个编码的字符串,使用base64.b64decode()解码它以获取其二进制数据,然后使用PIL图像库的Image.open()函数从这些字节创建一个图像对象,通过BytesIO提供文件类似的访问。最后,一个图像对象要么显示出来,要么展示出来,这样可以在屏幕上看到它;有效地解码、重建和可视化一个编码的图像字符串,最后在屏幕上显示出来以进行可视化。
示例
import base64
from io import BytesIO
from PIL import Image
# Read the encoded string from the text file
with open('encoded_image.txt', 'r') as file:
encoded_string = file.read()
# Decode the string
decoded_string = base64.b64decode(encoded_string)
# Create an image object from the decoded bytes
image = Image.open(BytesIO(decoded_string))
# Display or save the image
image.show()
输出
方法2:使用Zlib和Binascii
Zlib是一个Python压缩库,提供了使用DEFLATE算法进行数据压缩和解压缩的功能,具有对文件或网络传输进行有效处理的能力。此外,它对流(字符串/二进制数据)的压缩/解压缩能力在处理大量信息时非常有价值。
Python的binascii模块提供了将二进制数据转换为基于文本的协议或格式的功能,使数据可读性强且易于传输;通过使用十六进制、Base64或Quoted Printable表示等文本协议或格式,使数据易读性或传输性强。当处理需要将二进制数据转换为文本格式进行传输或存储的大型数据集时,通常会使用Binascii。
将图像转换为字符串
在下面的代码中,我们根据上面提到的(Tutorials point logo)读取一个图像文件,并使用zlib库压缩其数据。然后,使用binascii库将压缩的数据转换为十六进制字符串,然后存储在名为“compressed_image.txt”的文本文件中。
示例
import zlib
import binascii
# Read image file
with open('tpt_logo.jpg', 'rb') as image_file:
image_data = image_file.read()
# Compress the image data using zlib.
compressed_data = zlib.compress(image_data)
# Convert the compressed data to a hexadecimal string.
hex_string = binascii.hexlify(compressed_data).decode('utf-8')
# Save the hexadecimal string in a text file.
with open('compressed_image.txt', 'w') as file:
file.write(hex_string)
print("Hexadecimal string saved in 'compressed_image.txt'.")
输出
Hexadecimal string saved in 'compressed_image.txt'.
转换字符串为图像
下面的代码从”compressed_image.txt”文件中读取十六进制字符串。使用binascii.unhexlify()将字符串转换为压缩数据。然后,使用zlib.decompress()进行数据解压缩。解压后的数据以二进制模式(‘wb’)写入新文件”reconstructed_image.jpg”。输出表明重构图像已成功保存。
注意 − 这里的图像重构代码将结果保存在相同的文件夹中,不直接向用户显示;而是我们手动打开并在下面作为输出显示。
示例
import zlib
import binascii
# Read the hexadecimal string from the text file.
with open('compressed_image.txt', 'r') as file:
hex_string = file.read()
# Convert the hexadecimal string to compressed data.
compressed_data = binascii.unhexlify(hex_string)
# Decompress the data using zlib
decompressed_data = zlib.decompress(compressed_data)
# Create a new file and write the decompressed data to it.
with open('reconstructed_image.jpg', 'wb') as image_file:
image_file.write(decompressed_data)
print("Reconstructed image saved as 'reconstructed_image.jpg'.")
输出
Reconstructed image saved as 'reconstructed_image.jpg'.
结论
在Python中,有不同的方式将图像转换为字符串,主要包括base64编码/解码以及使用zlib和binascii进行压缩将二进制表示转换为文本表示。我们在本教程中讨论了这两种方法。无论选择哪种方法,最终都能为我们提供更高效地存储、传输和处理图像数据的选项。