当前位置:网站首页>How-PIL-to-Tensor
How-PIL-to-Tensor
2022-07-06 19:22:00 【红烧code】
图片是如何从 N u m P y NumPy NumPy形式向 T e n s o r Tensor Tensor形式转换的呢?
from PIL import Image
import numpy as np
from torchvision.transforms import ToTensor
img=Image.open('./sword.png')
img_arr=np.array(img)
img_tensor=ToTensor().__call__(img)
N u m P y NumPy NumPy的图片格式 H , W , C H,W,C H,W,C,即 h e i g h t , w i d t h , c h a n n e l s height,width,channels height,width,channels(高、宽、通道数)
img_arr.shape
(73, 183, 4)
T e n s o r Tensor Tensor图片格式为 C , H , W C,H,W C,H,W,即 c h a n n e l s , h e i g h t , w i d t h channels,height,width channels,height,width(通道数、高、宽)
img_tensor.shape
torch.Size([4, 73, 183])
实际上将 N u m P y NumPy NumPy数组每个位置上对应的像素值除以 255 255 255就得到了对应的 T e n s o r Tensor Tensor数据类型
# 取出图像左上角第一个点的RGBA值
print(img_arr[0, 0, :])
print(img_tensor[:, 0, 0])
[ 35 33 47 255]
tensor([0.1373, 0.1294, 0.1843, 1.0000])
# 验证, 完全正确
print(255*img_tensor[:, 0, 0])
print(img_arr[0, 0, :])
tensor([ 35., 33., 47., 255.])
[ 35 33 47 255]
边栏推荐
- 数论 --- 快速幂、快速幂求逆元
- Redis introduction complete tutorial: client case analysis
- 写作系列之contribution
- MySQL
- Qt蓝牙:QBluetoothDeviceInfo
- Classify the features of pictures with full connection +softmax
- Number theory --- fast power, fast power inverse element
- Code line breaking problem of untiy text box
- PCL 常用拟合模型及使用方法
- Redis入门完整教程:RDB持久化
猜你喜欢
随机推荐
基于ensp防火墙双击热备二层网络规划与设计
【软件测试】最全面试问题和回答,全文背熟不拿下offer算我输
普通测试年薪15w,测试开发年薪30w+,二者差距在哪?
Redis入门完整教程:客户端案例分析
CDB PDB user rights management
Unity使用MaskableGraphic画一条带箭头的线
MySQL
Redis getting started complete tutorial: replication topology
Google Earth Engine(GEE)——Landsat 全球土地调查 1975年数据集
HAVE FUN | “飞船计划”活动最新进展
6-6漏洞利用-SSH安全防御
How to write test cases for test coupons?
PSINS中19维组合导航模块sinsgps详解(时间同步部分)
Metaforce force meta universe fossage 2.0 smart contract system development (source code deployment)
Redis入門完整教程:問題定比特與優化
牛客编程题--必刷101之双指针篇
MySQL
Electrical engineering and automation
记一次JAP查询导致OOM的问题分析
Google Earth engine (GEE) -- 1975 dataset of Landsat global land survey