Skip to content

Commit 5b98f27

Browse files
Merge pull request #899 from akash435/imgcirclecropbranch
Added Circular Image Cropper in ImageProcessing Scripts
2 parents a960125 + 9746701 commit 5b98f27

File tree

16 files changed

+290
-0
lines changed

16 files changed

+290
-0
lines changed
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# ✔ CIRCULAR IMAGE CROPPER
2+
- ### An Circular Image Cropper is an image processing application created in python with tkinter gui and OpenCv library.
3+
- ### In this application user can select an image, and will be able to crop the image circularly.
4+
- ### Also user will be able to see the preview of the original image as well as the cropped image.
5+
- ### User can also reset the cropped image to original format using RESET button.
6+
- ### Also after cropping user will be able to save the cropped image to any where on the local system.
7+
8+
****
9+
10+
# REQUIREMENTS :
11+
- ### python 3=
12+
- ### tkinter module
13+
- ### filedialog from tkinter
14+
- ### messagebox
15+
- ### from PIL import Image, ImageTk, ImageDraw
16+
- ### numpy
17+
- ### cv2
18+
19+
****
20+
21+
# HOW TO Use it :
22+
- ### User just need to download the file, and run the circular_image_cropper.py, on local system.
23+
- ### After running a GUI window appears, where user can see button like START and EXIT.
24+
- ### After user clicks on the START button a main application window will appear, where user needs to select an image file using SELECT button from the local system.
25+
- ### After selecting the image, two images will appear on screen one on left side, which is original and one on write in which Circular Cropped format will be shown.
26+
- ### Now user can start cropping of selected image using CIRCULAR CROP button.
27+
- ### After segmenting user can also save the cropped image to any location in local system using SAVE button.
28+
- ### User can also reset the cropped image to original format using RESET button.s
29+
- ### Also there is exit button, clicking on which we get a exit dialog box asking the permission to exit.
30+
31+
# Purpose :
32+
- ### This scripts helps user to easily crop any image in circular format and even save it anywhere in local system.
33+
34+
# Compilation Steps :
35+
- ### Install tkinter, PIL, numpy, cv2
36+
- ### After that download the code file, and run circular_image_cropper.py on local system.
37+
- ### Then the script will start running and user can explore it by selecting any image and cropping it circularly.
38+
39+
****
40+
41+
# SCREENSHOTS :
42+
****
43+
44+
<p align="center">
45+
<img width = 1000 src="Images/1.jpg" /><br>
46+
<img width = 1000 src="Images/2.jpg" /><br>
47+
<img width = 1000 src="Images/3.jpg" /><br>
48+
<img width = 1000 src="Images/4.jpg" /><br>
49+
<img width = 1000 src="Images/5.jpg" /><br>
50+
<img width = 1000 src="Images/6.jpg" /><br>
51+
<img width = 1000 src="Images/7.jpg" /><br>
52+
<img width = 1000 src="Images/8.jpg" /><br>
53+
<img width = 1000 src="Images/9.jpg" /><br>
54+
<img width = 1000 src="Images/10.jpg" /><br>
55+
<img width = 1000 src="Images/11.jpg" /><br>
56+
<img width = 1000 src="Images/12.jpg" /><br>
57+
</p>
58+
59+
****
60+
61+
# Name :
62+
- ### Akash Ramanand Rajak
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
2+
# Circular Image Cropper
3+
4+
# imported necessary library
5+
import tkinter
6+
from tkinter import *
7+
import tkinter as tk
8+
import tkinter.messagebox as mbox
9+
from tkinter import filedialog
10+
from PIL import Image, ImageTk, ImageDraw
11+
import numpy as np
12+
import cv2
13+
14+
15+
# Main Window
16+
frame = Tk()
17+
frame.title('Circular Image Cropper')
18+
frame.geometry('1300x750')
19+
# frame.configure(bg = "white")
20+
21+
22+
# image on the main window
23+
path = "Images/front.jpg"
24+
# Creates a Tkinter-compatible photo image, which can be used everywhere Tkinter expects an image object.
25+
img1 = ImageTk.PhotoImage(Image.open(path))
26+
# The Label widget is a standard Tkinter widget used to display a text or image on the screen.
27+
panel = tk.Label(frame, image = img1)
28+
panel.place(x = 270, y = 150)
29+
30+
# starting label
31+
start1 = Label(frame, text='CIRCULAR IMAGE CROPPER', font=("Arial", 55,"underline"),fg="magenta")
32+
start1.place(x=120,y=10)
33+
34+
def start_fun():
35+
frame.destroy()
36+
37+
# creating an exit button
38+
prevB = Button(frame, text='START', command=start_fun, font=("Arial", 25), bg = "light green", fg = "blue", borderwidth=3, relief="raised")
39+
prevB.place(x = 200, y = 620)
40+
41+
# defined exit_win function, to show a exit dialog box when tried to exit
42+
def exit_win():
43+
if mbox.askokcancel("Exit", "Do you want to exit?"):
44+
frame.destroy()
45+
46+
# creating an exit button
47+
prevB = Button(frame, text='EXIT', command=exit_win, font=("Arial", 25), bg = "red", fg = "blue", borderwidth=3, relief="raised")
48+
prevB.place(x = 970, y = 620)
49+
50+
# this is done to show the exit dialog box when tried to exit from the main window, using the top-roght close button of titlebar
51+
frame.protocol("WM_DELETE_WINDOW", exit_win)
52+
frame.mainloop()
53+
54+
#created main window
55+
window = Tk()
56+
window.geometry("1300x750")
57+
window.title("Circular Image Cropper")
58+
59+
# defined variable
60+
global count, emig
61+
# global bright, con
62+
# global frp, tname # list of paths
63+
frp = []
64+
tname = []
65+
con = 1
66+
bright = 0
67+
panelB = None
68+
panelA = None
69+
70+
# function defined to get the path of the image selected
71+
def getpath(path):
72+
a = path.split(r'/')
73+
# print(a)
74+
fname = a[-1]
75+
l = len(fname)
76+
location = path[:-l]
77+
return location
78+
79+
# function defined to get the folder name from which image is selected
80+
def getfoldername(path):
81+
a = path.split(r'/')
82+
# print(a)
83+
name = a[-1]
84+
return name
85+
86+
# function defined to get the file name of image is selected
87+
def getfilename(path):
88+
a = path.split(r'/')
89+
fname = a[-1]
90+
a = fname.split('.')
91+
a = a[0]
92+
return a
93+
94+
# function defined to open the image file
95+
def openfilename():
96+
filename = filedialog.askopenfilename(title='"pen')
97+
return filename
98+
99+
# function defined to open the selected image
100+
def open_img():
101+
global x, panelA, panelB
102+
global count, eimg, location, filename
103+
count = 0
104+
x = openfilename()
105+
img = Image.open(x)
106+
eimg = img
107+
img = ImageTk.PhotoImage(img)
108+
temp = x
109+
location = getpath(temp)
110+
filename = getfilename(temp)
111+
# print(x)
112+
if panelA is None or panelB is None:
113+
panelA = Label(image=img)
114+
panelA.image = img
115+
panelA.pack(side="left", padx=50, pady=12)
116+
panelB = Label(image=img)
117+
panelB.image = img
118+
panelB.pack(side="right", padx=50, pady=12)
119+
else:
120+
panelA.configure(image=img)
121+
panelB.configure(image=img)
122+
panelA.image = img
123+
panelB.image = img
124+
125+
# function defined for cropping image in circle
126+
def crop_fun():
127+
global x
128+
# Open the input image as numpy array, convert to RGB
129+
img = Image.open(x).convert("RGB")
130+
npImage = np.array(img)
131+
h, w = img.size
132+
133+
# Create same size alpha layer with circle
134+
alpha = Image.new('L', img.size, 0)
135+
draw = ImageDraw.Draw(alpha)
136+
draw.pieslice([0, 0, h, w], 0, 360, fill=255)
137+
138+
# Convert alpha Image to numpy array
139+
npAlpha = np.array(alpha)
140+
141+
# Add alpha layer to RGB
142+
npImage = np.dstack((npImage, npAlpha))
143+
144+
# Save with alpha
145+
Image.fromarray(npImage).save('Cropped_Image.png')
146+
147+
imge = Image.open('Cropped_Image.png')
148+
imge = ImageTk.PhotoImage(imge)
149+
panelB.configure(image=imge)
150+
panelB.image = imge
151+
mbox.showinfo("Cropping Status", "Image Cropped in Circle successfully.")
152+
153+
# function defined to reset the edited image to original one
154+
def reset():
155+
global x
156+
# print(x)
157+
image = cv2.imread(x)[:, :, ::-1]
158+
global count, eimg
159+
count = 6
160+
global o6
161+
o6 = image
162+
image = Image.fromarray(o6)
163+
eimg = image
164+
image = ImageTk.PhotoImage(image)
165+
panelB.configure(image=image)
166+
panelB.image = image
167+
mbox.showinfo("Success", "Image reset to original format!")
168+
169+
# function defined to same the edited image
170+
def save_img():
171+
global location, filename, eimg
172+
print(filename)
173+
# eimg.save(location + filename + r"_edit.png")
174+
filename = filedialog.asksaveasfile(mode='w', defaultextension=".jpg")
175+
if not filename:
176+
return
177+
eimg.save(filename)
178+
mbox.showinfo("Success", "Cropped Image Saved Successfully!")
179+
180+
181+
182+
# top label
183+
start1 = tk.Label(text = "CIRCULAR IMAGE\nCROPPER", font=("Arial", 50, "underline"), fg="magenta") # same way bg
184+
start1.place(x = 350, y = 10)
185+
186+
# original image label
187+
start1 = tk.Label(text = "Original\nImage", font=("Arial", 40), fg="magenta") # same way bg
188+
start1.place(x = 100, y = 300)
189+
190+
# edited image label
191+
start1 = tk.Label(text = "Circular Cropped\nImage", font=("Arial", 40), fg="magenta") # same way bg
192+
start1.place(x = 800, y = 300)
193+
194+
# save button created
195+
saveb = Button(window, text="SAVE",command=save_img,font=("Arial", 25), bg = "orange", fg = "blue", borderwidth=3, relief="raised")
196+
saveb.place(x =80 , y =20 )
197+
198+
# choose button created
199+
chooseb = Button(window, text="SELECT",command=open_img,font=("Arial", 25), bg = "light green", fg = "blue", borderwidth=3, relief="raised")
200+
chooseb.place(x =170 , y =650 )
201+
202+
# crop button created
203+
enb = Button(window, text="CIRCULAR CROP",command=crop_fun,font=("Arial", 25), bg = "orange", fg = "blue", borderwidth=3, relief="raised")
204+
enb.place(x =530 , y =650 )
205+
206+
# reset button created
207+
resetb = Button(window, text="RESET",command=reset,font=("Arial", 25), bg = "yellow", fg = "blue", borderwidth=3, relief="raised")
208+
resetb.place(x =1000 , y =650 )
209+
210+
# function created for exiting
211+
def exit_win():
212+
if mbox.askokcancel("Exit", "Do you want to exit?"):
213+
window.destroy()
214+
215+
# exit button created
216+
exitb = Button(window, text="EXIT",command=exit_win,font=("Arial", 25), bg = "red", fg = "blue", borderwidth=3, relief="raised")
217+
exitb.place(x =1100 , y =20 )
218+
219+
220+
window.protocol("WM_DELETE_WINDOW", exit_win)
221+
window.mainloop()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
libraries used : tkinter
2+
from tkinter import filedialog
3+
import tkinter.messagebox
4+
from PIL import ImageTk, Image, ImageDraw
5+
os
6+
cv2
7+
numpy

0 commit comments

Comments
 (0)