PysonDB에서 이미지를 인코딩하고 저장하는 방법.
오늘 뭐 할까:
전제 조건
pip install pysondb
시작하자.
아래와 같은 이미지가 있습니다.
file2son.png

우리는 그것을 인코딩 할 것입니다.
from pysondb import db
import base64
from PIL import Image
from io import BytesIO
x=db.getDb("fs.json")
def addimage(fi,name):
with open(fi,"rb") as dataimg:
raw_data=base64.b64encode(dataimg.read())
x.add({"data":raw_data.decode('utf-8'),"name":name})
addimage("file2son.png","image12")
x=db.getDb("fs.json")
def addimage(fi,name):
with open(fi,"rb") as dataimg:
raw_data=base64.b64encode(dataimg.read())
x.add({"data":raw_data.decode('utf-8'),"name":name})
addimage("file2son.png","image12")
raw_data=base64.b64encode(dataimg.read())
x.add({"data":raw_data.decode('utf-8'),"name":name})
마지막으로 fs.json 데이터베이스에 데이터가 있습니다.
이미지를 되찾자
def getimage(filename,name):
img_data=x.getBy({"name":name})
img=Image.open(BytesIO(base64.b64decode(img_data[0]['data2'])))
img.save(filename,"PNG")
getimage("file.png","image12")
img_data=x.getBy({"name":name})
img=Image.open(BytesIO(base64.b64decode(img_data[0]['data2'])))
img.save(filename,"PNG")
전체 코드
from pysondb import db
import base64
from PIL import Image
from io import BytesIO
x=db.getDb("fs.json")
def addimage(fi,name):
with open(fi,"rb") as data:
raw_data=base64.b64encode(data.read())
x.add({"data":raw_data.decode('utf-8'),"name":name})
addimage("file2son.png","image12")
# addimage() is used to add file2son.png image in database with name as image12
def getimage(filename,name):
img_data=x.getBy({"name":name})
img=img_data[0]['data2'].decode('base64'))
img.save(filename,"PNG")
getimage("file.png","image12")
# getimage is used to retrieve the stored image of name image12 and get it as file.png
오늘은 여기까지입니다. 더 많은 블로그를 팔로우하세요.
의심의 여지가 있거나 내가 틀린 곳이 있습니까? 여기에 의견을 말하십시오. 내가 고칠 수 있도록.
Reference
이 문제에 관하여(PysonDB에서 이미지를 인코딩하고 저장하는 방법.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/fredysomy/how-to-encode-and-store-images-in-pysondb-4hco텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)