当前位置:网站首页>Image,cv2读取图片的numpy数组的转换和尺寸resize变化
Image,cv2读取图片的numpy数组的转换和尺寸resize变化
2022-07-06 08:27:00 【MAR-Sky】
几种图片尺寸修改和参数总结
(from torchvision import transforms as T)
显示尺寸格式的不同
- Image类型和T进行resize的图片的size属性显示参数顺序是W、H
- cv2的显示的shape的显示参数顺序是H、W、C
- T.ToTensor()(img),将图片转换为tensor类型,显示是size()方法,例如,
import numpy as np
from PIL import Image
from torchvision import transforms as T
# import matplotlib.image as Img
img = Image.open('4.jpg')
img_tran = T.Resize((512,256))(img)
print(img_tran.size) #(256, 512)
print(np.array(img_tran).shape) # (512, 256, 3)
img_to = T.ToTensor()(img_tran)
img_to_array = np.array(img_to)
print(img_to.size()) #torch.Size([3, 512, 256])
print(img_to_array.shape) # (3, 512, 256)
- 可以看出size()方法显示的是将通道放在第一位了。
修改图片尺寸的函数参数顺序
-T.Resize((H,W))(img)
- img.resize((W,H))、cv2.resize(img_read,(W,H))

Image图片数据和numpy数据的相互转换
Image图片转换为numpy数据
np.array(img)
from PIL import Image
import numpy as np
img = Image.open('4.jpg')
img_array = np.array(img)
numpy数据转换为Image图片
Image.fromarray(img_arr.astype(‘uint8’))
Image.fromarray(np.uint8(img))
from PIL import Image
import numpy as np
img = Image.open('4.jpg')
img_array = np.array(img)
img_image = Image.fromarray(img_arr.astype('uint8'))
Image尺寸显示和numpy的shape显示问题
Image.size属性显示的是**宽、高**
img_array.shape属性,显示的是**高、宽、通道**
from PIL import Image
from torchvision import transforms as T
import numpy as np
img = Image.open('4.jpg')
img_array = np.array(img)
print(img.size) # (720, 1160)
print(img_arr.shape) # (1160, 720, 3)
plt.figure()
plt.subplot(1,3,1)
plt.imshow(img)
plt.subplot(1,3,2)
plt.imshow(img_array)
plt.show()

img.resize(w, h),T.Resize((h,w))(img),cv2.resize(img,(w,h))
功能:将读取后的图片尺寸的宽和高修改
import cv2
import matplotlib.image as Img
from torchvision import transforms as T
img = Image.open('4.jpg')
img_resize = img.resize((512,256))
img_tran = T.Resize((512,256))(img)
img_array = np.array(img)
img_read = cv2.imread('4.jpg')
img_cv2 = cv2.resize(img_read,(512,256))
print(img_resize.size) #(512, 256)
print(img_array.shape) #(1160, 720, 3)
print(img_tran.size) # (256, 512)
print(img_cv2.shape) # (256, 512, 3)
plt.figure()
plt.subplot(1,3,1)
plt.title('img_resize')
plt.imshow(img_resize)
plt.subplot(1,3,2)
plt.title('img_tran')
plt.imshow(img_tran)
plt.subplot(1,3,3)
plt.title('img_cv2')
plt.imshow(img_cv2)
plt.show()
边栏推荐
- LDAP application (4) Jenkins access
- ESP系列引脚说明图汇总
- egg. JS getting started navigation: installation, use and learning
- Browser thread
- Hungry for 4 years + Ali for 2 years: some conclusions and Thoughts on the road of research and development
- sublime text没关闭其他运行就使用CTRL+b运行另外的程序问题
- Roguelike游戏成破解重灾区,如何破局?
- 2022 Inner Mongolia latest water conservancy and hydropower construction safety officer simulation examination questions and answers
- Report on Market Research and investment prospects of China's silver powder industry (2022 Edition)
- JVM performance tuning and practical basic theory - Part 1
猜你喜欢
![Verrouillage [MySQL]](/img/ce/9f8089da60d9b3a3f92a5e4eebfc13.png)
Verrouillage [MySQL]

Pyqt5 development tips - obtain Manhattan distance between coordinates

【MySQL】数据库的存储过程与存储函数通关教程(完整版)

MySQL learning record 10getting started with JDBC

CISP-PTE实操练习讲解

Summary of phased use of sonic one-stop open source distributed cluster cloud real machine test platform
![[cloud native] teach you how to build ferry open source work order system](/img/fb/507f763791235bd00bc8201e5d7741.png)
[cloud native] teach you how to build ferry open source work order system

pcd转ply后在meshlab无法打开,提示 Error details: Unespected eof

vulnhub hackme: 1

JVM 快速入门
随机推荐
根据csv文件某一列字符串中某个数字排序
【Nvidia开发板】常见问题集 (不定时更新)
Unified ordering background interface product description Chinese garbled
2022 Inner Mongolia latest construction tower crane (construction special operation) simulation examination question bank and answers
China high purity silver nitrate Market Research and investment strategy report (2022 Edition)
Use br to back up tidb cluster data to S3 compatible storage
ESP系列引脚说明图汇总
JVM performance tuning and practical basic theory - Part 1
Convolution, pooling, activation function, initialization, normalization, regularization, learning rate - Summary of deep learning foundation
Research Report on supply and demand and development prospects of China's high purity aluminum market (2022 Edition)
游戏解包的危害及资源加密的重要性
sys.argv
Ruffian Heng embedded bimonthly, issue 49
Chrome浏览器的crash问题
CISP-PTE实操练习讲解
China vanadium battery Market Research and future prospects report (2022 Edition)
移位运算符
1. Color inversion, logarithmic transformation, gamma transformation source code - miniopencv from zero
Wincc7.5 download and installation tutorial (win10 system)
[research materials] 2021 live broadcast annual data report of e-commerce - Download attached