【PIL与Tensor】Pytorch中Tensor与PIL图像格式相互转化

1 篇文章 0 订阅
1 篇文章 0 订阅

PIL图像转换为Tensor

在Pytorch中,PIL图像可以通过以下方式转换为Tensor:

import torch
from PIL import Image

image = Image.open('your_image.png')
tensor_img = torch.from_numpy(np.array(image)).permute(2, 0, 1).float()/255.0
print(tensor_img)
print(tensor_img.shape)

其中,np.array()将PIL Image转换为numpy数组,.permute()调整了数组的维度以适应Pytorch Tensor的格式,并将数据类型转换为float类型。

tensor([[[0.5137, 0.5137, 0.5216,  ..., 1.0000, 1.0000, 1.0000],
         [0.5098, 0.5137, 0.5176,  ..., 1.0000, 1.0000, 1.0000],
         [0.5020, 0.5020, 0.5098,  ..., 1.0000, 1.0000, 1.0000],
         ...,
         [0.2314, 0.2314, 0.2431,  ..., 0.2157, 0.2157, 0.2392],
         [0.2314, 0.2275, 0.2353,  ..., 0.2157, 0.2196, 0.2510],
         [0.2627, 0.2588, 0.2510,  ..., 0.2157, 0.2118, 0.2392]],

        [[0.4824, 0.4824, 0.4902,  ..., 1.0000, 1.0000, 1.0000],
         [0.4784, 0.4824, 0.4863,  ..., 1.0000, 1.0000, 1.0000],
         [0.4706, 0.4706, 0.4784,  ..., 1.0000, 1.0000, 1.0000],
         ...,
         [0.3137, 0.3059, 0.3176,  ..., 0.3059, 0.3059, 0.3294],
         [0.3137, 0.3020, 0.3098,  ..., 0.3059, 0.3098, 0.3412],
         [0.3373, 0.3412, 0.3333,  ..., 0.3059, 0.3020, 0.3294]],

        [[0.4706, 0.4706, 0.4784,  ..., 1.0000, 1.0000, 1.0000],
         [0.4667, 0.4706, 0.4745,  ..., 1.0000, 1.0000, 1.0000],
         [0.4588, 0.4588, 0.4667,  ..., 1.0000, 1.0000, 1.0000],
         ...,
         [0.4196, 0.4235, 0.4353,  ..., 0.4275, 0.4275, 0.4510],
         [0.4196, 0.4196, 0.4275,  ..., 0.4275, 0.4314, 0.4627],
         [0.4549, 0.4471, 0.4392,  ..., 0.4275, 0.4235, 0.4510]]])

Tensor转换为PIL图像

将Tensor转换为PIL图像可以使用以下代码:

import numpy as np
from PIL import Image

tensor = tensor_img
tensor = tensor.cpu().clone()
tensor = tensor.squeeze(0)
tensor = tensor.permute(1, 2, 0)
image = tensor.numpy()
image = (image * 255).astype(np.uint8)
image = Image.fromarray(image)

首先,将Tensor复制到CPU并调整维度。然后使用.numpy()函数将Tensor转换为numpy数组,并乘以255以还原为原始图像数据类型。最后使用Image.fromarray()将numpy数组转换为PIL Image。

输出为:

(4000, 2250, 3)
[[[131 123 120]
  [131 123 120]
  [133 125 122]
  ...
  [255 255 255]
  [255 255 255]
  [255 255 255]]

 [[130 122 119]
  [131 123 120]
  [132 124 121]
  ...
  [255 255 255]
  [255 255 255]
  [255 255 255]]

 [[128 120 117]
  [128 120 117]
  [130 122 119]
  ...
  [255 255 255]
  [255 255 255]
  [255 255 255]]
...
  ...
  [ 55  78 109]
  [ 54  77 108]
  [ 61  84 115]]]

验证 PIL 再次转为 tensor

import torch
from PIL import Image

tensor_img = torch.from_numpy(np.array(image)).permute(2, 0, 1).float()/255.0
print(tensor_img)
print(tensor_img.shape)

输出为

tensor([[[0.5137, 0.5137, 0.5216,  ..., 1.0000, 1.0000, 1.0000],
         [0.5098, 0.5137, 0.5176,  ..., 1.0000, 1.0000, 1.0000],
         [0.5020, 0.5020, 0.5098,  ..., 1.0000, 1.0000, 1.0000],
         ...,
         [0.2314, 0.2314, 0.2431,  ..., 0.2157, 0.2157, 0.2392],
         [0.2314, 0.2275, 0.2353,  ..., 0.2157, 0.2196, 0.2510],
         [0.2627, 0.2588, 0.2510,  ..., 0.2157, 0.2118, 0.2392]],

        [[0.4824, 0.4824, 0.4902,  ..., 1.0000, 1.0000, 1.0000],
         [0.4784, 0.4824, 0.4863,  ..., 1.0000, 1.0000, 1.0000],
         [0.4706, 0.4706, 0.4784,  ..., 1.0000, 1.0000, 1.0000],
         ...,
         [0.3137, 0.3059, 0.3176,  ..., 0.3059, 0.3059, 0.3294],
         [0.3137, 0.3020, 0.3098,  ..., 0.3059, 0.3098, 0.3412],
         [0.3373, 0.3412, 0.3333,  ..., 0.3059, 0.3020, 0.3294]],

        [[0.4706, 0.4706, 0.4784,  ..., 1.0000, 1.0000, 1.0000],
         [0.4667, 0.4706, 0.4745,  ..., 1.0000, 1.0000, 1.0000],
         [0.4588, 0.4588, 0.4667,  ..., 1.0000, 1.0000, 1.0000],
         ...,
         [0.4196, 0.4235, 0.4353,  ..., 0.4275, 0.4275, 0.4510],
         [0.4196, 0.4196, 0.4275,  ..., 0.4275, 0.4314, 0.4627],
         [0.4549, 0.4471, 0.4392,  ..., 0.4275, 0.4235, 0.4510]]])

可以看到当前转化是正确的!

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值