Home>

I want to achieve
I want to trim an image using tkinter. I want to move the line with the slide bar and save the image surrounded by the line, but an error occurs and I cannot save it.

Error code

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "C:\Users\anaconda3\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:/Users/Desktop/tkinter.py", line 15, in onSaveButton
    temp_image=Image.open(img)
  File "C:\Users\AppData\Local\Continuum\anaconda3\lib\site-packages\PIL\Image.py", line 2772, in open
    fp = io.BytesIO(fp.read())
AttributeError:'PhotoImage' object has no attribute'read'
import tkinter as tk
import tkinter.ttk as ttk
from PIL import Image, ImageTk
root=tk.Tk()
def onSaveButton():
    # Capture display image
    temp_image=Image.open(img)
    # Cut out at the selected position
    cropped_image=temp_image.crop((left.get(), top.get(), right.get(), bottom.get()))
    #Save
    cropped_image.save("c:/Desktop/a.jpg")
def onSliderLeft(args):
    canvas.delete("left_line")
    canvas.create_line(left.get(), 0, left.get(),500, tag="left_line", fill='green')
def onSliderRight(args):
    canvas.delete("right_line")
    canvas.create_line(right.get(), 0, right.get(),500, tag="right_line", fill='red')
def onSliderTop(args):
    canvas.delete("top_line")
    canvas.create_line(0, top.get(), 500,top.get(), tag="top_line", fill='green')
def onSliderBottom(args):
    canvas.delete("bottom_line")
    canvas.create_line(0, bottom.get(),500,bottom.get(), tag="bottom_line", fill='red')
#Define window title
root.title('a')
#Define window size
root.geometry('1000x700')#horizontal x vertical
#Image convert from opencv to pil
#image_bgr=cv2.imread("c:/temp/0327_/2_1.jpg")
#image_rgb=cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB)
#image_pil=Image.fromarray(image_rgb)
#image_tk=ImageTk.PhotoImage(image_pil)
img=Image.open("c:/temp/rabel3.jpg")
img=ImageTk.PhotoImage(img)

#Create a kanpus for displaying images
canvas=tk.Canvas(root, width=500, height=500)
canvas.grid(row=0, column=0)

#Create various buttons
# Button to save the selected area
button_save=tk.Button(root, text="save", command=onSaveButton)
button_save.grid(row=4,column=6)
# Reserve variable for slider
left=0
right=0
top=0
bottom=0
# Draw a line to cut out the image on the canvas
canvas.create_line(left,0,left,500,fill='green',width=5)
canvas.create_line(right,0,right,500,fill='red')
canvas.create_line(0,top,500,top,fill='green')
canvas.create_line(0,bottom,500,bottom,fill='red')

# Slider for cropping images
left=ttk.Scale(root, orient='h',from_=0,to_=500,length=150,command=onSliderLeft)
left.grid(row=3,column=0)

right=ttk.Scale(root,orient='h',from_=0,to_=500,length=150,command=onSliderRight)
right.grid(row=4,column=0)

top=ttk.Scale(root, orient='v',from_=0,to_=500,length=150,command=onSliderTop)
top.grid(row=4,column=2)

bottom=ttk.Scale(root,orient='v',from_=0,to_=500,length=150,command=onSliderBottom)
bottom.grid(row=4,column=3)

#Move according to the image displaying the line to cut out the image
left.set(0)
right.set(0)
top.set(0)
bottom.set(0)
#Display images on campus
canvas.create_image(0,0,image=img, anchor='nw')
root.mainloop()
  • Answer # 1

    A PhotoImage object is assigned to the img variable,
    The PhotoImage object cannot be opened with the Image.open function.

    to the Image.open function in the onSaveButton function processing
    It works if you pass the image path specified in the process of displaying the image at the beginning.

    image_name="c:/temp/rabel3.jpg"
    def onSaveButton():
        # Capture display image
        temp_image=Image.open(image_name)
        # Cut out at the selected position
        cropped_image=temp_image.crop((left.get(), top.get(), right.get(), bottom.get()))
        #Save
        cropped_image.save("c:/Desktop/a.jpg")