分(fēn)享到:

Python的圖像處理(lǐ)和mysql操作(zuò)模塊

日(rì)期:2017-03-22 20:47:00     閱讀(dú):260     文章(zhāng)來(lái)源:源美網絡     标簽:深圳網站(zhàn)建設,深圳網站(zhàn)制作(zuò)

1、Pillow 圖像處理(lǐ)模塊

安裝:pip install pillow


#引入依賴庫

from __future__ import print_function

import os, sys

from PIL import Image


#讀(dú)入圖片文件(jiàn),成功返回一個image object

im = Image.open("logo.png")

#print(im.format, im.size, im.mode)

#im.show() 調用外部程序打開圖片


#将圖片轉換爲jpg格式

static/picture/picture.html

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)

#生(shēng)成縮略圖

static/picture/picture.html

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數據庫操作(zuò)模塊

安裝: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:

        #讀(dú)取記錄

        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()



文章(zhāng)引用:

本站(zhàn)文章(zhāng)爲深圳網站(zhàn)建設·源美網絡原創策劃,如(rú)有版權糾紛或者違規問(wèn)題,請(qǐng)聯系我們删除,謝謝!

上一篇: 互金行業進入下半場 P2P行業,今年(nián)或僅存500家

下一篇: Centos壓力測試工(gōng)具WebBench

返回列表
最新案例
OUR ADVANTAGE WORKS

售後保障

承諾任何問(wèn)題1小時内解決

數據備份

更安全、更高效、更穩定

價格公道精準

項目經理(lǐ)精準報價不弄虛作(zuò)假

合作(zuò)無風(fēng)險

重合同講信譽,無效全額退款