No1:
【Pillow】圖像處理標准庫
縮放
from PIL import Image # 打開一個jpg圖像文件,注意是當前路徑: im = Image.open('test.jpg') # 獲得圖像尺寸: w, h = im.size print('Original image size: %sx%s' % (w, h)) # 縮放到50%: im.thumbnail((w//2, h//2)) print('Resize image to: %sx%s' % (w//2, h//2)) # 把縮放后的圖像用jpeg格式保存: im.save('thumbnail.jpg', 'jpeg')
濾鏡
from PIL import Image, ImageFilter # 打開一個jpg圖像文件,注意是當前路徑: im = Image.open('test.jpg') # 應用模糊濾鏡: im2 = im.filter(ImageFilter.BLUR) im2.save('blur.jpg', 'jpeg')
字母驗證碼
from PIL import Image, ImageDraw, ImageFont, ImageFilter import random # 隨機字母: def rndChar(): return chr(random.randint(65, 90)) # 隨機顏色1: def rndColor(): return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255)) # 隨機顏色2: def rndColor2(): return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127)) # 240 x 60: width = 60 * 4 height = 60 image = Image.new('RGB', (width, height), (255, 255, 255)) # 創建Font對象: font = ImageFont.truetype('arial.ttf', 36) # 創建Draw對象: draw = ImageDraw.Draw(image) # 填充每個像素: for x in range(width): for y in range(height): draw.point((x, y), fill=rndColor()) # 輸出文字: for t in range(4): draw.text((60 * t + 10, 10), rndChar(), font=font, fill=rndColor2()) # 模糊: image = image.filter(ImageFilter.BLUR) image.save('code.jpg', 'jpeg')
效果圖
No2:
【requests】處理URL
Get
params參數
json
post
>>> r = requests.post('https://accounts.douban.com/login', data={'form_email': 'abc@example.com', 'form_password': '123456'})
上傳文件
>>> upload_files = {'file': open('report.xls', 'rb')} >>> r = requests.post(url, files=upload_files)
No3:
【chardet】檢測編碼
No4:
【psutil】獲取系統信息
等等各種運維用到的信息
No5:
【virtualenv】創建隔離的python運行環境
原理很簡單,就是把系統Python復制一份到virtualenv的環境,用命令source venv/bin/activate
進入一個virtualenv環境時,virtualenv會修改相關環境變量,讓命令python
和pip
均指向當前的virtualenv環境。
No6:
【Tkinter】圖形界面
from tkinter import * class Application(Frame): def __init__(self,master=None): Frame.__init__(self,master) self.pack() self.createWidgets() def createWidgets(self): self.helloLabel=Label(self,text='Hello,World!') self.helloLabel.pack() self.quitButton = Button(self,text='Quit',command=self.quit) self.quitButton.pack() if __name__ == '__main__': app=Application() app.master.title('Hello World!') app.mainloop()
結果界面好丑
圖形界面還有其他寫法,后續再學