pytorch图像缩放
时间: 2024-05-26 10:09:20 浏览: 119
PyTorch中的图像缩放通常通过torchvision.transforms中的Resize方法实现。该方法可以将输入的图像缩放到指定的大小或按比例缩放。
以下是一个示例代码,将图像缩放到指定大小:
```python
from torchvision.transforms import Resize
import torchvision.transforms.functional as TF
from PIL import Image
# 打开图像
img = Image.open('example.jpg')
# 定义缩放的大小
resize = Resize((256, 256))
# 进行缩放
resized_img = resize(img)
# 显示缩放后的图像
resized_img.show()
```
除了指定大小外,还可以通过传递一个scale参数来按比例缩放图像。以下是一个示例代码:
```python
from torchvision.transforms import Resize
import torchvision.transforms.functional as TF
from PIL import Image
# 打开图像
img = Image.open('example.jpg')
# 定义缩放比例
scale = 0.5
# 进行缩放
resize = Resize(int(scale * img.size))
resized_img = resize(img)
# 显示缩放后的图像
resized_img.show()
```
阅读全文
相关推荐


















