Python模塊(3)--PIL 簡易使用教程

PIL模塊-用與記

  • 1.圖片導入Image.open()
  • 2.圖像顯示.show()
  • 4.查看圖片屬性.format,.size,.mode
  • 3.圖像格式轉換.convert()
  • 4.圖像模式“L”,“RGB”,"CYMK"
  • 5. 圖片旋轉.rotate()
    • 旋轉方式1:旋轉不擴展
    • 旋轉方式2:旋轉擴展
    • 旋轉方式3:旋轉,擴展,白色填充
  • 6.圖片切割.crop()
  • 7.圖片保存.save()

PIL:Python Imaging Library,為Python圖像處理常用的庫。

PIL手冊網站:http://effbot.org/imagingbook/pil-index.htm
PIL庫中最重要的一個類:Image,可以通過這個類/模塊 導入圖像,處理圖像,保存圖像

import Image

1.圖片導入Image.open()

img=Image.open(“xxx/xxx/lena.jpg”)

導入路徑中的文件名后要帶擴展名,不然會報一下錯誤:

IOError: [Errno 2] No such file or directory: ‘lena’

2.圖像顯示.show()

img.show()

在這里插入圖片描述

4.查看圖片屬性.format,.size,.mode

圖片源格式,圖片尺寸,圖片模式

print(img.format , img.size , img.mode)

輸出

(‘JPEG’, (200, 200), ‘RGB’)

3.圖像格式轉換.convert()

img.convert('L")
img.show()

在這里插入圖片描述
mode 從"RGB" 轉變成"L"

4.圖像模式“L”,“RGB”,“CYMK”

PIL中的圖像有一下9種模式,使用.convert()函數,可以讓圖片在這9中模式中來回轉換。(作為參數,模式要加引號)
· 1 (1-bit pixels, black and white, stored with one pixel per byte)
· L (8-bit pixels, black and white) # 灰度
· P (8-bit pixels, mapped to any other mode using a colour palette)
· RGB (3x8-bit pixels, true colour) # 彩色
· RGBA (4x8-bit pixels, true colour with transparency mask)
· CMYK (4x8-bit pixels, colour separation)
· YCbCr (3x8-bit pixels, colour video format)
· I (32-bit signed integer pixels)
· F (32-bit floating point pixels)
參考資料:https://www.cnblogs.com/shangpolu/p/8041848.html

5. 圖片旋轉.rotate()

旋轉方式1:旋轉不擴展

#在原圖上旋轉,旋轉后的圖像與原圖大小一致,轉出圖像的部分會被丟棄,所以會造成信息丟失
img2=img.rotate(45)

在這里插入圖片描述

旋轉方式2:旋轉擴展

#旋轉后擴展,信息不丟失,圖像尺寸變大
img3=img.rotate(45,expand=True)

在這里插入圖片描述)

旋轉方式3:旋轉,擴展,白色填充

img_alpha=img.convert("RGBA")  # 將原圖轉換成RGBA的格式,四個通道,另一個通道表示透明度,默認為255,完全不透明
rot = img_alpha.rotate(45, expand = True) # 旋轉圖片fff=Image.new("RGBA",rot.size,(255,255,255,255))
out=Image.composite(rot,fff,mask=rot)
out.convert(img.mode).show()
# RGBA模式需要轉換成RGB,才能存儲,不然會報錯

在這里插入圖片描述

參考資料:https://blog.csdn.net/luolinll1212/article/details/83059118

6.圖片切割.crop()

box = (0, 0, 100, 150) #切割區域的四個坐標(left,top,right,bottom) 
region = img.crop(box)

在這里插入圖片描述

PIL 圖像坐標系以圖像的左上角為(0,0)位置,第一維度為長,第二維度為高。

7.圖片保存.save()

img.save("…/testdir/img.jpg")

如果文件目錄不存在,就會報錯

FileNotFoundError: [Errno 2] No such file or directory: ‘…/testdir/img.jpg’

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/445112.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/445112.shtml
英文地址,請注明出處:http://en.pswp.cn/news/445112.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

日志級別 debug info warn eirror fatal

日志級別 debug info warn eirror fatal 軟件中總免不了要使用諸如 Log4net, Log4j, Tracer 等東東來寫日志,不管用什么,這些東東大多是大同小異的,一般都提供了這樣5個日志級別: Debug Info Warn Error Fatal一個等級比一個高&…

輸入輸出系統

I/O設備:輸入輸出和存儲功能的設備 I/O設備的分類 按傳輸的速度: 低速設備(如鍵盤、鼠標、語音輸入輸出設備) 中速設備(如行式打印機、激光打印機等) 高速設備(如磁帶機、磁盤機、光盤機等&…

vue2源碼解析---v-model雙向數據綁定

什么是v-model v-model 是 Vue 中的一個指令,用于實現表單元素與 Vue 實例中數據的雙向綁定。這意味著當表單元素的值發生變化時,Vue 實例中的數據也會隨之更新 工作原理 生成ast樹 本質上是語法糖 結合了v-bind和v-on兩個指令 示例代碼 new Vue({e…

php收集的精典代碼

1. οncοntextmenu"window.event.return&#xff06;#118aluefalse" 將徹底屏蔽鼠標右鍵 <table border οncοntextmenureturn(false)><td>no</table> 可用于Table 2. <body onselectstart"return false"> 取消選取、防止復制…

python外卷(7)--glob

glob模塊1.glob.glob()2.對比os.listdir()glob是python自帶的一個操作文件的模塊&#xff0c;可用于查找 指定路徑 中 匹配的 文件。1.glob.glob() 下面是一個測試文件路徑&#xff1a; (base) pppp-System-Product-Name:~/Desktop/test_glob$ tree . ├── a │ ├── 1…

Sublime Text 2配置強大的IDE開發環境,運行java

Sublime Text 2是我無意中發現的、據說十分強大的、便捷的編輯器&#xff0c;許多程序員都投入到Sublime Text 2的懷抱中。 1 配置java開發環境的方法如下&#xff1a; 在jdk安裝目錄下的bin文件夾下新建一個bat格式的文件&#xff0c;文件命為javacexec.bat。 如果是在Wind…

thinkphp的快捷方法實例化對象

D、F、S、C、L、A、I 他們都在functions.php這個文件家 下面我分別說明一下他們的功能 D&#xff08;&#xff09; 加載Model類 M&#xff08;&#xff09; 加載Model類 A&#xff08;&#xff09; 加載Action類 L&#xff08;&#xff09; 獲取語言定義 C&#xff08;&#xf…

Python外卷(8)--pdist, squareform

pdist, squareform1.pdist, squareform使用例子2.通過矩陣的四則運算實現上述pdist, squareformscipy.spatial.distance 距離計算庫中有兩個函數&#xff1a;pdist, squareform&#xff0c;用于計算樣本對之間的歐式距離&#xff0c;并且將樣本間距離用方陣表示出來。&#xff…

模擬進程調度

功能 data.h #ifndef _Data_h_ #define _Data_h_#include <stdio.h> #include <stdlib.h> #include <string.h>#define ElemType PCB #define Status int #define OK 1 #define ERROR 0 #define TimeSlice 1 #define Infinity 10 //INT_MAX#define NAME_M…

gdb調試多進程和多線程命令

1. 默認設置下&#xff0c;在調試多進程程序時GDB只會調試主進程。但是GDB&#xff08;>V7.0&#xff09;支持多進程的 分別以及同時 調試&#xff0c;換句話說&#xff0c;GDB可以同時調試多個程序。只需要設置follow-fork-mode(默認值&#xff1a;parent)和detach-on-fork…

python外卷(10)--取整

"取整"那些事1.python 內置函數1.1int()--向下取整1.2round()--四舍五入2.math模塊取整函數2.1 math.floor()--向下取整2.2 math.ceil()--向上取整2.3 math.modf()--分別取小數部分和整數部分3.numpy模塊取整函數3.1 numpy.floor()--向下取整3.2 numpy.ceil()--向上取…

模擬銀行家算法

介紹 data.h #ifndef _Data_h_ #define _Data_h_#include <stdio.h> #include <stdlib.h> #include <string.h>#define ElemType PCB #define Status int #define true 1 #define false 0 #define OK 1 #define ERROR 0 #define RESOURCE_NUM …

Lua 與 C混合編程 .

本文通過程序實例說明C調用lua腳本和lua調用C的方法: 先建立一個 test.c文件: #include <stdio.h> #include <stdlib.h> #include "lua.h" #include "lualib.h" #include "lauxlib.h" #pragma comment(lib, "lua5.1.lib&qu…

模擬固定分區分配

介紹 data.h #ifndef _Data_h_ #define _Data_h_#include <stdio.h> #include <stdlib.h> #include <string.h> #define LIST_INIT_SIZE 10 #define LISTINCREMENT 2 #define true 1 #define false 0 #define PCBType PCB #define Status int…

Linux下的lua和boost c++的搭建和安裝

先下載lua &#xff0c;boost c http://www.lua.org/versions.html#5.2 http://www.boost.org/ http://sourceforge.net/projects/luabind/ 1. 安裝lua [rootlocalhost ~]#tar zxvf lua-5.1.2.tar.gz -C /usr/local [rootlocalhost ~]# cd /usr/local/ [rootlocalhost lo…

模擬基本分頁存儲

介紹 data.h #ifndef _Data_h_ #define _Data_h_#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h>#define LIST_INIT_SIZE 10 #define LISTINCREMENT 2 #define true 1 #define false 0 #define PCBType PC…

常用正則表達式和shell命令列表

取當前目錄下普通文件的后綴名列表&#xff1a; ls -l | awk /^-/{print $NF} |awk -F. {print $NF}|awk !/^$/ 匹配0和正整數的正則表達式&#xff08;除0以外&#xff0c;其它數字不能以0開頭&#xff09;&#xff1a; (^0$)|(^[0-9]\d*$) 匹配中文字符的正則表達式&#xff…

無限踩坑系列(7)-Latex使用Tips

Latex常用命令1.latex注釋2.圖片左邊對齊3.字母頭上加聲調4.腳注5.公式中加空格6.字體加粗體7.公式換行8.\textsuperscript{*}9.\begin{itemize}10.\operatorname{trace}11.\noindent12.\textcircled{}圓圈數字一些TIPs1.latex注釋 單行使用百分號%注釋 多行使用如下命令,在編…

在CentOS6.2下安裝DNS服務軟件Bind并快速配置簡單實例

[實踐Ok]在CentOS6.2下安裝DNS并快速配置實例&#xff0c;共八步&#xff0c;心路歷程如下&#xff1a;背景介紹&#xff1a;在日常的開發中&#xff0c;往往會在測試機和外網的Http的Url實際接口是不一樣的&#xff0c;在測試機一個Url地址&#xff0c;在外網中又是一個地址。…

模擬動態分區分配

介紹 list.h #ifndef _List_h_ #define _List_h_#include "Data.h"//******* 鏈表 *******// Status InitLinkList(LinkList *L); void PCBAssign(PCBType *e1, PCBType e2); Status GetElemt_L(LinkList L,int i,PCBType *e); Status ListIn…