📜  python librairie images (1)

📅  最后修改于: 2023-12-03 14:45:59.892000             🧑  作者: Mango

Python Libraries for Image Processing

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

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.

Installing Pillow
pip install Pillow
Basic Usage
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

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.

Installing OpenCV
pip install opencv-python
Basic Usage
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

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.

Installing Scikit-Image
pip install scikit-image
Basic Usage
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()
Conclusion

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!