import pandas as pd
from sqlalchemy import create_engine
from PIL import Image
import base64
from io import BytesIO
engine = create_engine('mysql+pymysql://root:111111@113.198.234.39/project', echo = False)
## 이미지 DB에 저장하는 code
buffer = BytesIO()
im = Image.open('test3.jpg')
im.save(buffer, format = 'jpeg')
img_str = base64.b64encode(buffer.getvalue())
img_df = pd.DataFrame({'carnumber': '999가1234', 'pic' :[img_str]})
img_df.to_sql('illegal_carnumber', con=engine, if_exists='append', index=False)
## DB에서 차량번호가 존재하는지 불러오는 code
img_read = pd.read_sql(sql="select EXISTS (select * FROM legal_carnumber WHERE carnumber='11가1234') as success;", con=engine)
img_str1 = img_read['success'].values[0]
#print(type(img_str1))
#print(img_str1)
if(img_str1 == 1):
print("DB에 존재하니 : ",True)
else:
print("DB에 존재하니 : ",False)
## DB에서 Python 이미지으로 불러오는 code
img_read1 = pd.read_sql(sql='select pic from illegal_carnumber', con=engine)
img_str2 = img_read1['pic'].values[0]
print(type(img_str2))
img = base64.decodestring(img_str2)
im = Image.open(BytesIO(img))
im.show()