欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python中提升圖片清晰度的四種方法

 更新時(shí)間:2025年02月28日 09:56:48   作者:一晌小貪歡  
這篇文章主要為大家詳細(xì)介紹了Python中提升圖片清晰度的四種常用方法,文中的示例代碼簡(jiǎn)潔易懂,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

1、庫(kù)的安裝

庫(kù)用途安裝
pillow圖片相關(guān)pip install pillow -i https://pypi.tuna.tsinghua.edu.cn/simple/
cv2視圖相關(guān)pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple/
torch視圖相關(guān)ppip install torch -i https://pypi.tuna.tsinghua.edu.cn/simple/
torchvision視圖相關(guān)ppip install torchvision -i https://pypi.tuna.tsinghua.edu.cn/simple/

2、實(shí)現(xiàn)方法

方法1—PIL

庫(kù)用途安裝
pillow圖片相關(guān)pip install pillow -i https://pypi.tuna.tsinghua.edu.cn/simple/

示例代碼

from PIL import Image, ImageFilter

# 打開圖片
image = Image.open('input_image.jpg')

# 應(yīng)用銳化濾鏡
sharpened_image = image.filter(ImageFilter.SHARPEN)

# 保存結(jié)果
sharpened_image.save('sharpened_image.jpg')

方法2—cv2

庫(kù)用途安裝
cv2視圖相關(guān)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)

# 保存結(jié)果
cv2.imwrite('sharpened_image.jpg', sharpened)

方法3—torch

庫(kù)用途安裝
cv2視圖相關(guān)pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple/
torch視圖相關(guān)ppip install torch -i https://pypi.tuna.tsinghua.edu.cn/simple/
torchvision視圖相關(guān)ppip install torchvision -i https://pypi.tuna.tsinghua.edu.cn/simple/

示例代碼

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

# 加載預(yù)訓(xùn)練的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)

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

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

方法4—waifu2x

庫(kù)用途安裝
waifu2x視圖相關(guān)ppip install waifu2x -i https://pypi.tuna.tsinghua.edu.cn/simple/

示例代碼

from waifu2x import Waifu2x

# 創(chuàng)建waifu2x對(duì)象
waifu2x = Waifu2x()

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

到此這篇關(guān)于Python中提升圖片清晰度的四種方法的文章就介紹到這了,更多相關(guān)Python提升圖片清晰度內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論