Created
November 3, 2022 10:01
-
-
Save gsoykan/f48086016e803790bd3489a00922382b to your computer and use it in GitHub Desktop.
crops an image and saves
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Importing Image class from PIL module | |
from PIL import Image | |
# Opens a image in RGB mode | |
im = Image.open(r"C:\Users\Admin\Pictures\network.png") | |
# Setting the points for cropped image | |
left = 155 | |
top = 65 | |
right = 360 | |
bottom = 270 | |
# Cropped image of above dimension | |
# (It will not change original image) | |
im1 = im.crop((left, top, right, bottom)) | |
# Shows the image in image viewer | |
im1.show() | |
try: | |
im1 = im1.save("cropped.jpg") | |
except: | |
# sometimes it might throw cannot save RGBA image as jpeg... | |
im1 = im1.convert('RGB') | |
im1 = im1.save("cropped.jpg") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment