python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Python提升图片清晰度

Python中提升图片清晰度的四种方法

作者:一晌小贪欢

这篇文章主要为大家详细介绍了Python中提升图片清晰度的四种常用方法,文中的示例代码简洁易懂,感兴趣的小伙伴可以跟随小编一起学习一下

1、库的安装

用途安装
pillow图片相关pip install pillow -i https://pypi.tuna.tsinghua.edu.cn/simple/
cv2视图相关pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple/
torch视图相关ppip install torch -i https://pypi.tuna.tsinghua.edu.cn/simple/
torchvision视图相关ppip install torchvision -i https://pypi.tuna.tsinghua.edu.cn/simple/

2、实现方法

方法1—PIL

用途安装
pillow图片相关pip install pillow -i https://pypi.tuna.tsinghua.edu.cn/simple/

示例代码

from PIL import Image, ImageFilter

# 打开图片
image = Image.open('input_image.jpg')

# 应用锐化滤镜
sharpened_image = image.filter(ImageFilter.SHARPEN)

# 保存结果
sharpened_image.save('sharpened_image.jpg')

方法2—cv2

用途安装
cv2视图相关pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple/

示例代码

import cv2
import numpy as np

# 读取图片
image = cv2.imread('input_image.jpg')

# 高斯模糊
blurred = cv2.GaussianBlur(image, (0, 0), 3)

# 锐化
sharpened = cv2.addWeighted(image, 1.5, blurred, -0.5, 0)

# 保存结果
cv2.imwrite('sharpened_image.jpg', sharpened)

方法3—torch

用途安装
cv2视图相关pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple/
torch视图相关ppip install torch -i https://pypi.tuna.tsinghua.edu.cn/simple/
torchvision视图相关ppip install torchvision -i https://pypi.tuna.tsinghua.edu.cn/simple/

示例代码

import cv2
import torch
from torchvision.transforms import ToTensor, ToPILImage

# 加载预训练的ESRGAN模型
model = torch.hub.load('xinntao/ESRGAN', 'esrgan', pretrained=True)
model.eval()

# 读取图片
image = cv2.imread('input_image.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = ToTensor()(image).unsqueeze(0)

# 使用模型进行超分辨率
with torch.no_grad():
    output = model(image)

# 保存结果
output_image = ToPILImage()(output.squeeze(0))
output_image.save('super_resolution_image.jpg')

方法4—waifu2x

用途安装
waifu2x视图相关ppip install waifu2x -i https://pypi.tuna.tsinghua.edu.cn/simple/

示例代码

from waifu2x import Waifu2x

# 创建waifu2x对象
waifu2x = Waifu2x()

# 提升图片清晰度
waifu2x.upscale_image('input_image.jpg', 'output_image.jpg')

到此这篇关于Python中提升图片清晰度的四种方法的文章就介绍到这了,更多相关Python提升图片清晰度内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文