分享到:

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

日期:2017-03-22 20:47:00     阅读:260     文章来源:源美网络     标签:深圳网站建设,深圳网站制作

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

售后保障

承诺任何问题1小时内解决

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款