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

 更新时间:2025年02月28日 09:56:48   作者:一晌小贪欢  
这篇文章主要为大家详细介绍了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提升图片清晰度内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Python编程itertools模块处理可迭代集合相关函数

    Python编程itertools模块处理可迭代集合相关函数

    本篇博客将为你介绍Python函数式编程itertools模块中处理可迭代集合的相关函数,有需要的朋友可以借鉴参考下,希望可以有所帮助
    2021-09-09
  • Python Matplotlib绘制箱线图的全过程

    Python Matplotlib绘制箱线图的全过程

    又称箱形图(boxplot)或盒式图,数据大小、占比、趋势等等的呈现其包含一些统计学的均值、分位数、极值等等统计量,因此该图信息量较大,下面这篇文章主要给大家介绍了关于Python Matplotlib绘制箱线图的相关资料,需要的朋友可以参考下
    2021-09-09
  • Python数组变形的几种实现方法

    Python数组变形的几种实现方法

    本文主要介绍了Python数组变形的几种实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-05-05
  • PyTorch搭建LSTM实现多变量多步长时序负荷预测

    PyTorch搭建LSTM实现多变量多步长时序负荷预测

    这篇文章主要为大家介绍了PyTorch搭建LSTM实现多变量多步长时序负荷预测,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-05-05
  • Python中yield关键字的理解与使用

    Python中yield关键字的理解与使用

    yield关键字用于创建生成器函数,一种高效利用内存的函数类型,可以像迭代器对象一样使用,本文主要介绍了Python中的yield关键字的应用,需要的可以参考下
    2023-08-08
  • python中的np.round()函数示例详解

    python中的np.round()函数示例详解

    np.round()是NumPy库中的一个函数,用于对数组或单个数值进行四舍五入,该函数返回一个与输入类型相同的数组或数值,并可以通过可选的参数来指定保留的小数位数,这篇文章主要介绍了python中的np.round()函数,需要的朋友可以参考下
    2024-06-06
  • python中redis查看剩余过期时间及用正则通配符批量删除key的方法

    python中redis查看剩余过期时间及用正则通配符批量删除key的方法

    这篇文章主要介绍了python中redis查看剩余过期时间及用正则通配符批量删除key的方法,需要的朋友可以参考下
    2018-07-07
  • 使用Python Fast API发布API服务的过程详解

    使用Python Fast API发布API服务的过程详解

    这篇文章主要介绍了使用Python Fast API发布API服务,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-04-04
  • python中dict获取关键字与值的实现

    python中dict获取关键字与值的实现

    这篇文章主要介绍了python中dict获取关键字与值的实现方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-05-05
  • Django admin显示json字段方法详解

    Django admin显示json字段方法详解

    这篇文章主要为大家介绍了Django admin显示json字段方法详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-06-06

最新评论