python 将图片转换为base64编码转储进数据库,,# _*_ codi
python 将图片转换为base64编码转储进数据库,,# _*_ codi
# _*_ coding: utf-8 _*_
#中间件:拓展工具遍历文件夹,对文件夹图片进行base64编码,写入数据库,并且读取查找方法
__author__ = ‘wf15038‘
__date__ = "2018/8/28 18:38"
import pymysql
import sys
from datetime import datetime
import base64
import os
now = datetime.now().strftime(‘%Y-%m-%d %H:%M:%S‘)
def dbinfo():
conn = ""
conn = pymysql.connect(host=‘ip地址‘, port=3505, user="数据库", password=‘密码‘, database=‘itportal‘,
charset=‘utf8‘,cursorclass =pymysql.cursors.DictCursor)
cur = conn.cursor()
if not cur:
return "access db is fail!"
else:
return conn
local_dir = ‘E:\\xiugai\\ico‘
try:
for root,dirs,files in os.walk(local_dir):
for filepath in files:
image_path = os.path.join(local_dir,filepath)
imagename,_ = os.path.splitext(filepath)
print(imagename)
fp = open(image_path,‘rb‘)
base64_date = base64.b64encode(fp.read())
fp.close()
try:
conn=dbinfo()
conncur = conn.cursor()
sql_insertimage="insert into soft_PT (softname,imstore,submittime) VALUE (%s, %s, %s) "
conncur.execute(sql_insertimage, (imagename,base64_date,now))
seatdic= conncur.fetchall()
conn.commit()
conn.close()
except pymysql.Error as e :
print("Error %d %s" % (e.args[0],e.args[1]))
sys.exit(1)
except IOError as e:
print("Error %d %s" % (e.args[0],e.args[1]))
sys.exit(1)
try:
conn = dbinfo()
conncur = conn.cursor()
sql_selectimage = "select imstore from soft_PT"
conncur.execute(sql_selectimage)
softpath = conncur.fetchall()
softpathlist = [x[‘imstore‘].decode(‘gbk‘) for x in softpath]
print(softpathlist)
for i in softpathlist:
print(i)
conn.commit()
conn.close()
except pymysql.Error as e :
print(e)
sys.exit(1)
python 将图片转换为base64编码转储进数据库
相关内容
- 如何查找python安装包的路径site-packages?,,使用命令:
- python+selenium四:iframe查看、定位、切换,,1.查看iframe
- Python基础(二)之元组及字典,python基础字典,元组:以
- 基于python3.5的 rsa+base64加解密,python3.5base64,python3.5.
- Python【2】:初入python 用户输入,if,(while 循环),
- Python Deque 模块,,Deque模块是Py
- Python - 多元组(tuple),pythontuple,声明一个多元组(4,
- Python3集成安装xadmin,,Python3集成安
- python面试题——框架和其他(132题),python132,1、djan
- 分享 《利用Python进行数据分析(第二版)》高清中文版
评论关闭