溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

python如何制作縮略圖

發(fā)布時(shí)間:2020-10-10 11:20:45 來(lái)源:腳本之家 閱讀:213 作者:Bopeiod 欄目:開發(fā)技術(shù)

本文實(shí)例為大家分享了python制作縮略圖的具體代碼,供大家參考,具體內(nèi)容如下

import cv2 #導(dǎo)入opencv模塊
from tkinter import * #導(dǎo)入tkinter模塊
from tkinter import ttk #tkinter最新的主題部件
from PIL import Image

#初始化模塊
root = Tk()
root.title('Pt')
root.geometry('600x300')
#查找圖片路徑,成功則顯示圖片
def searchPicture():
 location = locOfPicture.get()
 img = cv2.imread(location)
 cv2.imshow("Image",img)

#生成縮略圖
def setPicture():
 # 獲取圖片路徑
 location = locOfPicture.get()
 # 對(duì)圖片進(jìn)行操作
 im = Image.open(location)
 im.thumbnail((int(heightOfPicture.get()),int(widthOfPicture.get())))
 im.save(nameOfImg.get(),'JPEG')


label1 = ttk.Label(root,text='選擇圖片')
label2 = ttk.Label(root,text='長(zhǎng):')
label3 = ttk.Label(root,text='寬:')
label4 = ttk.Label(root,text='文件名')

#存儲(chǔ)輸入框中輸入的變量
locOfPicture = StringVar()
heightOfPicture= StringVar()
widthOfPicture = StringVar()
nameOfImg = StringVar()

entry1 = ttk.Entry(root,textvariable = locOfPicture,width=50)
entry2 = ttk.Entry(root,textvariable=heightOfPicture,width=10)
entry3 = ttk.Entry(root,textvariable=widthOfPicture,width=10)
entry4 = ttk.Entry(root,textvariable=nameOfImg,width=25)

button1 = ttk.Button(root,text='確定',command=searchPicture)
button2 = ttk.Button(root,text='確定生成',command=setPicture)

#進(jìn)行界面布局
label1.grid(column=0,row=0)
entry1.grid(column=1,row=0,columnspan=3)
button1.grid(column=4,row=0)
label2.grid(column=0,row=1)
entry2.grid(column=1,row=1)
label3.grid(column=2,row=1)
entry3.grid(column=3,row=1)
entry4.grid(column=1,row=2,columnspan=2)
button2.grid(column=3,row=2)

root.mainloop()

效果圖:

python如何制作縮略圖

小編再分享一段代碼:

#!/usr/bin/env python
#coding=utf-8
'''
Created on 2012-6-2
 
@author: fatkun
'''
import Image
import os
import sys
import glob
import time
 
def make_thumb(path, thumb_path, size):
 """生成縮略圖"""
 img = Image.open(path)
 width, height = img.size
 # 裁剪圖片成正方形
 if width > height:
  delta = (width - height) / 2
  box = (delta, 0, width - delta, height)
  region = img.crop(box)
 elif height > width:
  delta = (height - width) / 2
  box = (0, delta, width, height - delta)
  region = img.crop(box)
 else:
  region = img
 
 # 縮放
 thumb = region.resize((size, size), Image.ANTIALIAS)
 
 base, ext = os.path.splitext(os.path.basename(path))
 filename = os.path.join(thumb_path, '%s_thumb.jpg' % (base,))
 print filename
 # 保存
 thumb.save(filename, quality=70)
 
def merge_thumb(files, output_file):
 """合并圖片"""
 imgs = []
 width = 0
 height = 0
 
 # 計(jì)算總寬度和長(zhǎng)度
 for file in files:
  img = Image.open(file)
  if img.mode != 'RGB':
   img = img.convert('RGB')
  imgs.append(img)
  if img.size[0] > width:
   width = img.size[0]
  height += img.size[1]
 
 # 新建一個(gè)白色底的圖片
 merge_img = Image.new('RGB', (width, height), 0xffffff)
 cur_height = 0
 for img in imgs:
  # 把圖片粘貼上去
  merge_img.paste(img, (0, cur_height))
  cur_height += img.size[1]
 
 merge_img.save(output_file, quality=70)
 
if __name__ == '__main__':
 ROOT_PATH = os.path.abspath(os.path.dirname(__file__))
 IMG_PATH = os.path.join(ROOT_PATH, 'img')
 THUMB_PATH = os.path.join(IMG_PATH, 'thumbs')
 if not os.path.exists(THUMB_PATH):
  os.makedirs(THUMB_PATH)
 
 # 生成縮略圖
 files = glob.glob(os.path.join(IMG_PATH, '*.jpg'))
 begin_time = time.clock()
 for file in files:
  make_thumb(file, THUMB_PATH, 90)
 end_time = time.clock()
 print ('make_thumb time:%s' % str(end_time - begin_time))
 
 # 合并圖片
 files = glob.glob(os.path.join(THUMB_PATH, '*_thumb.jpg'))
 merge_output = os.path.join(THUMB_PATH, 'thumbs.jpg')
 begin_time = time.clock()
 merge_thumb(files, merge_output)
 end_time = time.clock()
 print ('merge_thumb time:%s' % str(end_time - begin_time))

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI