Python implementation will save the picture in binary format to a MySQL database, and remove:

Creating a database table:

CREATE TABLE photo ( photo_no int(6) unsigned NOT NULL auto_increment, image MEDIUMBLOB, PRIMARY KEY (`photo_no`) );

Python implementation will save the picture to the MySQL database in binary format:

import sys
import pymysql
from PIL import Image
import os
 
path = "./"

fp = open("./1.png", 'rb')
img = fp.read()
fp.close()

database = pymysql.connect(host="localhost", user="root", passwd="", db="hua")
cursor = database.cursor()
sql = "INSERT INTO photo (image) VALUES  (%s);"
args = (img)
cursor.execute(sql, args)
database.commit()
cursor.close()
database.close()

print("============")
print("Done! ")

Python implementation will be saved from the picture MySQL database to a local binary format:

import pymysql as mdb
import sys

conn = mdb.connect(host='localhost',user='root',passwd='',db='hua')
cursor = conn.cursor()
cursor.execute("SELECT image FROM photo LIMIT 1")
fout = open('quchu1.png','wb')
fout.write(cursor.fetchone()[0])
fout.close()
cursor.close()
conn.close()

Guess you like

Origin www.cnblogs.com/ming-4/p/11869825.html