脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python如何制作缩略图

python如何制作缩略图

2021-06-22 00:21Bopeiod Python

python如何制作缩略图?这篇文章主要为大家详细介绍了python制作缩略图的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了python制作缩略图的具体代码,供大家参考,具体内容如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import cv2 #导入opencv模块
from tkinter import * #导入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()
 # 对图片进行操作
 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='长:')
label3 = ttk.label(root,text='宽:')
label4 = ttk.label(root,text='文件名')
 
#存储输入框中输入的变量
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)
 
#进行界面布局
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如何制作缩略图

小编再分享一段代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/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
 
 # 计算总宽度和长度
 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]
 
 # 新建一个白色底的图片
 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))

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/Bopeiod/article/details/51816392

延伸 · 阅读

精彩推荐