[英]Encoding an image file with base64
我想使用 base64 模塊將圖像編碼為字符串。 不過我遇到了一個問題。 如何指定要編碼的圖像? 我嘗試將目錄用於圖像,但這只會導致目錄被編碼。 我希望對實際的圖像文件進行編碼。
編輯
我試過這個片段:
with open("C:\Python26\seriph1.BMP", "rb") as f:
data12 = f.read()
UU = data12.encode("base64")
UUU = base64.b64decode(UU)
print UUU
self.image = ImageTk.PhotoImage(Image.open(UUU))
但我收到以下錯誤:
Traceback (most recent call last):
File "<string>", line 245, in run_nodebug
File "C:\Python26\GUI1.2.9.py", line 473, in <module>
app = simpleapp_tk(None)
File "C:\Python26\GUI1.2.9.py", line 14, in __init__
self.initialize()
File "C:\Python26\GUI1.2.9.py", line 431, in initialize
self.image = ImageTk.PhotoImage(Image.open(UUU))
File "C:\Python26\lib\site-packages\PIL\Image.py", line 1952, in open
fp = __builtin__.open(fp, "rb")
TypeError: file() argument 1 must be encoded string without NULL bytes, not str
我做錯了什么?
我不確定我是否理解你的問題。 我假設你正在做一些事情:
import base64
with open("yourfile.ext", "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
當然,您必須首先打開文件,然后讀取其內容——您不能簡單地將路徑傳遞給 encode 函數。
編輯:好的,這是您編輯原始問題后的更新。
首先,記住在 Windows 上使用路徑分隔符時使用原始字符串(字符串前綴為 'r'),以防止意外碰到轉義字符。 其次,PIL 的 Image.open 要么接受文件名,要么接受類文件(也就是說,該對象必須提供 read、seek 和 tell 方法)。
話雖如此,您可以使用 cStringIO 從內存緩沖區創建這樣的對象:
import cStringIO
import PIL.Image
# assume data contains your decoded image
file_like = cStringIO.StringIO(data)
img = PIL.Image.open(file_like)
img.show()
使用python 2.x,您可以使用.encode進行簡單編碼:
with open("path/to/file.png", "rb") as f:
data = f.read()
print data.encode("base64")
第一個答案將打印一個帶有前綴 b' 的字符串。 這意味着您的字符串將像這樣 b'your_string' 要解決此問題,請添加以下代碼行。
encoded_string= base64.b64encode(img_file.read())
print(encoded_string.decode('utf-8'))
import base64
from PIL import Image
from io import BytesIO
with open("image.jpg", "rb") as image_file:
data = base64.b64encode(image_file.read())
im = Image.open(BytesIO(base64.b64decode(data)))
im.save('image1.png', 'PNG')
借鑒Ivo van der Wijk和gnibbler之前開發的內容,這是一個動態解決方案
import cStringIO
import PIL.Image
image_data = None
def imagetopy(image, output_file):
with open(image, 'rb') as fin:
image_data = fin.read()
with open(output_file, 'w') as fout:
fout.write('image_data = '+ repr(image_data))
def pytoimage(pyfile):
pymodule = __import__(pyfile)
img = PIL.Image.open(cStringIO.StringIO(pymodule.image_data))
img.show()
if __name__ == '__main__':
imagetopy('spot.png', 'wishes.py')
pytoimage('wishes')
然后,您可以決定使用Cython編譯輸出圖像文件以使其更酷。 使用這種方法,您可以將所有圖形捆綁到一個模塊中。
正如我在您之前的問題中所說,不需要對字符串進行 base64 編碼,它只會使程序變慢。 只需使用repr
>>> with open("images/image.gif", "rb") as fin:
... image_data=fin.read()
...
>>> with open("image.py","wb") as fout:
... fout.write("image_data="+repr(image_data))
...
現在,圖像存儲為一個變量,名為image_data
在一個名為image.py
開始一個新的解釋,並導入IMAGE_DATA
>>> from image import image_data
>>>
它為我工作
import base64
import requests
# Getting image in bytes
response = requests.get("image_url")
# image encoding
encoded_image = base64.b64encode(response.content)
# image decoding and without it's won't work due to some '\xff' error
decoded_image= base64.b64decode(encoded_image)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.