欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python切割圖片成九宮格的示例代碼

 更新時(shí)間:2020年03月10日 09:48:01   作者:麥葉  
這篇文章主要介紹了Python切割圖片成九宮格的相關(guān)知識,本文通過截圖實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

這篇文字講述如何使用Python把一張完整的大圖切割成9份小圖片,制作朋友圈九宮格圖文分享。

原圖如下:

 

我們想要利用這張圖制作高逼格的九宮格朋友圈分享。

達(dá)到類似于這樣的效果:

 

實(shí)現(xiàn)原理非常簡單,那就是利用PIL庫對原圖不斷畫小區(qū)域然后切下來存儲成新的小圖片。

假設(shè)每一個(gè)格子的寬和高分別是w、h,那么第row行(從0開始計(jì)數(shù)),第col列(從0開始計(jì)數(shù))的格子左上角坐標(biāo)和右下角坐標(biāo)分別是(col * w, row * h),(col * w + w, r * h + h)。

 

code snippet:
#! /usr/local/bin/python3
# -*- coding: utf-8 -*-
fromPILimportImage
defcut_image(image):
width, height = image.size
item_width = width /3.0
item_height = height /3.0
box_list = []
forrowinrange(0,3):
forcolinrange(0,3):
box = (col * item_width, row * item_height,( col +1) * item_width,( row +1) * item_height)
box_list.append( box )
image_list = [image.crop(box)forboxinbox_list]
returnimage_list
defsave_images(image_list):
dirName ='output'
ifFalse== os.path.exists( dirName ):
os.makedirs( dirName )
index =1
forimageinimage_list:
image.save(‘./output/python'+str(index) +'.png','PNG')
index +=1
if__name__ =='__main__':
image = Image.open("use.png")
image_list = cut_image(image)
save_images(image_list)

為了能在朋友圈中預(yù)覽時(shí)看到所有圖片的完整樣子,建議保證自己的原始圖片是正方形的,然后再運(yùn)行這個(gè)腳本,在output中得到九張圖片。最后,嗯,就可以去秀了!

總結(jié)

到此這篇關(guān)于Python切割圖片成九宮格的文章就介紹到這了,更多相關(guān)Python切割圖片 九宮格 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論