调整图像文件夹的大小

时间:2021-06-14 02:09:20

标签: python tkinter

我想在文本框中写入高度和宽度后调整图像文件夹的大小,我需要使用 tkinter 保存这个调整大小的文件夹。我从这段代码开始:

import tkinter as tk 
from PIL import ImageTk, Image
from tkinter import filedialog
import os

root = Tk()
root.geometry("800x600")
src_file = StringVar()
imgWidth = IntVar()
imgHeight = IntVar()

def open_filedialog():
      global  folder
      folder = filedialog.askdirectory()
      src_file.set(folder) 

label = Label(wrapper, text="Source File")
label.pack(side=LEFT, padx=10, pady=10)

entry = Entry(wrapper, textvariable = src_file,width="50")
entry.pack(side = LEFT, padx=10, pady=10)

select_btn = Button(wrapper , text="Select Image",command= open_filedialog)
select_btn.pack(side= LEFT, padx=10,pady=10)

width_box = Entry(imageResize_wrapper,textvariable = imgWidth)
width_box.pack(side=LEFT, padx=10, pady=10)

height_box = Entry(imageResize_wrapper,textvariable = imgHeight)
height_box.pack(side=LEFT, padx=10, pady=10)

set_btn = Button(imageResize_wrapper, text = "Set",font=(
 "Consaolas", 16),fg="#000", command = set_imageSize)
set_btn.pack(side= LEFT, padx=10, pady=10)

root.mainloop()

2 个答案:

答案 0 :(得分:0)

这是一个解决方案:

from tkinter import Tk, Label, Entry, Button, Frame
from tkinter.filedialog import askdirectory
from PIL import Image
import os


def get_dir():
    directory = askdirectory()
    dir_lbl.config(text=directory)


def convert_images():
    source = dir_lbl.cget('text')
    extension = '.png'
    w = int(width.get())
    h = int(height.get())
    image_list = [source + '/' + file for file in os.listdir(path=source) if extension in file]
    new_dir = source + '_converted'
    os.mkdir(new_dir)

    for image in image_list:
        print(image)
        img = Image.open(image)
        img = img.resize((w, h), Image.ANTIALIAS)
        img.save(new_dir + f'/{image.split("/")[-1]}')


root = Tk()
root.geometry('500x300')

dir_frame = Frame(root)
dir_frame.pack(pady=10)

Button(dir_frame, text='Choose Directory', command=get_dir).pack()
dir_lbl = Label(dir_frame, text='')
dir_lbl.pack()

settings_frame = Frame(root)
settings_frame.pack(pady=10)

width_frame = Frame(settings_frame)
width_frame.pack()
Label(width_frame, text='Width: ').pack(side='left')
width = Entry(width_frame)
width.pack(side='right')

height_frame = Frame(settings_frame)
height_frame.pack()
Label(height_frame, text='Height: ').pack(side='left')
height = Entry(height_frame)
height.pack(side='right')

Button(settings_frame, text='Convert All', command=convert_images).pack(pady=10)

root.mainloop()

首先要提到的是,可以添加许多其他选项供用户选择以及如何处理图片,例如允许单步浏览每个图像或可能允许选择他们自己的目录名称,但简单的东西在这里。

说明 首先导入所有必要的东西:库、模块、类和东西

然后定义一个函数来获取用户将选择的目录。这是一个简单的过程:使用内置方法调用“选择器”并将返回的字符串作为目录的路径放在标签上,以便向用户显示他选择的内容并存储引用。

定义了最重要的另一个函数。首先使用 .cget 中小部件的 tkinter 方法从标签中获取文本(也可以使用 sth 之类的 dir_lbl['text'] 或 sth else)。那将是源目录。然后定义一个扩展只是为了可读性,如果将来添加一个参数,更改内容会更容易。接下来获取用户输入的调整大小图像的宽度和高度(应该添加一些内容以防止用户输入错误数据),然后创建该目录中所有图像的列表,这是使用列表推导完成的。 (如果您不了解它们,您应该阅读,因为出于这些原因,它们非常棒)但基本上它的作用是添加文件名中包含 .png 的所有文件(理论上类似于 {{1 }} 也可以添加,但如果真的会发生这种情况非常罕见,再次可以改进这一点)。然后通过在当前目录名的末尾添加 image.png.jpg 来创建一个新的目录名(和路径)(同样可以将其更改为用户选择或某事)。然后使用 "_converted" 模块创建一个新目录(可能仅适用于 Windows,因此可以使用更跨平台的东西)。然后遍历所有这些图像,打开它们并调整它们的大小,然后保存在新目录中。

其余的代码几乎都是纯粹的 os,没有太多可解释的。

如果您有任何问题,请咨询他们。

答案 1 :(得分:0)

我试过这个代码。它对我有用。

from tkinter import *
from PIL import ImageTk, Image
from tkinter import filedialog
import os, sys, glob


root = Tk()
root.geometry("800x600")

src_file = StringVar()
imgWidth = IntVar()
imgHeight = IntVar()


def open_filedialog():
    folder = filedialog.askdirectory( parent = root)
    src_file.set(folder)
    

#resize image
def set_image_size():
    root_dir = src_file  
    nw = int(imgWidth.get())
    nh = int(imgHeight.get()) 
    for file in glob.iglob(root_dir.get() +'**/*.jpg', recursive=True) : 
        print(file)
        im = Image.open(file)
        imResize = im.resize((nw,nh), Image.ANTIALIAS)
        imResize.save(file)    
        imResize.save(file)

    
           

wrapper = LabelFrame(root,text="Source File")
wrapper.pack(fill="both",expand="yes",padx=20,pady=20)

imageResize_wrapper = LabelFrame(root,text="Resize")
imageResize_wrapper.pack(fill="both",expand="yes",padx=20,pady=20)

Image_wrapper = LabelFrame(root, text="Image")
Image_wrapper.pack(fill="both",expand="yes",padx="20",pady=20)

#wrapper 
label = Label(wrapper, text="Source File")
label.pack(side=LEFT, padx=10, pady=10)

entry = Entry(wrapper, textvariable = src_file,width="50")
entry.pack(side = LEFT, padx=10, pady=10)

select_btn = Button(wrapper , text="Select Image",command= open_filedialog)
select_btn.pack(side= LEFT, padx=10,pady=10)
# wrapper 

#image Resize wrapper 
width_lbl = Label(imageResize_wrapper, text="width", font=(
    "Consaolas", 16),fg="#000", bg="#DF9B6D")
width_lbl.pack(side=LEFT,padx=10, pady=10)

width_box = Entry(imageResize_wrapper,textvariable = imgWidth)
width_box.pack(side=LEFT, padx=10, pady=10)

height_lbl = Label(imageResize_wrapper, text="Height", font=(
    "Consaolas", 16),fg="#000", bg="#DF9B6D")
height_lbl.pack(side=LEFT,padx=10, pady=10)

height_box = Entry(imageResize_wrapper,textvariable = imgHeight)
height_box.pack(side=LEFT, padx=10, pady=10)

set_btn = Button(imageResize_wrapper, text = "resize and save",font=(
    "Consaolas", 16),fg="#000", command = set_image_size)
set_btn.pack(side= LEFT, padx=10, pady=10)



root.mainloop()

但我需要在 glob.iglob() 中包含更多扩展名(.jpg、.jpeg 等)。如何编辑上面的代码来实现它。

相关问题