-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment.py
More file actions
252 lines (209 loc) · 7.96 KB
/
Assignment.py
File metadata and controls
252 lines (209 loc) · 7.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
"""
"Instructions"
-> Ask user for file path of image or upload
-> Give Options :
1. Image Manipulation & Transformation
2. Basic Drawing Techniques
-> If 1:
What to perform on Image?
1. Resizing & Scaling (user input -> with selectable sections as center , left , top left , bottom left , right , top right , bottom right , custom)
2. Cropping (user input -> -> with selectable sections as center , left , top left , bottom left , right , top right , bottom right , custom)
3. Rotating (user input with little info what degree will do what)
4. Flipping ( user input -> vertical , horizontal , or both)
After that to the chosen and show the original and operated image
Ask user if he want to download or not and do as it.
If 2:
What to perfrom on Image ?
1. Line Draw (user input -> -> with selectable sections as center , left , top left , bottom left , right , top right , bottom right , custom)
1. Rectangle Draw (user input -> -> with selectable sections as center , left , top left , bottom left , right , top right , bottom right , custom)
1. Circle Draw (user input -> -> with selectable sections as center , left , top left , bottom left , right , top right , bottom right , custom)
1. Text Add (text and position : user input -> -> with selectable sections as center , left , top left , bottom left , right , top right , bottom right , custom)
After that to the chosen and show the original and operated image
Ask user if he want to download or not and do as it.
"""
import cv2
import numpy as np
import os
from tkinter import filedialog, Tk
def load_image():
root = Tk()
root.withdraw()
file_path = filedialog.askopenfilename(
title="Select an image", filetypes=[("Image files", "*.jpg *.png *.jpeg *.bmp")]
)
if not file_path:
print("No file selected.")
return None
img = cv2.imread(file_path)
return img, file_path
def display_images(original, modified, window_title="Result"):
combined = np.hstack((original, modified))
cv2.imshow(window_title, combined)
cv2.waitKey(0)
cv2.destroyAllWindows()
def save_image(img, filename="output.jpg"):
cv2.imwrite(filename, img)
print(f"Image saved as {filename}")
def select_position(section, img_shape, size=(100, 100)):
h, w = img_shape[:2]
sw, sh = size
positions = {
"center": ((w - sw) // 2, (h - sh) // 2),
"left": (0, (h - sh) // 2),
"right": (w - sw, (h - sh) // 2),
"top left": (0, 0),
"top right": (w - sw, 0),
"bottom left": (0, h - sh),
"bottom right": (w - sw, h - sh),
}
if section in positions:
return positions[section]
else:
x = int(input("Enter custom x: "))
y = int(input("Enter custom y: "))
return (x, y)
def resize_image(img):
scale = float(input("Enter scale factor (e.g. 0.5 for half size): "))
h, w = img.shape[:2]
new_w, new_h = int(w * scale), int(h * scale)
resized = cv2.resize(img, (new_w, new_h))
return resized
def crop_image(img):
section = (
input(
"Select section to crop (center, left, top left, bottom left, right, top right, bottom right, custom): "
)
.strip()
.lower()
)
crop_w = int(input("Enter crop width: "))
crop_h = int(input("Enter crop height: "))
x, y = select_position(section, img.shape, size=(crop_w, crop_h))
cropped = img[y : y + crop_h, x : x + crop_w]
return cropped
def rotate_image(img):
angle = float(input("Enter rotation angle (positive=CCW, negative=CW): "))
h, w = img.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, angle, 1.0)
rotated = cv2.warpAffine(img, M, (w, h))
return rotated
def flip_image(img):
direction = (
input("Enter flip direction (vertical, horizontal, both): ").strip().lower()
)
if direction == "vertical":
return cv2.flip(img, 0)
elif direction == "horizontal":
return cv2.flip(img, 1)
elif direction == "both":
return cv2.flip(img, -1)
else:
print("Invalid flip direction.")
return img
def draw_line(img):
section = (
input(
"Select section to draw line (center, left, top left, bottom left, right, top right, bottom right, custom): "
)
.strip()
.lower()
)
x, y = select_position(section, img.shape)
return cv2.line(img.copy(), (x, y), (x + 100, y), (255, 255, 255), 5)
def draw_rectangle(img):
section = (
input(
"Select section to draw rectangle (center, left, top left, bottom left, right, top right, bottom right, custom): "
)
.strip()
.lower()
)
rect_w = 100
rect_h = 100
x, y = select_position(section, img.shape, size=(rect_w, rect_h))
return cv2.rectangle(img.copy(), (x, y), (x + rect_w, y + rect_h), (255, 0, 0), 5)
def draw_circle(img):
section = (
input(
"Select section to draw circle (center, left, top left, bottom left, right, top right, bottom right, custom): "
)
.strip()
.lower()
)
radius = 50
x, y = select_position(section, img.shape, size=(radius * 2, radius * 2))
center = (x + radius, y + radius)
return cv2.circle(img.copy(), center, radius, (0, 255, 0), 5)
def add_text(img):
text = input("Enter the text to add: ")
section = (
input(
"Select section to add text (center, left, top left, bottom left, right, top right, bottom right, custom): "
)
.strip()
.lower()
)
font = cv2.FONT_HERSHEY_SIMPLEX
font_scale = 2
thickness = 3
(text_w, text_h), baseline = cv2.getTextSize(text, font, font_scale, thickness)
x, y = select_position(section, img.shape, size=(text_w, text_h))
y += text_h
return cv2.putText(
img.copy(), text, (x, y), font, font_scale, (0, 255, 255), thickness
)
def main():
img, path = load_image()
if img is None:
return
print("\nChoose an option:")
print("1. Image Manipulation & Transformation")
print("2. Basic Drawing Techniques")
choice = input("Enter your choice (1/2): ").strip()
result = img.copy()
if choice == "1":
print("\nImage Manipulation Options:")
print("1. Resizing & Scaling")
print("2. Cropping")
print("3. Rotating")
print("4. Flipping")
sub_choice = input("Enter your choice (1-4): ").strip()
if sub_choice == "1":
result = resize_image(img)
elif sub_choice == "2":
result = crop_image(img)
elif sub_choice == "3":
result = rotate_image(img)
elif sub_choice == "4":
result = flip_image(img)
else:
print("Invalid choice.")
return
elif choice == "2":
print("\nDrawing Options:")
print("1. Line Draw")
print("2. Rectangle Draw")
print("3. Circle Draw")
print("4. Text Add")
sub_choice = input("Enter your choice (1-4): ").strip()
if sub_choice == "1":
result = draw_line(img)
elif sub_choice == "2":
result = draw_rectangle(img)
elif sub_choice == "3":
result = draw_circle(img)
elif sub_choice == "4":
result = add_text(img)
else:
print("Invalid choice.")
return
else:
print("Invalid choice.")
return
display_images(img, result)
download = input("Do you want to download the result? (y/n): ").strip().lower()
if download == "y":
filename = input("Enter filename to save (e.g., result.jpg): ").strip()
save_image(result, filename)
main()