Python 中影像到字串的轉換及反向轉換
當我們談論“在 Python 中將影像轉換為字串格式並轉換回來”時,指的是將影像檔案轉換為可以使用字串儲存和管理的內容,然後最終將其轉換回原始影像格式。
將影像轉換為字串 − 將影像轉換為字串涉及將它們的二進位制資料轉換為文字格式,以便透過需要基於文字的影像的網路協議進行傳輸或儲存。當使用 API、資料庫或透過需要文字資料傳輸或儲存的網路協議傳送影像時,此轉換方法可以幫助我們更有效地傳輸或儲存影像。
生成的字串通常由使用特定編碼方案(例如 base64 編碼)表示的影像二進位制資料的表示組成。此方法確保每一段二進位制資訊都使用 ASCII 字元表示,以便輕鬆地作為字串進行傳輸或儲存。
將字串轉換為影像 − 將字串轉換回影像需要將編碼的字串解碼回它們的二進位制資料表示,使我們能夠使用它們的編碼表示來重建影像檔案。
在本練習中,我們將使用 JPEG 格式的 Tutorialspoint 徽標將其轉換為字串形式,然後使用該字串形式將其恢復為影像格式。我們將使用以下影像作為輸入檔案

方法 1:Base64 編碼/解碼
Base64 編碼是一種越來越流行的將二進位制資料(如影像)表示為字串格式的方法。Python 提供其 base64 模組來使用此演算法進行編碼和解碼。
將影像轉換為字串
以下程式碼以二進位制模式讀取名為“tpt_logo.jpg”的影像檔案,並使用 base64.b64encode() 函式將其內容編碼為 base64 字串,然後使用 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 或可列印引號表示)使資料易於閱讀或傳輸。在處理需要在傳輸或儲存過程中轉換為文字格式的二進位制資料時,處理大型資料集時,通常會使用 Binascii,這些資料集需要使用基於文字的協議或格式進行儲存或傳輸。
將影像轉換為字串
在下面的程式碼中,我們讀取上面提到的影像檔案(Tutorials point 徽標)並使用 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 進行壓縮以進行二進位制表示到文字表示的轉換。我們在本教程中討論了這兩種方法。無論採用哪種方法,最終它們都為我們提供了更有效地儲存、傳輸和處理影像資料的選擇。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP