我們專注于高端品牌網(wǎng)站創(chuàng)意設(shè)計(jì)與開發(fā)
1、Pillow 圖像處理模塊
安裝:pip install pillow
#引入依賴庫
from __future__ import print_function
import os, sys
from PIL import Image
#讀入圖片文件,成功返回一個(gè)image object
im = Image.open("logo.png")
#print(im.format, im.size, im.mode)
#im.show() 調(diào)用外部程序打開圖片
#將圖片轉(zhuǎn)換為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數(shù)據(jù)庫操作模塊
安裝:pip install pymysql
import pymysql.cursors
#鏈接數(shù)據(jù)庫
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()
文章引用:http://www.lt-ad.com/new/214.html
本站文章為深圳網(wǎng)站建設(shè)·源美網(wǎng)絡(luò)原創(chuàng)策劃,如有版權(quán)糾紛或者違規(guī)問題,請(qǐng)聯(lián)系我們刪除,謝謝!
售后保障
承諾任何問題1小時(shí)內(nèi)解決數(shù)據(jù)備份
更安全、更高效、更穩(wěn)定價(jià)格公道精準(zhǔn)
項(xiàng)目經(jīng)理精準(zhǔn)報(bào)價(jià)不弄虛作假合作無風(fēng)險(xiǎn)
重合同講信譽(yù),無效全額退款