Skip to content

Instantly share code, notes, and snippets.

@gsoykan
Created November 3, 2022 10:01
Show Gist options
  • Save gsoykan/f48086016e803790bd3489a00922382b to your computer and use it in GitHub Desktop.
Save gsoykan/f48086016e803790bd3489a00922382b to your computer and use it in GitHub Desktop.
crops an image and saves
# 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