Skip to content

Commit 66ac52d

Browse files
committed
image_cropper added
1 parent c9db9e5 commit 66ac52d

File tree

5 files changed

+45
-0
lines changed

5 files changed

+45
-0
lines changed

image_cropper/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# IMAGE CROPPER
2+
This script helps to crop an image by specifying cropping points
3+
4+
## INPUT
5+
It takes an image and cropping points as input
6+
7+
## OUTPUT
8+
It saves the cropped image to the specified location and shows the image in the image viewer as output
9+
10+
## AUTHOR
11+
Aigerim Karatay

image_cropper/cropped_img.jpg

2.7 KB
Loading

image_cropper/demo.jpg

13.4 KB
Loading

image_cropper/image_cropper.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Importing Image class from PIL module
2+
from PIL import Image
3+
4+
def crop():
5+
# Taking the image path as an input
6+
file_path = str(input('Enter the image path (absolute path): '))
7+
8+
# Opening image with an RGB mode, works only with jpeg/jpg formats
9+
img = Image.open(file_path, mode="r")
10+
11+
# Calculating size of the original image in pixels for reference
12+
width, height = img.size
13+
print('the old width is: ' + str(width) + ' and the old height is: '+ str(height))
14+
15+
# Taking left, top, right, bottom points for the cropped image as input
16+
# Image pixels coorginate grid: X increases from left to right, Y increases from top to bottom
17+
left = int(input('Enter the new left point: '))
18+
top = int(input('Enter the new top point: '))
19+
right = int(input('Enter the new right point: '))
20+
bottom = int(input('Enter the new bottom point: '))
21+
22+
# Crop the image with specified points
23+
cropped_img = img.crop((left, top, right, bottom))
24+
25+
# Save the cropped image as a separate image named "cropped_img.jpg" under a specified directory
26+
cropped_image_path = str(input('Enter the path to save the cropped image (absolute path): '))
27+
cropped_img.save(f"{cropped_image_path}/cropped_img.jpg")
28+
29+
# Show the image in the image viewer
30+
cropped_img.show()
31+
32+
if __name__ == '__main__':
33+
crop()

image_cropper/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Pillow==8.4.0

0 commit comments

Comments
 (0)