如何在我的 tkinter 项目中创建一个保存按钮来保存输出?
How to create a save button in my tkinter project to save the output?
我结合 tkinter 和 cv2 创建了将彩色图像转换为黑白图像的程序,但我无法理解如何添加保存黑白图像的按钮。怎么做?
# importing the Packages
import cv2 # importing cv2 package for converting the colored imaged to gray or vice versa
import tkinter as tk # importing tkinter package for creating Gui and its instance io created is tk
from tkinter import filedialog as fd # importing the filedialog package from tkinter to open the file default file explorer
from PIL import Image, ImageTk # Python imaginary library for performing operation on images
root = tk.Tk()
root.title("Color to blackNwhite Convertor")
root.config(bg='#FFFAFA')
def selectimage():
global panelA,panelB #used two global variables to create two panels
location = fd.askopenfilename()
if len(location) > 0:
image = cv2.imread(location)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# convert the images to PIL format...
image = Image.fromarray(image)
gray = Image.fromarray(gray)
# ...and then to ImageTk format
image = ImageTk.PhotoImage(image)
gray = ImageTk.PhotoImage(gray)
if panelA is None or panelB is None:
panelA = tk.Label(image=image)
panelA.image = image
panelA.pack(side="left", padx=10, pady=10)
panelB = tk.Label(image=gray)
panelB.image = gray
panelB.pack(side="right", padx=10, pady=10)
else:
panelA.configure(image=image)
panelB.configure(image=gray)
panelA.image = image
panelB.image = gray
panelA = None
panelB = None
btn = tk.Button(root, text="Select an image",font=('arial',10,'bold'),bg="cyan", command=selectimage)
btn.config(height=5,width=15)
btn.pack(side="bottom", fill="both", expand="yes", padx="10", pady="10")
# kick off the GUI
root.mainloop()
您可能想要创建一个后记文件:
canvas.postscript(file="file_name.ps", colormode='color')
然后使用 Pillow 将 postscript 文件保存为 png :
from PIL import Image
myImage = Image.open("file_name.ps")
myImage.save('imageName.png', 'png')
您应该保留Image.fromarray(gray)
返回的图像的引用,然后创建一个按钮来触发回调以在灰度图像上使用save()
保存灰度图像:
# importing the Packages
import cv2 # importing cv2 package for converting the colored imaged to gray or vice versa
import tkinter as tk # importing tkinter package for creating Gui and its instance io created is tk
from tkinter import filedialog as fd # importing the filedialog package from tkinter to open the file default file explorer
from PIL import Image, ImageTk # Python imaginary library for performing operation on images
root = tk.Tk()
root.title("Color to blackNwhite Convertor")
root.config(bg='#FFFAFA')
def selectimage():
global panelA,panelB #used two global variables to create two panels
global gray_image ### for saving reference to the gray image
location = fd.askopenfilename()
if len(location) > 0:
image = cv2.imread(location)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# convert the images to PIL format...
image = Image.fromarray(image)
gray_image = Image.fromarray(gray) ### save the gray image
# ...and then to ImageTk format
image = ImageTk.PhotoImage(image)
gray = ImageTk.PhotoImage(gray_image) ### use 'gray_image' instead of 'gray'
if panelA is None or panelB is None:
panelA = tk.Label(image=image)
panelA.image = image
panelA.pack(side="left", padx=10, pady=10)
panelB = tk.Label(image=gray)
panelB.image = gray
panelB.pack(side="right", padx=10, pady=10)
else:
panelA.configure(image=image)
panelB.configure(image=gray)
panelA.image = image
panelB.image = gray
panelA = None
panelB = None
gray_image = None
bframe = tk.Frame(root)
bframe.pack(side="bottom", fill="both", expand="yes")
btn = tk.Button(bframe, text="Select an image",font=('arial',10,'bold'),bg="cyan", command=selectimage)
btn.config(height=5,width=15)
btn.pack(side="left", fill="x", expand=1)
### function to save the gray image
def save_image():
if gray_image:
# ask the filename to be used as the output
filename = fd.asksaveasfilename()
if filename:
gray_image.save(filename) # save the gray image
tk.Button(bframe, text="Save image", font="arial 10 bold", bg="gold", width=15, height=5, command=save_image).pack(side="left", fill="x", expand=1)
# kick off the GUI
root.mainloop()
我结合 tkinter 和 cv2 创建了将彩色图像转换为黑白图像的程序,但我无法理解如何添加保存黑白图像的按钮。怎么做?
# importing the Packages
import cv2 # importing cv2 package for converting the colored imaged to gray or vice versa
import tkinter as tk # importing tkinter package for creating Gui and its instance io created is tk
from tkinter import filedialog as fd # importing the filedialog package from tkinter to open the file default file explorer
from PIL import Image, ImageTk # Python imaginary library for performing operation on images
root = tk.Tk()
root.title("Color to blackNwhite Convertor")
root.config(bg='#FFFAFA')
def selectimage():
global panelA,panelB #used two global variables to create two panels
location = fd.askopenfilename()
if len(location) > 0:
image = cv2.imread(location)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# convert the images to PIL format...
image = Image.fromarray(image)
gray = Image.fromarray(gray)
# ...and then to ImageTk format
image = ImageTk.PhotoImage(image)
gray = ImageTk.PhotoImage(gray)
if panelA is None or panelB is None:
panelA = tk.Label(image=image)
panelA.image = image
panelA.pack(side="left", padx=10, pady=10)
panelB = tk.Label(image=gray)
panelB.image = gray
panelB.pack(side="right", padx=10, pady=10)
else:
panelA.configure(image=image)
panelB.configure(image=gray)
panelA.image = image
panelB.image = gray
panelA = None
panelB = None
btn = tk.Button(root, text="Select an image",font=('arial',10,'bold'),bg="cyan", command=selectimage)
btn.config(height=5,width=15)
btn.pack(side="bottom", fill="both", expand="yes", padx="10", pady="10")
# kick off the GUI
root.mainloop()
您可能想要创建一个后记文件:
canvas.postscript(file="file_name.ps", colormode='color')
然后使用 Pillow 将 postscript 文件保存为 png :
from PIL import Image
myImage = Image.open("file_name.ps")
myImage.save('imageName.png', 'png')
您应该保留Image.fromarray(gray)
返回的图像的引用,然后创建一个按钮来触发回调以在灰度图像上使用save()
保存灰度图像:
# importing the Packages
import cv2 # importing cv2 package for converting the colored imaged to gray or vice versa
import tkinter as tk # importing tkinter package for creating Gui and its instance io created is tk
from tkinter import filedialog as fd # importing the filedialog package from tkinter to open the file default file explorer
from PIL import Image, ImageTk # Python imaginary library for performing operation on images
root = tk.Tk()
root.title("Color to blackNwhite Convertor")
root.config(bg='#FFFAFA')
def selectimage():
global panelA,panelB #used two global variables to create two panels
global gray_image ### for saving reference to the gray image
location = fd.askopenfilename()
if len(location) > 0:
image = cv2.imread(location)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# convert the images to PIL format...
image = Image.fromarray(image)
gray_image = Image.fromarray(gray) ### save the gray image
# ...and then to ImageTk format
image = ImageTk.PhotoImage(image)
gray = ImageTk.PhotoImage(gray_image) ### use 'gray_image' instead of 'gray'
if panelA is None or panelB is None:
panelA = tk.Label(image=image)
panelA.image = image
panelA.pack(side="left", padx=10, pady=10)
panelB = tk.Label(image=gray)
panelB.image = gray
panelB.pack(side="right", padx=10, pady=10)
else:
panelA.configure(image=image)
panelB.configure(image=gray)
panelA.image = image
panelB.image = gray
panelA = None
panelB = None
gray_image = None
bframe = tk.Frame(root)
bframe.pack(side="bottom", fill="both", expand="yes")
btn = tk.Button(bframe, text="Select an image",font=('arial',10,'bold'),bg="cyan", command=selectimage)
btn.config(height=5,width=15)
btn.pack(side="left", fill="x", expand=1)
### function to save the gray image
def save_image():
if gray_image:
# ask the filename to be used as the output
filename = fd.asksaveasfilename()
if filename:
gray_image.save(filename) # save the gray image
tk.Button(bframe, text="Save image", font="arial 10 bold", bg="gold", width=15, height=5, command=save_image).pack(side="left", fill="x", expand=1)
# kick off the GUI
root.mainloop()