python json文件传输图片
生活随笔
收集整理的這篇文章主要介紹了
python json文件传输图片
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
第一步、將圖片轉為str
image='1.jpg' print(type(image)) def imageToStr(image):with open(image,'rb') as f:image_byte=base64.b64encode(f.read())print(type(image_byte))image_str=image_byte.decode('ascii') #byte類型轉換為strprint(type(image_str))return image_str image1=imageToStr(image) print(type(image1))#輸出結果 <class 'str'> <class 'bytes'> <class 'str'> <class 'str'>第二步、存入json數據:
data = {"engineeringdata": {"date":12,"value": "59.3;98.5","image":image1} }?第三歩、將json中數據取出轉化為圖片:
def strToImage(str,filename):image_str= str.encode('ascii')image_byte = base64.b64decode(image_str)image_json = open(filename, 'wb')image_json.write(image_byte) #將圖片存到當前文件的fileimage文件中image_json.close()file_address = "./fileimage/" + data['engineeringdata']['date'] + r".jpg" strToImage(data['engineeringdata']['image'],file_address)?
總結
以上是生活随笔為你收集整理的python json文件传输图片的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python时间time模块介绍
- 下一篇: 【Python】Python简介和Pyt