使用Python圖像處理庫Pillow處理圖像文件的案例分析
使用Python語言解決實(shí)際問題時(shí),往往需要使用由第三方開發(fā)的開源Python軟件庫。
本案例使用圖像處理庫Pillow中的模塊、對象來處理圖像:實(shí)現(xiàn)讀取圖像、獲取圖像信息、調(diào)整圖像大小、旋轉(zhuǎn)圖像、平滑圖像、剪切圖像等基本圖像處理任務(wù)。
01、安裝Pillow
Pillow是Python中的圖像處理庫(PIL,Python Image Library),提供了了廣泛的文件格式支持,強(qiáng)大的圖像處理能力,主要包括圖像儲存、圖像顯示、格式轉(zhuǎn)換以及基本的圖像處理操作等。
Pillow位于Python包索引(PyPI)中,可以使用pip來安裝。注意,Anaconda包含了Pillow庫。
【例1】使用pip安裝Pillow庫。
以管理員身份運(yùn)行命令行提示符,輸入命令pip3 install Pillow,安裝Pillow庫。如圖1所示。
■ 圖1 使用pip安裝Pillow庫
02、打開和顯示圖像
Pillow庫包含幾十個模塊,組織在名為PIL的包中。PIL包中的一個模塊是Image。PIL.Image提供了一些包括從文件中加載圖像和創(chuàng)建新圖像的函數(shù),其中的Image用于表示圖像對象。
【例2】打開和顯示圖像。
使用PIL.Image模塊的open()函數(shù)可以打開一個圖像,返回一個圖像對象,然后調(diào)用圖像對象的show()方法,可以在屏幕上顯示圖像。
>>> import PIL; from PIL import Image >>> im = PIL.Image.open("c:/pythonpa/cs/img/mandrill.jpg") >>> im.show() >>> print(im.format, im.size, im.mode) #顯示圖像的格式、大小和模式信息 JPEG (298, 298) RGB
說明/
(1)im.format返回包含圖像格式的字符串(JPEG、GIF、TIFF、BMP、PNG、…)。
(2)im.size返回包含圖像寬度和高度的元組,單位為像素。與每個像素相關(guān)的是一對坐標(biāo)(i, j),用于標(biāo)識像素的列i和行j。列從左到右編號,從0開始;行從上到下編號,也從0開始。
(3)im.mode返回包含圖像模式的字符串(RGB、CYMK、Grayscale、…)。
03、圖像的基本操作
圖像對象的copy()方法用于拷貝圖像;crop()方法用于剪裁圖像;paste()方法用于將一個圖像粘貼(覆蓋)在另一個圖像上面;resize()方法用于調(diào)整圖像大??;rotate()方法用于旋轉(zhuǎn)和翻轉(zhuǎn)圖像;filter()方法用于圖像過濾。
Pillow提供的圖像處理工具包括其它眾多模塊。有關(guān)Pillow的更多信息,請查閱在線文檔:Pillow (PIL Fork) 10.0.0 documentation
使用PIL.Image模塊中的函數(shù)new()可以創(chuàng)建一個給定模式和大小的新圖像對象。例如,創(chuàng)建一個新的大小為800×600的RGB圖像的代碼如下:
>>> im2 = PIL.Image.new('RGB', (800,600))
【例3】圖像的基本操作示例。
把一幅圖像的4個副本排列成2×2網(wǎng)格:在左上方的副本是原始圖像,而畫面右上方、左下方、右下方則分別使用模塊PIL.ImageFilter中定義的內(nèi)置過濾器CONTOUR、EMBOSS、FIND_EDGES進(jìn)行過濾。
#模塊:c:\pythonpa\cs\image_test.py #命令行:python image_test.py c:\pythonpa\cs\img\mandrill.jpg #功能:把c:\pythonpa\cs\img\mandrill.jpg的4個副本排列成2×2網(wǎng)格并顯示 import sys import os import PIL.Image import PIL.ImageFilter im = PIL.Image.open(sys.argv[1]) width, height = im.size # 創(chuàng)建新圖像,大小為原始圖像的4倍 res = PIL.Image.new(im.mode, (2*width, 2*height)) # 把原始圖像放置在左上角 res.paste(im, (0, 0, width, height)) # 把輪廓過濾CONTOUR的圖像放置在右上角 contour = im.filter(PIL.ImageFilter.CONTOUR) res.paste(contour, (width, 0, 2*width, height)) # 把浮雕過濾EMBOSS的圖像放置在左下角 emboss = im.filter(PIL.ImageFilter.EMBOSS) res.paste(emboss, (0, height, width, 2*height)) # 把邊緣過濾FIND_EDGES的圖像放置在右下角 edges = im.filter(PIL.ImageFilter.FIND_EDGES) res.paste(edges, (width, height, 2*width, 2*height)) # 顯示結(jié)果圖像 res.show()
04、批量圖像格式轉(zhuǎn)換
使用PIL.Image模塊的open()函數(shù)打開磁盤圖像文件時(shí),會根據(jù)文件內(nèi)容自動確定文件格式。使用Image對象的save()方法保存圖像時(shí),可以指定格式,從而實(shí)現(xiàn)格式轉(zhuǎn)換。
【例4】批量圖像格式轉(zhuǎn)換。
#模塊:c:\pythonpa\cs\image_convert.py #命令行:python image_convert.py c:\pythonpa\cs\img jpg png #功能:把c:\pythonpa\cs\img下的所有jpg文件轉(zhuǎn)換為png文件 import sys import glob import os import PIL.Image img_path = sys.argv[1] + "/*." + sys.argv[2] for infile in glob.glob(img_path): f,e = os.path.splitext(infile) outfile = f + "." + sys.argv[3] PIL.Image.open(infile).save(outfile)
說明/
(1)glob模塊可以使用通配符匹配文件名。例如glob.glob("c:\tmp\*.jpg"),可以返回c:\tmp下的所有后綴為jpg的文件列表。
(2)os.path.splitext(p)可以拆分文件名和后綴。
05、批量創(chuàng)建縮略圖
縮略圖是網(wǎng)絡(luò)開發(fā)或圖像軟件預(yù)覽常用的一種基本技術(shù),使用Python的Pillow圖像庫中Image模塊中的Image對象的thumbnail()方法,可以很方便地建立縮略圖。
【例5】批量創(chuàng)建縮略圖。
#模塊:c:\pythonpa\cs\ image_thumbnail.py #命令行:python image_thumbnail.py c:\pythonpa\cs\img jpg #功能:把c:\pythonpa\cs\img下的所有*.jpg文件轉(zhuǎn)換為*_s.jpg縮略圖 import sys import os import glob import PIL.Image img_path = sys.argv[1] + "/*." + sys.argv[2] size = (128,128) for infile in glob.glob(img_path): f,e = os.path.splitext(infile) outfile = f + "_s." + sys.argv[2] img = PIL.Image.open(infile) img.thumbnail(size, PIL.Image.ANTIALIAS) img.save(outfile)
說明/
(1)glob模塊可以使用通配符匹配文件名。例如glob.glob("c:\tmp\*.jpg"),可以返回c:/tmp下的所有后綴為jpg的文件列表。
(2)os.path.splitext(p)可以拆分文件名和后綴。
06、批量圖像加文字水印
圖片加水印是防止盜版的有效方式之一。首先使用Python的Pillow圖像庫中的Image模塊的new函數(shù)可以創(chuàng)建水印圖像對象,并使用ImageDraw模塊在水印圖像上繪制文字,最后通過Image模塊的composite函數(shù)合成水印圖像和原圖像。
【例6】批量圖像加文字水印。
#模塊:c:\pythonpa\cs\image_watermark1.py #命令行:python image_watermark1.py c:\pythonpa\cs\img jpg "Python" #功能:把c:\pythonpa\cs\img下的所有*.jpg文件加"Python"水印并另存為*_w.jpg import sys import os import glob from PIL import Image, ImageDraw, ImageFont img_path = sys.argv[1] + "/*." + sys.argv[2] img_suffix = sys.argv[2] txt_log = sys.argv[3] for infile in glob.glob(img_path): f, e = os.path.splitext(infile) outfile = f + "_w." + img_suffix im = Image.open(infile) im_log = Image.new('RGBA', im.size) fnt = ImageFont.truetype("c:/Windows/fonts/Tahoma.ttf", 20) d = ImageDraw.ImageDraw(im_log) d.text((0, 0), txt_log, font = fnt) im_out = Image.composite(im_log, im, im_log) im_out.save(outfile)
07、批量圖像加圖片水印
加圖片水印的原理和加文字水印相同,首先使用Python的Pillow圖像庫中的Image模塊的new函數(shù)可以創(chuàng)建水印圖像對象,并使用圖像對象的paste方法把log圖像粘貼到水印圖像,最后通過Image模塊的composite函數(shù)合成水印圖像和原圖像。
【例7】批量圖像加圖片水印。
#模塊:c:\pythonpa\cs\image_watermark2.py #命令行:python image_watermark2.py c:\pythonpa\cs\img jpg c:\pythonpa\cs\img\python-logo.png #功能:把c:\pythonpa\cs\img下的所有*.jpg文件加水印python-logo.png并另存為*_w.jpg import sys import os import glob from PIL import Image, ImageDraw, ImageFont img_path = sys.argv[1] + "/*." + sys.argv[2] img_suffix = sys.argv[2] log_file = sys.argv[3] for infile in glob.glob(img_path): f, e = os.path.splitext(infile) outfile = f + "_w." + img_suffix im = Image.open(infile) im_log = Image.open(log_file) im_mark = Image.new('RGBA', im.size) im_mark.paste(im_log, (0, 0)) im_out = Image.composite(im_mark, im, im_mark) im_out.save(outfile)
08、批量調(diào)整圖像大小
調(diào)整圖像大小也是網(wǎng)絡(luò)開發(fā)或圖像軟件預(yù)覽常用的一種基本技術(shù)。使用Image對象的resize()方法可以調(diào)整圖像大小。
【例8】批量調(diào)整圖像大小。
#模塊:c:\pythonpa\cs\image_resize.py #命令行:python image_resize.py c:\pythonpa\cs\img jpg 640 480 #功能:把c:\pythonpa\cs\img下的所有*.jpg文件大小調(diào)整為640*480并另存為*_640.jpg import sys import os import glob import PIL.Image img_path = sys.argv[1] + "/*." + sys.argv[2] img_suffix = sys.argv[2] img_size_width = int(sys.argv[3]) img_size_height = int(sys.argv[4]) for infile in glob.glob(img_path): f, e = os.path.splitext(infile) outfile = f + "_" + str(img_size_width) + "." + img_suffix im = PIL.Image.open(infile) im_out = im.resize((img_size_width, img_size_height)) im_out.save(outfile)
以上就是使用Python圖像處理庫Pillow處理圖像文件的案例分析的詳細(xì)內(nèi)容,更多關(guān)于Python Pillow處理圖像文件的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Anaconda3+tensorflow2.0.0+PyCharm安裝與環(huán)境搭建(圖文)
這篇文章主要介紹了Anaconda3+tensorflow2.0.0+PyCharm安裝與環(huán)境搭建(圖文),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02Python實(shí)現(xiàn)把utf-8格式的文件轉(zhuǎn)換成gbk格式的文件
這篇文章主要介紹了Python實(shí)現(xiàn)把utf-8格式的文件轉(zhuǎn)換成gbk格式的文件,本文給出了實(shí)現(xiàn)代碼并同時(shí)剖析了代碼的作用,需要的朋友可以參考下2015-01-01python實(shí)現(xiàn)簡單成績錄入系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡單成績錄入系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09