1、Pillow 图像处理模块
安装:pip install pillow
#引入依赖库
from __future__ import print_function
import os, sys
from PIL import Image
#读入图片文件,成功返回一个image object
im = Image.open("logo.png")
#print(im.format, im.size, im.mode)
#im.show() 调用外部程序打开图片
#将图片转换为jpg格式
#python py.py logo.jpg
for infile in sys.argv[1:]:
f, e = os.path.splitext(infile)
outfile = f + ".jpg"
if infile != outfile:
try:
Image.open(infile).save(outfile)
except IOError:
print("cannot convert", infile)
#生成缩略图
#python py.py logo.jpg
size = (80, 128)
for infile in sys.argv[1:]:
outfile = os.path.splitext(infile)[0] + ".thumbnail"
if infile != outfile:
try:
im = Image.open(infile)
im.thumbnail(size)
im.save(outfile, "JPEG")
except IOError:
print("cannot create thumbnail for", infile)
#获取图片信息
for infile in sys.argv[1:]:
try:
with Image.open(infile) as im:
print(infile, im.format, "%dx%d" % im.size, im.mode)
except IOError:
pass
#图片切割
box = (200,200,200,200)
region = im.crop(box)
#粘贴回原图
region = region.transpose(Image.ROTATE_180)
im.paste(region, box)
2、PyMysql Mysql数据库操作模块
安装:pip install pymysql
import pymysql.cursors
#链接数据库
conn = pymysql.connect(host='localhost',
user='root',
password='root',
db='test',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
try:
with conn.cursor() as cursor:
#插入一条记录
sql = "INSERT INTO `ceshi` (`ordernum`, `xin`) VALUES (%s, %s)"
cursor.execute(sql, ('中文字测试', '20'))
conn.commit()
with conn.cursor() as cursor:
#读取记录
sql = "SELECT `id`, `ordernum` FROM `ceshi` WHERE `id`=%s"
cursor.execute(sql, ('12033742',))
result = cursor.fetchone()
print(result)
with conn.cursor() as cursor:
#更新记录
sql = "update `ceshi` set xin='test' WHERE `ordernum`=%s"
cursor.execute(sql, ('2017085212',))
with conn.cursor() as cursor:
#删除记录
sql = "delete from `ceshi` WHERE `ordernum`=%s"
cursor.execute(sql, ('2017085212',))
finally:
conn.close()
售后保障
承诺任何问题1小时内解决数据备份
更安全、更高效、更稳定价格公道精准
项目经理精准报价不弄虚作假合作无风险
重合同讲信誉,无效全额退款