问题
additional question I'm working on a project for a family member, I have fairly limited experience with Python. I have written my script on Python, and it works exactly how I need. The code takes a photo and adds a red border to either the length or the width to create a square.
My issue is my family member does not use Python, and after some light research I have come across tkinter for a GUI. I would like to have a text box where the image name can be typed, and that runs the code from the input line.
I have it where you type MyImage.PNG into the console and it saves the border version into the same file. Also, I understand how to design with tkinter--just not how to actually execute my script with the textbox/buttons. Any advice on how to achieve this?
I would like to call images from a file, without having to individually type the image name. Is that possible through an open loop possibly? Or even using a Tkinter widget to read the images file, create a list from the file, and then individually be able to select each image and hit enter.
Thank you!
from PIL import Image, ImageOps
import math
original = input("Please enter an input: ")
im = Image.open(original)
print(im.size)
print(type(im.size))
w, h = im.size
print('width: ', w)
print('height:', h)
if w>h:
x = w - h
b = math.floor(x/2)
a = 0
if w<h:
x = h - w
a = math.floor(x/2)
b = 0
def add_border(input_image, output_image, border, color=0):
img = Image.open(input_image)
if isinstance(border, int) or isinstance(border, tuple):
bimg = ImageOps.expand(img, border=border, fill=color)
else:
raise RuntimeError('Border is not an integer or tuple!')
bimg.save(output_image)
if __name__ == '__main__':
add_border(original,
output_image= 'border' + original,
border=(a,b,a,b),
color='indianred')
回答1:
Tkinter has an input field function so you could do something like:
from tkinter import *
def Get_Name_Of_Picture():
original=str(PicName.get())
PicName=Entry(screen)
PicName.pack()
Button(screen, text='Enter', command=Get_Name_Of_Picture).pack(side=LEFT)
Something like that(you might have to put the rest of the code in the Get_Name_Of_Picture function) but this will allow the user to type an input into an entry field, and once they are done hit the enter button which will get the string of what was typed and so on.
If you need more advice just comment on this answer and I will try to help.
来源:https://stackoverflow.com/questions/62856536/running-python-code-through-user-friendly-interface