本篇內(nèi)容介紹了“怎么用Python制作的九宮格圖片”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
10年的海豐網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。成都全網(wǎng)營銷的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整海豐建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。成都創(chuàng)新互聯(lián)公司從事“海豐網(wǎng)站設(shè)計(jì)”,“海豐網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
原理
1、用Python制作的九宮格圖像生成器包裝exe文件,用戶無需部署安裝Python的開發(fā)環(huán)境,即可在當(dāng)?shù)剡\(yùn)行該程序,快速生成九宮格圖像。
2、用PIL庫不斷畫小區(qū)域,切下來存儲(chǔ)成新的小圖片。
實(shí)例
假設(shè)每一個(gè)格子的寬和高分別是w、h,那么第row行(從0開始計(jì)數(shù)),第col列(從0開始計(jì)數(shù))的格子左上角坐標(biāo)和右下角坐標(biāo)分別是(col * w, row * h),(col * w + w, r * h + h)。
# -*- 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') #注意這個(gè)函數(shù)! #將之前的圖粘貼在新圖上,居中 if width > height:#原圖寬大于高,則填充圖片的豎直維度 #(x,y)二元組表示粘貼上圖相對(duì)下圖的起始位置,是個(gè)坐標(biāo)點(diǎn)。 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) #因?yàn)榕笥讶σ恍蟹?張圖。 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 # 點(diǎn)擊按鈕,實(shí)現(xiàn)圖片分割 def cTofClicked(): file_path=str(entryCd.get()) # 獲取要進(jìn)行分割的圖片路徑 image = Image.open(file_path) #image.show() image = fill_image(image) image_list = cut_image(image) save_images(image_list) labelcTof.config(text="九宮格圖片已生,請(qǐng)?jiān)诔绦蛩谀夸洸榭矗?quot;) # 窗體 top=tk.Tk() top.title('九宮格圖片生成器') labelcTof=tk.Label(top,text="請(qǐng)輸入要進(jìn)行轉(zhuǎn)換的圖片路徑:",height=4,\ width=40,fg="blue") labelcTof.pack() entryCd=tk.Entry(top,text='0') # 文本框,獲取圖片路徑 entryCd.pack() label_tip=tk.Label(top,text="請(qǐng)檢查圖片路徑是否輸入正確!",height=2,\ width=40,fg="gray") label_tip.pack() btnCal=tk.Button(top,text="點(diǎn)擊生成九宮格圖片",fg="red",bg="yellow",command=cTofClicked) # 點(diǎn)擊回調(diào)函數(shù) btnCal.pack() top.mainloop() # 執(zhí)行主循環(huán)
“怎么用Python制作的九宮格圖片”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
本文名稱:怎么用Python制作的九宮格圖片
網(wǎng)址分享:http://muchs.cn/article34/pidcpe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、網(wǎng)站設(shè)計(jì)公司、商城網(wǎng)站、域名注冊(cè)、網(wǎng)站建設(shè)、自適應(yīng)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)