python图片等比例压缩

# -*- coding: utf-8 -*-
"""
Created on Wed Nov  6 08:56:57 2019

@author: 01
"""

# -*- coding:utf-8 -*-

import MySQLdb
from hashlib import sha1
import tkinter
import tkinter.filedialog
import os
from PIL import ImageGrab
from time import sleep
from tkinter import *
import cv2
from PIL import Image,ImageTk
from tkinter.filedialog import askopenfilename
import json
import codecs
from tkinter.filedialog import askdirectory
import tkinter.messagebox
import sys,time
from tkinter import ttk
import numpy as np 
#登录界面===============================================
def jiunu():      
      
        
        def cv_imread(filePath):  
            cv_img=cv2.imdecode(np.fromfile(filePath,dtype=np.uint8),-1)  
            ## imdecode读取的是rgb,如果后续需要opencv处理的话,需要转换成bgr,转换后图片颜色会变化    ##cv_img=cv2.cvtColor(cv_img,cv2.COLOR_RGB2BGR)   
            return cv_img
        # 创建tkinter主窗口
        root = tkinter.Tk()
        
        root.title('图片处理')
        
        # 指定主窗口位置与大小
        root.geometry('1000x800+100+50')   # width x height + widthoffset + heightoffset
        
        # 不允许改变窗口大小
        root.resizable(False, False)
        root.focusmodel()
        def choosepic():
            path_ = askopenfilename()
            path.set(path_)
            img_open = Image.open(file_entry.get())
            img_open=img_open.resize((350,350))
            img = ImageTk.PhotoImage(img_open)
            image_label.config(image=img)
            image_label.image = img  # keep a reference  
            treeview.insert('', 0, values=(path_))
        def choosepic1():
            pat = askdirectory()
            path.set(pat)
            j=0
           
        def choosepic2():
            pat1 = askdirectory()
            a=file_entry.get()
            img_list=os.listdir(a)
            ade=0
            for i in img_list:
                ww=a+"/"+i
                img_array=cv_imread(ww)  
                print("chaungkuan",img_array.shape[0])
                bili=int(int(img_array.shape[1]*300)/int(img_array.shape[0]))
                
                new_array=cv2.resize(img_array,(bili,300))
               
                save_path=pat1+"/"+str(ade)+".jpg"
                cv2.imwrite(save_path,new_array)
                ade=ade+1
            xy_text1.set("执行完毕")
        xy_text1 = StringVar() 
        path = StringVar()
        tkinter.Button(root, text = '选择图片路径' ,command=choosepic1).place(x = 440, y = 155,w = 100, h = 40)
        file_entry = Entry(root, state='readonly', text=path)
        file_entry.place(x=20, y=55,width = 850, height = 40)
        label1 =Label(root, textvariable=xy_text1,fg = 'blue')
        label1.place(x=80, y=555,width = 850, height = 40) 
        
        path1 = StringVar()
        tkinter.Button(root, text = '选择保存图片路径' ,command=choosepic2).place(x = 440, y = 355,w = 100, h = 40)
        file_entry1 = Entry(root, state='readonly', text=path1)
        file_entry1.place(x=20, y=255,width = 850, height = 40)           
             
        root.mainloop()
jiunu()





 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值