Python切圖九宮格的實(shí)現(xiàn)方法
本文介紹了Python切圖九宮格的實(shí)現(xiàn)方法,分享給大家,具體如下

# -*- coding: utf-8 -*-
'''
將一張圖片填充為正方形后切為9張圖
'''
from PIL import Image
import sys
#將圖片填充為正方形
def fill_image(image):
width, height = image.size
#選取長(zhǎng)和寬中較大值作為新圖片的
new_image_length = width if width > height else height
#生成新圖片[白底]
new_image = Image.new(image.mode, (new_image_length, new_image_length), color='white')
#將之前的圖粘貼在新圖上,居中
if width > height:#原圖寬大于高,則填充圖片的豎直維度
#(x,y)二元組表示粘貼上圖相對(duì)下圖的起始位置
new_image.paste(image, (0, int((new_image_length - height) / 2)))
else:
new_image.paste(image, (int((new_image_length - width) / 2),0))
return new_image
#切圖
def cut_image(image):
width, height = image.size
item_width = int(width / 3)
box_list = []
# (left, upper, right, lower)
for i in range(0,3):#兩重循環(huán),生成9張圖片基于原圖的位置
for j in range(0,3):
#print((i*item_width,j*item_width,(i+1)*item_width,(j+1)*item_width))
box = (j*item_width,i*item_width,(j+1)*item_width,(i+1)*item_width)
box_list.append(box)
image_list = [image.crop(box) for box in box_list]
return image_list
#保存
def save_images(image_list):
index = 1
for image in image_list:
image.save('./python'+str(index) + '.png', 'PNG')
index += 1
if __name__ == '__main__':
file_path = "python.jpeg"
image = Image.open(file_path)
#image.show()
image = fill_image(image)
image_list = cut_image(image)
save_images(image_list)



以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python+flask實(shí)現(xiàn)restful接口的示例詳解
這篇文章主要為大家詳細(xì)介紹了Python如何利用flask實(shí)現(xiàn)restful接口,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下2023-02-02
Python?TypeError:?‘float‘?object?is?not?subscriptable錯(cuò)
發(fā)現(xiàn)問題寫python的時(shí)候出現(xiàn)了這個(gè)錯(cuò),所以想著給大家總結(jié)下,這篇文章主要給大家介紹了關(guān)于Python?TypeError:?‘float‘?object?is?not?subscriptable錯(cuò)誤的解決辦法,需要的朋友可以參考下2022-12-12
python根據(jù)給定文件返回文件名和擴(kuò)展名的方法
這篇文章主要介紹了python根據(jù)給定文件返回文件名和擴(kuò)展名的方法,實(shí)例分析了Python操作文件及字符串的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03
Python實(shí)現(xiàn)刪除windows下的長(zhǎng)路徑文件
這篇文章主要為大家詳細(xì)介紹一下如何利用Python語言實(shí)現(xiàn)刪除windows下的長(zhǎng)路徑文件功能,文中的示例代碼講解詳細(xì),具有一定參考借鑒價(jià)值,感興趣的可以了解一下2022-07-07
Python光學(xué)仿真wxpython透鏡演示系統(tǒng)計(jì)算與繪圖
這篇文章主要為大家介紹了Python光學(xué)仿真wxpython透鏡演示系統(tǒng)計(jì)算與繪圖的實(shí)現(xiàn)示例。有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-10-10
Python使用matplotlib繪制Logistic曲線操作示例
這篇文章主要介紹了Python使用matplotlib繪制Logistic曲線操作,結(jié)合實(shí)例形式詳細(xì)分析了Python基于matplotlib庫(kù)繪制Logistic曲線相關(guān)步驟與實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-11-11
pytorch中交叉熵?fù)p失函數(shù)的使用小細(xì)節(jié)
這篇文章主要介紹了pytorch中交叉熵?fù)p失函數(shù)的使用細(xì)節(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02

