Skip to content

Commit ce5b32e

Browse files
authored
Merge pull request #812 from aigerakaratay/main
image_cropper added
2 parents b958d67 + f013ccf commit ce5b32e

File tree

5 files changed

+47
-0
lines changed

5 files changed

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