python如何制作缩略图

 更新时间:2019年04月30日 09:50:55   作者:Bopeiod  
python如何制作缩略图?这篇文章主要为大家详细介绍了python制作缩略图的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

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()

效果图:

小编再分享一段代码:

#!/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))

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

相关文章

  • PyCharm插件开发实践之PyGetterAndSetter详解

    PyCharm插件开发实践之PyGetterAndSetter详解

    这篇文章主要介绍了PyCharm插件开发实践-PyGetterAndSetter,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-10-10
  • Python中装饰器兼容加括号和不加括号的写法详解

    Python中装饰器兼容加括号和不加括号的写法详解

    这篇文章主要给大家介绍了关于Python中装饰器兼容加括号和不加括号写法的相关资料,文中通过示例代码介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
    2017-07-07
  • 基于PyQT实现区分左键双击和单击

    基于PyQT实现区分左键双击和单击

    这篇文章主要介绍了基于PyQT实现区分左键双击和单击,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-05-05
  • Python的列表和元组详情

    Python的列表和元组详情

    这篇文章主要介绍了Python的列表和元组,列表和元组是python组常见的内置内省,下面文章我们讲围绕Python的列表和元组的相关资料展开话题,感兴趣的小伙伴以参考一下
    2021-10-10
  • python snownlp情感分析简易demo(分享)

    python snownlp情感分析简易demo(分享)

    下面小编就为大家带来一篇python snownlp情感分析简易demo(分享)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-06-06
  • Python中random模块生成随机数详解

    Python中random模块生成随机数详解

    本文给大家汇总了一下在Python中random模块中最常用的生成随机数的方法,有需要的小伙伴可以参考下
    2016-03-03
  • 总结Python使用过程中的bug

    总结Python使用过程中的bug

    今天给大家带来的是关于Python的相关知识,文章围绕着Python使用过程中的bug展开,文中有非常详细的介绍,需要的朋友可以参考下
    2021-06-06
  • python imutils包基本概念及使用

    python imutils包基本概念及使用

    python imutils包可以很简洁的调用opencv接口,轻松实现图像的平移,旋转,缩放,骨架化等操作,对python imutils包基本概念及使用方法感兴趣的朋友一起看看吧
    2021-07-07
  • Python常用内置函数和关键字使用详解

    Python常用内置函数和关键字使用详解

    在Python中有许许多多的内置函数和关键字,它们是我们日常中经常可以使用的到的一些基础的工具,可以方便我们的工作。本文将详细讲解他们的使用方法,需要的可以参考一下
    2022-05-05
  • Python字符串和其常用函数合集

    Python字符串和其常用函数合集

    这篇文章主要给大介绍Python字符串和分享其常用函数合集,字符串、首字母大写定义、所有字母大写、所有字母小写等函数,具有一定的参考价值,需要的朋友可以参考一下
    2022-03-03

最新评论