日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python语言编写一个生成九宫格图片的代码_python实现图片九宫格分割

發布時間:2023/12/13 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python语言编写一个生成九宫格图片的代码_python实现图片九宫格分割 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

大家都知道在微信朋友圈或者微博以及QQ動態中,有很多“強迫癥患者”發圖片都愛發9張,而有些圖是一張圖片分成的九宮圖,對于這種操作,大家知道是怎么做到的嗎?

本文就是用Python做的一個九宮格圖片生成器,是一個打包好的exe文件,用戶無需部署安裝Python的開發環境,在本地就可以運行此程序,以此快速生成九宮格圖片。

下面是程序的所有代碼,這是一個Python GUI程序,代碼不多,也很容易理解:

# -*- coding: UTF-8 -*-

# 將一張圖片分成九張,九宮格

import tkinter as tk

from PIL import Image

import sys

#先將 input image 填充為正方形

def fill_image(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

# 分割圖片

def cut_image(image):

width, height = image.size

item_width = int(width / 3) #因為朋友圈一行放3張圖。

box_list = []

# (left, upper, right, lower)

for i in range(0,3):

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(str(index) + '.png', 'PNG')

index += 1

# 點擊按鈕,實現圖片分割

def cTofClicked():

file_path=str(entryCd.get()) # 獲取要進行分割的圖片路徑

image = Image.open(file_path)

#image.show()

image = fill_image(image)

image_list = cut_image(image)

save_images(image_list)

labelcTof.config(text="九宮格圖片已生,請在程序所在目錄查看!")

# 窗體

top=tk.Tk()

top.title('九宮格圖片生成器')

labelcTof=tk.Label(top,text="請輸入要進行轉換的圖片路徑:",height=4,\

width=40,fg="blue")

labelcTof.pack()

entryCd=tk.Entry(top,text='0') # 文本框,獲取圖片路徑

entryCd.pack()

label_tip=tk.Label(top,text="請檢查圖片路徑是否輸入正確!",height=2,\

width=40,fg="gray")

label_tip.pack()

btnCal=tk.Button(top,text="點擊生成九宮格圖片",fg="red",bg="yellow",command=cTofClicked) # 點擊回調函數

btnCal.pack()

top.mainloop() # 執行主循環

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

總結

以上是生活随笔為你收集整理的python语言编写一个生成九宫格图片的代码_python实现图片九宫格分割的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。