九宮格圖片
- 效果圖
- 大概講解
- 部分講解
- 原始碼
- 🎃特別介紹:
效果圖



大概講解
簡單的使用了PIL庫里面的Image對目標圖片進行剪切和保存將圖片填充為正方形,組成:原圖+百底+正方形,創建了ImageObj(object)類類里面的函式功能標注的都有有不明白的地方可以留言,這次選擇的照片像素不是很高,效果不是太理想,之所以沒放女友照片一是怕你們愛上她我吃醋,二是現在官方查的嚴不讓放美女圖,😁😁
部分講解
🛎?基本上總體分為四部主功能:
# 1、打開圖片
image = Image.open(file_path)
# 2、將圖片填充為正方形,組成:原圖+百底+正方形
image = self.__fill_image(image)
# 保存圖片
image.save('temp.jpg')
# 3、裁剪合成后圖片,為9張圖片
nine_images = self.__cut_image(image)
# 4、保存9張圖片到本地
self.save_images(nine_images)
🛎?剪切圖片功能函式:
def __cut_image(self, image):
"""
把一張圖片裁剪成9張圖片的坐標值,然后進行裁剪成小圖片
:return:
"""
width, height = image.size
print('原圖的寬/高分別為:', width, height)
# 每張圖片的寬度
item_width = int(width / 3)
print('裁剪后的寬度為:', item_width)
box_list = []
for i in range(0, 3):
for j in range(0, 3):
# 坐標值分別是:左、上、右、底
box = (j * item_width, i * item_width, (j + 1) * item_width, (i + 1) * item_width)
print(box)
box_list.append(box)
# 裁剪圖片
image_list = [image.crop(box) for box in box_list]
return image_list
🛎?填充切割圖片部分:
def __fill_image(self, image):
width, height = image.size
# 長和寬較大值,作為新圖片的寬高
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)二元組表示粘貼上圖相對下圖的起始位置
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
原始碼
from PIL import Image
class ImageObj(object):
def __init__(self):
pass
def start(self, file_path):
# 1、打開圖片
image = Image.open(file_path)
# 2、將圖片填充為正方形,組成:原圖+百底+正方形
image = self.__fill_image(image)
# 保存圖片
image.save('temp.jpg')
# 3、裁剪合成后圖片,為9張圖片
nine_images = self.__cut_image(image)
# 4、保存9張圖片到本地
self.save_images(nine_images)
def __cut_image(self, image):
"""
把一張圖片裁剪成9張圖片的坐標值,然后進行裁剪成小圖片
:return:
"""
width, height = image.size
print('原圖的寬/高分別為:', width, height)
# 每張圖片的寬度
item_width = int(width / 3)
print('裁剪后的寬度為:', item_width)
box_list = []
for i in range(0, 3):
for j in range(0, 3):
# 坐標值分別是:左、上、右、底
box = (j * item_width, i * item_width, (j + 1) * item_width, (i + 1) * item_width)
print(box)
box_list.append(box)
# 裁剪圖片
image_list = [image.crop(box) for box in box_list]
return image_list
def __fill_image(self, image):
"""
將圖片填充為正方形
:param image:
:return:
"""
width, height = image.size
# 長和寬較大值,作為新圖片的寬高
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)二元組表示粘貼上圖相對下圖的起始位置
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 save_images(self, nine_images):
"""
保存圖片
:param nine_images:
:return:
"""
index = 1
for image in nine_images:
image.save(str(index) + '.jpg')
index += 1
有什么問題可以私信或者評論,如果您覺得還可以,可以動動手指幫小弟點個贊嗎,不勝感激,
🎃特別介紹:
📣小白練手專欄,適合剛入手的新人歡迎訂閱編程小白進階
📣有什么不明白的歡迎私信或留言,得到細致講解,另外想要進階的朋友可以關注練手專案專欄
📣另外想學JavaWeb進廠的同學可以看看這個專欄:傳送們
📣是個面試和考研的演算法練習我們一起加油上岸之路

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/292186.html
標籤:python
上一篇:Python 的內建函式
