Python crop image
In this post, you will learn how to crop an image using the Python programming language.
Image processing is the technique of performing some operations on an image. It is a type of signal processing in which the input is an image and the result may be an image or qualities or features related to that image. It is used to find various patterns and aspects in images.
Image cropping is a method of photo editing that includes removing a portion of an image to highlight some portions, change the aspect ratio, or enhance the framing.
Python crop image using Pillow
The Pillow and PIL (Python Imaging Library) are the same. The PIL was used with Python2. Pillow is a fork of the PIL repository which is currently compatible with Python3. This library is mostly used for image opening, processing, and transforming. It is available for Windows, Mac OS X and Linux. With the use of this library, we can also perform several image operations, like resizing, cropping, file conversion, rotating, and much more.
Pillow Installation
To install Pillow on Windows, open a terminal window and run the following command.
pip install pillow
This library is pre-installed on my working system, so it returns the following-
Requirement already satisfied: pillow in c:\python37\scripts\projects\env\lib\site-packages (7.0.0)
Check Installation of Python Pillow
We can check the successful installation of Pillow by printing the installed version.
import PIL
print('Pillow Version:', PIL.__version__)
Python Pillow: Cropping Image
The image.crop() is used to crop the image. It crops a region from an image which is defined by a 4 tuples (left, upper, right, lower).
Syntax-Image.crop(box=None)
The box parameter accepts a tuple with four values of the four coordinates for the crop rectangle: left, upper, right, and lower. It returns a rectangular region as (left, upper, right, lower) tuple.
Python Crop Image using Pillow
from PIL import Image
# Open Image
img = Image.open('mountain.jpg')
# Size of the image in pixels
width, height = img.size
# Setting the points for cropped image
left = 1
top = 2
right = 250
bottom = 200
# Cropped image of above dimension
img_cropped = img.crop((left, top, right, bottom))
img_cropped.show()
Original Image -
Output of the above code-
Python crop image using OpenCV
Python OpenCV has several filtering techniques to perform smoothing operations on images. These smoothing techniques are generally used to reduce noise, reduce detail, and so on.
To crop an image using the Python openCV, first we need to import it with the import statement. Then, we use the cv2.imread() method to read the image. After reading the image, we define the dimensions to crop the image.
import cv2
img = cv2.imread("hillimg.jpg")
x = 50
y = 50
h = 250
w= 250
cropped_image = img[y:y+h,x:x+w]
cv2.imshow("Cropped Image",cropped_image)
cv2.imshow("OriginalImage",img)
cv2.imwrite("Cropped Image.jpg", cropped_image)
cv2.waitKey(0)
Original Image -
Output of the above code-
Related Articles
Geometric Transformation OpenCV PythonPython OpenCV Overlaying or Blending Two Images
Detect Specific Color From Image using Python OpenCV
Python OpenCV Histogram of Grayscale Image
Eye Detection Program in Python OpenCV
Python OpenCV Image Filtering
Python OpenCV Histogram of Color Image
Image Thresholding Python OpenCV
Harris Corner Detection using Python OpenCV
NumPy program to copy data from a given array to another array
Multiply all elements in list Python
Remove element from list Python
Inverse of a matrix in Python
Python Contour Plot Examples
Python iterate list with index
Python add list to list
Prettytable in Python
Python dict inside list
Convert array to list Python
Python Matplotlib Bar Plot