亚洲国产欧美一区二区三区f,亚洲A∨精品永久无码青草网,亚洲 暴爽 av人人爽日日碰,亚洲AV永久无码精心天堂久久_无码

系統城裝機大師 - 唯一官網:www.outletmksalestore.com!

當前位置:首頁 > 腳本中心 > python > 詳細頁面

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

時間:2020-03-10來源:電腦系統城作者:電腦系統城

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

原圖如下:

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

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

達到類似于這樣的效果:

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

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

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

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


 
  1. code snippet:
  2. #! /usr/local/bin/python3
  3. # -*- coding: utf-8 -*-
  4. fromPILimportImage
  5. defcut_image(image):
  6. width, height = image.size
  7. item_width = width /3.0
  8. item_height = height /3.0
  9. box_list = []
  10. forrowinrange(0,3):
  11. forcolinrange(0,3):
  12. box = (col * item_width, row * item_height,( col +1) * item_width,( row +1) * item_height)
  13. box_list.append( box )
  14. image_list = [image.crop(box)forboxinbox_list]
  15. returnimage_list
  16. defsave_images(image_list):
  17. dirName ='output'
  18. ifFalse== os.path.exists( dirName ):
  19. os.makedirs( dirName )
  20. index =1
  21. forimageinimage_list:
  22. image.save(‘./output/python'+str(index) +'.png','PNG')
  23. index +=1
  24. if__name__ =='__main__':
  25. image = Image.open("use.png")
  26. image_list = cut_image(image)
  27. save_images(image_list)

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

總結

到此這篇關于Python切割圖片成九宮格的文章就介紹到這了,更多相關Python切割圖片 九宮格 內容請搜索我們以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持我們!

分享到:

相關信息

系統教程欄目

欄目熱門教程

人氣教程排行

站長推薦

熱門系統下載

亚洲国产欧美一区二区三区f,亚洲A∨精品永久无码青草网,亚洲 暴爽 av人人爽日日碰,亚洲AV永久无码精心天堂久久_无码 日本少妇又色又爽又高潮