Python学习(九)

学习网址:廖雪峰的Python教程

常用的第三方模块

Python既有内建模块,还有大量的第三方模块,模块网址按需安装

PIL

全称是: Python Imaging Library 是Python的图像处理标准库,功能强大且易用,仅支持到Python 2.7

Pillow

支持最新Python3.X

安装方式: pip install pillow

操作缩放图像:

1
2
3
4
5
6
7
8
9
>>> from PIL import Image
>>> i = Image.open('C:/Users/snow/Desktop/img/boy.jpg')
>>> w,h = i.size
>>> print('Image size: %s*%s' % (w,h))
Image size: 1080*1002
>>> i.thumbnail((w//2,h//2))
>>> print('Image size: %s*%s' % (w//2,h//2))
Image size: 540*501
>>> i.save('thumboy.jpg','jpeg')

图片模糊:

1
2
3
4
>>> from PIL import Image,ImageFilter
>>> ii = Image.open('C:/Users/snow/Desktop/img/iii.png')
>>> iii = ii.filter(ImageFilter.BLUR)
>>> iii.save('ib.png','png')

virtualenv

为Python应用提供不同的独立的Python运行环境,以适应不同版本

安装方法: pip install virtualenv

windows环境下的使用:

打开命令提示符,可以指定位置,使用 python -m venv 文件名 就可以创建一个独立的干净的没有第三方库的Python运行环境.

在此环境下使用pip安装其他库,就会安装在新创建的环境中,

图像界面

Tkinter

文档地址

官方解释:

The tkinter package (“Tk interface”) is the standard Python interface to the Tk GUI toolkit. Both Tk and tkinter are available on most Unix platforms, as well as on Windows systems. (Tk itself is not part of Python; it is maintained at ActiveState.) You can check that tkinter is properly installed on your system by running python -m tkinter from the command line; this should open a window demonstrating a simple Tk interface.

可以看出,tkinter是Tk 图形库的一个标准Python接口,既可以在Unix平台上,也可以在Windows平台上运行,Tk它本身并不是Python的一部分,还在维护.可以在命令行窗口直接输入 python -m tkinter 就会弹出一个窗口.

Hello World窗口示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import tkinter as tk
class Application(tk.Frame):
def init(self, master=None):
super().init(master)
self.pack()
self.create_widgets()
def create_widgets(self):
self.hi_there = tk.Button(self)
self.hi_there["text"] = "Hello World\n(click me)"
self.hi_there["command"] = self.say_hi
self.hi_there.pack(side="top")
self.quit = tk.Button(self, text="QUIT",fg="red",
command=root.destroy)
self.quit.pack(side="bottom")
def say_hi(self):
print("hi there, everyone!")
root = tk.Tk()
app = Application(master=root)
app.mainloop()

点击一下,可以在命令行里输出”hi there,everyone!”

真诚地希望能帮到你!