📅  最后修改于: 2023-12-03 14:45:59.892000             🧑  作者: Mango
Image processing is an important aspect of computer science and is widely used in various fields. Python, being a popular language among programmers, offers various libraries and packages for image processing. In this article, we will discuss some of the most commonly used libraries for image processing in Python.
Pillow is a fork of the Python Imaging Library (PIL) and is a popular library for handling images in Python. It supports a wide range of image file formats and provides various functions for image processing such as cropping, resizing, filtering, and color manipulation.
pip install Pillow
from PIL import Image
im = Image.open('image.jpg')
im.show()
# Resize image
new_im = im.resize((500, 500))
new_im.show()
# Convert to grayscale
gray_im = im.convert('L')
gray_im.show()
# Crop image
box = (100, 100, 400, 400)
cropped_im = im.crop(box)
cropped_im.show()
OpenCV (Open Source Computer Vision) is a popular computer vision library that also provides functions for image processing. It is written in C++ but has Python bindings, making it easy for Python programmers to use.
pip install opencv-python
import cv2
# Read image
img = cv2.imread('image.jpg')
# Show image
cv2.imshow('Image', img)
cv2.waitKey()
# Resize image
resized_img = cv2.resize(img, (500, 500))
cv2.imshow('Resized Image', resized_img)
cv2.waitKey()
# Convert to grayscale
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('Grayscale Image', gray_img)
cv2.waitKey()
# Crop image
cropped_img = img[100:400, 100:400]
cv2.imshow('Cropped Image', cropped_img)
cv2.waitKey()
Scikit-Image is a Python package for image processing that is built on top of NumPy and SciPy. It provides a wide range of functions for image processing such as feature detection, segmentation, and filtering.
pip install scikit-image
from skimage import io, filters
# Read image
img = io.imread('image.jpg')
# Show image
io.imshow(img)
io.show()
# Resize image
resized_img = io.resize(img, (500, 500))
io.imshow(resized_img)
io.show()
# Convert to grayscale
gray_img = io.imread('image.jpg', as_gray=True)
io.imshow(gray_img)
io.show()
# Crop image
cropped_img = img[100:400, 100:400]
io.imshow(cropped_img)
io.show()
In this article, we discussed some of the most commonly used libraries and packages for image processing in Python. These libraries provide various functions for image processing such as resizing, cropping, filtering, and color manipulation. Choose the library that suits your needs and start working with images in Python!