分享到:

Python的图像处理和mysql操作模块

日期:2017-03-22     阅读:88     文章来源:源美设计     标签:Python

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



文章引用:http://www.szymweb.com/new/214.html

本站文章为深圳网站建设·源美设计原创策划,如有版权纠纷或者违规问题,请联系我们删除,谢谢!

上一篇:互金行业进入下半场 P2P行业,今年或仅存500家

下一篇:Centos压力测试工具WebBench

返回列表
最新案例
OUR ADVANTAGE WORKS