PIL的常見示例

2022-09-13 13:18:13 字數 3547 閱讀 4706

#opencv是將開啟為numpy array的形式

#pil是將開啟為pil.image的形式

'''from pil import imagecolor

#顏色與rgba值

print(imagecolor.getcolor('red', 'rgba'))

print(imagecolor.getcolor('black', 'rgb'))

'''#使用pillow操作影象

from pil import image

im_path = r'c:\\users\\12271\\desktop\\**練習\\python基礎練習\\test.png'

im = image.open(im_path)

width, height = im.size

#寬和高

print(im.size, width, height)

#格式,以及格式的詳細描述

print(im.format, im.format_description)

im.s**e(r'c:\\users\\12271\\desktop\\**練習\\python基礎練習\\testt.png')

im.show()

#新建影象

#pillow也可以新建空白影象,第乙個引數是mode即顏色空間模式,第二個引數指定了影象的解析度(寬*高),第三個指顏色

#通常使用rgb模式就可以了

newim= image.new('rgb', (100, 100), 'red')

newim.show()

# 傳入元組形式的rgba值或者rgb值

# 在rgb模式下,第四個引數失效,預設255,在rgba模式下,也可只傳入前三個值,a值預設255

blcakim = image.new('rgb',(200, 100), (255, 255, 0, 120))

blcakim.show()

#裁剪影象

im = image.open(r'c:\\users\\12271\\desktop\\**練習\\python基礎練習\\test.png')

cropedim = im.crop((100,100,200,200)) #(x1,x2,y1,y2)

cropedim.show()

#複製與貼上影象到另乙個影象

im = image.open(r'c:\\users\\12271\\desktop\\**練習\\python基礎練習\\test.png')

cropedim = im.crop((100,100,200,200))

im.paste(cropedim, (0, 0)) #將複製到另一張上

im.show()

#調整的大小

im = image.open(r'c:\\users\\12271\\desktop\\**練習\\python基礎練習\\test.png')

width, height = im.size

resizedim = im.resize((width, height+(1920-1080)))

resizedim.show()

#旋轉和翻轉影象

im = image.open(r'c:\\users\\12271\\desktop\\**練習\\python基礎練習\\test.png')

im.rotate(90).s**e(r'c:\\users\\12271\\desktop\\**練習\\python基礎練習\\rotate90.png')

im.rotate(270).s**e(r'c:\\users\\12271\\desktop\\**練習\\python基礎練習\\rotate270.png')

im.rotate(180).s**e(r'c:\\users\\12271\\desktop\\**練習\\python基礎練習\\rotate180.png')

im.rotate(20).s**e(r'c:\\users\\12271\\desktop\\**練習\\python基礎練習\\rotate20.png')

im.rotate(20, expand=true).s**e(r'c:\\users\\12271\\desktop\\**練習\\python基礎練習\\rotate20_expand.png')

im.transpose(image.flip_left_right).s**e(r'c:\\users\\12271\\desktop\\**練習\\python基礎練習\\transepose_lr.png')

im.transpose(image.flip_top_bottom).s**e(r'c:\\users\\12271\\desktop\\**練習\\python基礎練習\\transepose_tb.png')

#影象過濾

from pil import image, imagefilter

im = image.open(r'c:\\users\\12271\\desktop\\**練習\\python基礎練習\\test.png')

# 高斯模糊

im.filter(imagefilter.gaussianblur).s**e(r'c:\\users\\12271\\desktop\\**練習\\python基礎練習\\gaussianblur.png')

# 普通模糊

im.filter(imagefilter.blur).s**e(r'c:\\users\\12271\\desktop\\**練習\\python基礎練習\\blur.png')

# 邊緣增強

im.filter(imagefilter.edge_enhance).s**e(r'c:\\users\\12271\\desktop\\**練習\\python基礎練習\\edge_enhance.png')

# 找到邊緣

im.filter(imagefilter.find_edges).s**e(r'c:\\users\\12271\\desktop\\**練習\\python基礎練習\\find_edges.png')

# 浮雕

im.filter(imagefilter.emboss).s**e(r'c:\\users\\12271\\desktop\\**練習\\python基礎練習\\emboss.png')

# 輪廓

im.filter(imagefilter.contour).s**e(r'c:\\users\\12271\\desktop\\**練習\\python基礎練習\\contour.png')

# 銳化

im.filter(imagefilter.sharpen).s**e(r'c:\\users\\12271\\desktop\\**練習\\python基礎練習\\sharpen.png')

# 平滑

im.filter(imagefilter.smooth).s**e(r'c:\\users\\12271\\desktop\\**練習\\python基礎練習\\smooth.png')

# 細節

im.filter(imagefilter.detail).s**e(r'c:\\users\\12271\\desktop\\**練習\\python基礎練習\\detail.png')

opencv的常見示例

opencv是將開啟為numpy array的形式 pil是將開啟為pil.image的形式 的讀取 分割以及儲存 import cv2 img cv2.imread test.png im height,im width,im dep img.shape img size img.size 返回影...

PIL庫的使用

導入庫 from pil import image 需要的字元 隨便定義就行了 ascii char list wm ahkbdpqwmzo0qljuyxzcvunxr jft 1 i 定義顏色 def get char r,b,g,alpha 256 if alpha 0 返回空格 return ...

3 6常見查詢示例

3.6.1列的最大值 3.6.2儲存某列最大值的行 3.6.3每組最大列數 3.6.4保留某列的分組最大值的行 3.6.5使用使用者定義的變數 3.6.6使用外來鍵 3.6.7搜尋兩個鍵 3.6.8計算每天的訪問量 3.6.9使用auto increment 以下是有關如何解決mysql常見問題的示...