I have a python script named crop.py, when I run it via visual studio code it doesn't report errors, but nothing happens, like the script is expecting some other command to work. The code allows you to select an image, crop it according to four points and then save it. Here the code:
import tkinter as tk
from tkinter import filedialog
from PIL import Image, ImageTk
class App:
def __init__(self, root):
self.root = root
self.image = None
self.image_tk = None
self.points = []
# create a canvas for the image
self.canvas = tk.Canvas(root, width=600, height=600)
self.canvas.pack()
# bind the left mouse button click to the canvas
self.canvas.bind("<Button-1>", self.select_point)
# create a button to open an image file
self.open_button = tk.Button(root, text="Open Image", command=self.open_image)
self.open_button.pack()
# create a button to crop the image
self.crop_button = tk.Button(root, text="Crop Image", command=self.crop_image)
self.crop_button.pack()
def select_point(self, event):
# add the selected point to the list
self.points.append((event.x, event.y))
# draw a circle at the selected point
self.canvas.create_oval(event.x - 5, event.y - 5, event.x 5, event.y 5, fill="red")
# if we have selected four points, unbind the left mouse button
if len(self.points) == 4:
self.canvas.unbind("<Button-1>")
def open_image(self):
# open a file dialog to select an image file
file_path = filedialog.askopenfilename()
# open the image file
self.image = Image.open(file_path)
# resize the image to fit the canvas
self.image = self.image.resize((600, 600))
# convert the image to a PhotoImage object
self.image_tk = ImageTk.PhotoImage(self.image)
# draw the image on the canvas
self.canvas.create_image(0, 0, anchor="nw", image=self.image_tk)
def crop_image(self):
# check if we have selected four points
if len(self.points) != 4:
return
# get the top-left and bottom-right points
x1, y1 = self.points[0]
x2, y2 = self.points[1]
x3, y3 = self.points[2]
x4, y4 = self.points[3]
# find the top-left and bottom-right coordinates of the cropped image
left = min(x1, x2, x3, x4)
top = min(y1, y2, y3, y4)
right = max(x1, x2, x3, x4)
bottom = max(y1, y2, y3, y4)
# crop the image
cropped_image= image.crop((left,top,right,bottom))
#save the cropped image
cropped_image.save(filedialog.asksaveasfilename())
I haven't worked with python for too long, so sorry if the question is trivial. Could someone tell me how to run the code? (open the image, crop it etc..)
CodePudding user response:
Just instantiate it and hand over a tk object.
root = tk.TK()
app = App(root)
CodePudding user response:
You have to create an instance of the class. When you have a class named Car
, it can have:
Some properties:
color
fuel_type
brand
- ...
And some methods (functions):
drive_forward
refuel
- ...
You access the properties and methods using <instance_name>.<property_or_method_name>
In-class use self.<property_or_method_name>
You dont't have to use self
but it's recommended, almost all programmers use it as self
. You can name it as you want, it's just the first argument of the function.
Here is the sample:
class Car: # create the class
def __init__(self, brand): # initialize the instance
# some properties:
self.brand = brand # accessing through ´self.´ - instance's property
self.color = "red"
self.fuel_type = "diesel"
self.fuel_amount = 0
def drive(self):
print("Vrrrm.")
def refuel(self, fuel_amount): # another method
self.fuel_amount = fuel_amount
c = Car("Volvo") # creating an instance of the class
print(c.fuel_amount) # accessing through <instance_name>.<property_or_method_name>
# returns 0
c.refuel(10) # increase the fuel by 10
print(c.fuel_amount)
# returns 10
Notice: do not call the functions with the self
argument. It is autofilled with an object of the instance.
In your case:
window = tk.Tk() # create a window
app = App(window) # create an class instance
Hope it helps.