<p>您应该保留由<code>Image.fromarray(gray)</code>返回的图像的引用,然后创建一个按钮来触发回调以使用灰色图像上的<code>save()</code>保存灰色图像:</p>
<pre class="lang-py prettyprint-override"><code># 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()
</code></pre>