python九宫格拼图_Python切割图片成九宫格
這篇文字講述如何使用Python把一張完整的大圖切割成9份小圖片,制作朋友圈九宮格圖文分享。
原圖如下:
Image
我們想要利用這張圖制作高逼格的九宮格朋友圈分享。
達到類似于這樣的效果:
實現原理非常簡單,那就是利用PIL庫對原圖不斷畫小區域然后切下來存儲成新的小圖片。
假設每一個格子的寬和高分別是w、h,那么第row行(從0開始計數),第col列(從0開始計數)的格子左上角坐標和右下角坐標分別是(col * w, row * h),(col * w + w, r * h + h)。
如果你在學習Python的過程當中有遇見任何問題,可以加入python交流學企鵝群:【611+530+101】,多多交流問題,互幫互助,群里有不錯的學習教程和開發工具。學習python有任何問題(學習方法,學習效率,如何就業),可以隨時來咨詢我
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)
為了能在朋友圈中預覽時看到所有圖片的完整樣子,建議保證自己的原始圖片是正方形的,然后再運行這個腳本,在output中得到九張圖片。最后,嗯,就可以去秀了!
總結
以上是生活随笔為你收集整理的python九宫格拼图_Python切割图片成九宫格的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 自动驾驶传感器产业链
- 下一篇: LIO-SAM中的scan_to_map