Python 处理图片常用的20个脚本,你知道几个?

开发 前端
处理图片是 Python 中常见的任务,尤其是在计算机视觉、图像处理和数据分析等领域。Python 提供了多个强大的库来处理图像,其中最常用的是 Pillow(PIL 的一个分支)和 OpenCV。

处理图片是 Python 中常见的任务,尤其是在计算机视觉、图像处理和数据分析等领域。Python 提供了多个强大的库来处理图像,其中最常用的是 Pillow(PIL 的一个分支)和 OpenCV。

使用Pillow库

读取和显示图像:
from PIL import Image
import matplotlib.pyplot as plt
img = Image.open('image.jpg')
plt.imshow(img)
plt.show()

保存图像:

from PIL import Image
img = Image.open('image.jpg')
img.save('output_image.png', 'PNG')

调整图像大小:

from PIL import Image
img = Image.open('image.jpg')
resized_img = img.resize((800, 600))
resized_img.save('resized_image.jpg')

裁剪图像:

from PIL import Image
img = Image.open('image.jpg')
cropped_img = img.crop((50, 50, 300, 300))  # (left, upper, right, lower)
cropped_img.save('cropped_image.jpg')

旋转图像:

from PIL import Image
img = Image.open('image.jpg')
rotated_img = img.rotate(90)
rotated_img.save('rotated_image.jpg')

转换为灰度图像:

from PIL import Image
img = Image.open('image.jpg').convert('L')
img.save('gray_image.jpg')

添加文字到图像:

from PIL import Image, ImageDraw, ImageFont
img = Image.open('image.jpg')
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("arial.ttf", 36)
draw.text((50, 50), "Hello, World!", fill="white", fnotallow=font)
img.save('text_image.jpg')

图像合成:

from PIL import Image
background = Image.open('background.jpg')
foreground = Image.open('foreground.png').convert('RGBA')
background.paste(foreground, (0, 0), foreground)
background.save('composite_image.jpg')

图像模糊:

from PIL import Image, ImageFilter
img = Image.open('image.jpg')
blurred_img = img.filter(ImageFilter.BLUR)
blurred_img.save('blurred_image.jpg')

图像锐化:

from PIL import Image, ImageFilter
img = Image.open('image.jpg')
sharpened_img = img.filter(ImageFilter.SHARPEN)
sharpened_img.save('sharpened_image.jpg')

图像增强:

from PIL import Image, ImageEnhance
img = Image.open('image.jpg')
enhancer = ImageEnhance.Contrast(img)
enhanced_img = enhancer.enhance(1.5)
enhanced_img.save('enhanced_image.jpg')

获取图像信息:

from PIL import Image
img = Image.open('image.jpg')
print(f"Format: {img.format}")
print(f"Size: {img.size}")
print(f"Mode: {img.mode}")

使用OpenCV库

读取和显示图像:

import cv2
import matplotlib.pyplot as plt
img = cv2.imread('image.jpg')
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img_rgb)
plt.show()

保存图像:

import cv2
img = cv2.imread('image.jpg')
cv2.imwrite('output_image.png', img)

调整图像大小:

import cv2
img = cv2.imread('image.jpg')
resized_img = cv2.resize(img, (800, 600))
cv2.imwrite('resized_image.jpg', resized_img)

裁剪图像:

import cv2
img = cv2.imread('image.jpg')
cropped_img = img[50:300, 50:300]
cv2.imwrite('cropped_image.jpg', cropped_img)

旋转图像:

import cv2
img = cv2.imread('image.jpg')
(h, w) = img.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, 90, 1.0)
rotated_img = cv2.warpAffine(img, M, (w, h))
cv2.imwrite('rotated_image.jpg', rotated_img)

转换为灰度图像:

import cv2
img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)
cv2.imwrite('gray_image.jpg', img)

边缘检测:

import cv2
img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)
edges = cv2.Canny(img, 100, 200)
cv2.imwrite('edges_image.jpg', edges)

图像阈值处理:

import cv2
img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)
_, thresh_img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
cv2.imwrite('threshold_image.jpg', thresh_img)
责任编辑:武晓燕 来源: 测试开发学习交流
相关推荐

2021-10-12 09:20:02

数据库SQL脚本

2019-10-28 09:53:42

Java开发结构

2020-02-23 23:29:07

Python编程开发

2021-05-06 15:15:13

Python工具代码

2024-03-01 13:48:00

Git配置系统

2023-10-30 18:00:00

Docker命令开源平台

2023-10-31 08:23:54

网络命令Linux

2023-04-11 08:49:42

排序函数SQL

2022-06-30 08:31:54

排序函数SQL

2018-06-21 15:17:15

机器学习

2019-03-05 11:22:17

操作系统调度算法

2023-05-30 14:54:17

Python循环语句工具

2023-11-23 10:21:37

2023-12-15 10:42:05

2022-01-07 08:23:38

k8s AnnotationNginx

2010-10-13 09:45:50

Linux监控脚本

2018-06-06 09:10:34

编程语言Python库

2020-10-29 10:59:44

Python开发数据

2024-08-06 11:40:57

2024-03-27 14:35:09

自动验证工具
点赞
收藏

51CTO技术栈公众号