圖像和辦公文檔處理
圖像和辦公文檔處理
用程序來處理圖像和辦公文檔經(jīng)常出現(xiàn)在實(shí)際開發(fā)中,Python的標(biāo)準(zhǔn)庫中雖然沒有直接支持這些操作的模塊,但我們可以通過Python生態(tài)圈中的第三方模塊來完成這些操作。
操作圖像
計(jì)算機(jī)圖像相關(guān)知識
顏色。如果你有使用顏料畫畫的經(jīng)歷,那么一定知道混合紅、黃、藍(lán)三種顏料可以得到其他的顏色,事實(shí)上這三種顏色就是被我們稱為美術(shù)三原色的東西,它們是不能再分解的基本顏色。在計(jì)算機(jī)中,我們可以將紅、綠、藍(lán)三種色光以不同的比例疊加來組合成其他的顏色,因此這三種顏色就是色光三原色,所以我們通常會將一個顏色表示為一個RGB值或RGBA值(其中的A表示Alpha通道,它決定了透過這個圖像的像素,也就是透明度)。
名稱 RGBA值 名稱 RGBA值 White (255, 255, 255, 255) Red (255, 0, 0, 255) Green (0, 255, 0, 255) Blue (0, 0, 255, 255) Gray (128, 128, 128, 255) Yellow (255, 255, 0, 255) Black (0, 0, 0, 255) Purple (128, 0, 128, 255) 像素。對于一個由數(shù)字序列表示的圖像來說,最小的單位就是圖像上單一顏色的小方格,這些小方塊都有一個明確的位置和被分配的色彩數(shù)值,而這些一小方格的顏色和位置決定了該圖像最終呈現(xiàn)出來的樣子,它們是不可分割的單位,我們通常稱之為像素(pixel)。每一個圖像都包含了一定量的像素,這些像素決定圖像在屏幕上所呈現(xiàn)的大小。
用Pillow操作圖像
Pillow是由從著名的Python圖像處理庫PIL發(fā)展出來的一個分支,通過Pillow可以實(shí)現(xiàn)圖像壓縮和圖像處理等各種操作??梢允褂孟旅娴拿顏戆惭bPillow。
pip?install?pillow
Pillow中最為重要的是Image類,讀取和處理圖像都要通過這個類來完成。
>>>?from?PIL?import?Image
>>>
>>>?image?=?Image.open('./res/guido.jpg')
>>>?image.format,?image.size,?image.mode
('JPEG',?(500,?750),?'RGB')
>>>?image.show()

剪裁圖像
>>>?image?=?Image.open('./res/guido.jpg')
>>>?rect?=?80,?20,?310,?360
>>>?image.crop(rect).show()

生成縮略圖
>>>?image?=?Image.open('./res/guido.jpg')
>>>?size?=?128,?128
>>>?image.thumbnail(size)
>>>?image.show()

縮放和黏貼圖像
>>>?image1?=?Image.open('./res/luohao.png')
>>>?image2?=?Image.open('./res/guido.jpg')
>>>?rect?=?80,?20,?310,?360
>>>?guido_head?=?image2.crop(rect)
>>>?width,?height?=?guido_head.size
>>>?image1.paste(guido_head.resize((int(width?/?1.5),?int(height?/?1.5))),?(172,?40))

旋轉(zhuǎn)和翻轉(zhuǎn)
>>>?image?=?Image.open('./res/guido.png')
>>>?image.rotate(180).show()
>>>?image.transpose(Image.FLIP_LEFT_RIGHT).show()


操作像素
>>>?image?=?Image.open('./res/guido.jpg')
>>>?for?x?in?range(80,?310):
...?????for?y?in?range(20,?360):
...?????????image.putpixel((x,?y),?(128,?128,?128))
...?
>>>?image.show()

濾鏡效果
>>>?from?PIL?import?Image,?ImageFilter
>>>
>>>?image?=?Image.open('./res/guido.jpg')
>>>?image.filter(ImageFilter.CONTOUR).show()

處理Excel電子表格
Python的openpyxl模塊讓我們可以在Python程序中讀取和修改Excel電子表格,由于微軟從Office 2007開始使用了新的文件格式,這使得Office Excel和LibreOffice Calc、OpenOffice Calc是完全兼容的,這就意味著openpyxl模塊也能處理來自這些軟件生成的電子表格。
import?datetime
from?openpyxl?import?Workbook
wb?=?Workbook()
ws?=?wb.active
ws['A1']?=?42
ws.append([1,?2,?3])
ws['A2']?=?datetime.datetime.now()
wb.save("sample.xlsx")
處理Word文檔
利用python-docx模塊,Python可以創(chuàng)建和修改Word文檔,當(dāng)然這里的Word文檔不僅僅是指通過微軟的Office軟件創(chuàng)建的擴(kuò)展名為docx的文檔,LibreOffice Writer和OpenOffice Writer都是免費(fèi)的字處理軟件。
from?docx?import?Document
from?docx.shared?import?Inches
document?=?Document()
document.add_heading('Document?Title',?0)
p?=?document.add_paragraph('A?plain?paragraph?having?some?')
p.add_run('bold').bold?=?True
p.add_run('?and?some?')
p.add_run('italic.').italic?=?True
document.add_heading('Heading,?level?1',?level=1)
document.add_paragraph('Intense?quote',?style='Intense?Quote')
document.add_paragraph(
????'first?item?in?unordered?list',?style='List?Bullet'
)
document.add_paragraph(
????'first?item?in?ordered?list',?style='List?Number'
)
document.add_picture('monty-truth.png',?width=Inches(1.25))
records?=?(
????(3,?'101',?'Spam'),
????(7,?'422',?'Eggs'),
????(4,?'631',?'Spam,?spam,?eggs,?and?spam')
)
table?=?document.add_table(rows=1,?cols=3)
hdr_cells?=?table.rows[0].cells
hdr_cells[0].text?=?'Qty'
hdr_cells[1].text?=?'Id'
hdr_cells[2].text?=?'Desc'
for?qty,?id,?desc?in?records:
????row_cells?=?table.add_row().cells
????row_cells[0].text?=?str(qty)
????row_cells[1].text?=?id
????row_cells[2].text?=?desc
document.add_page_break()
document.save('demo.docx')
