当前位置:网站首页>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()
边栏推荐
- [2022 Guangdong saim] Lagrange interpolation (multivariate function extreme value divide and conquer NTT)
- 2022.02.13 - NC003. Design LRU cache structure
- 移位运算符
- 灰度升级 TiDB Operator
- hcip--mpls
- VMware virtualization cluster
- 1. Color inversion, logarithmic transformation, gamma transformation source code - miniopencv from zero
- JVM performance tuning and practical basic theory - Part 1
- Hcip day 16
- Colorlog结合logging打印有颜色的日志
猜你喜欢

Precise query of tree tree

MySQL learning record 10getting started with JDBC
![[research materials] 2022 China yuancosmos white paper - Download attached](/img/b4/422dff0510bbe67f3578202d6e80b7.jpg)
[research materials] 2022 China yuancosmos white paper - Download attached

Wincc7.5 download and installation tutorial (win10 system)

ROS编译 调用第三方动态库(xxx.so)

Fibonacci sequence

2022.02.13 - NC001. Reverse linked list
![Verrouillage [MySQL]](/img/ce/9f8089da60d9b3a3f92a5e4eebfc13.png)
Verrouillage [MySQL]

Ruffian Heng embedded bimonthly, issue 49

Pyqt5 development tips - obtain Manhattan distance between coordinates
随机推荐
Use Alibaba icon in uniapp
Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation
Roguelike游戏成破解重灾区,如何破局?
Remote storage access authorization
JVM performance tuning and practical basic theory - Part 1
Unified ordering background interface product description Chinese garbled
Report on Market Research and investment prospects of China's silver powder industry (2022 Edition)
1. Color inversion, logarithmic transformation, gamma transformation source code - miniopencv from zero
Analysis of Top1 accuracy and top5 accuracy examples
被破解毁掉的国产游戏之光
根据csv文件某一列字符串中某个数字排序
备份与恢复 CR 介绍
灰度升级 TiDB Operator
Research and investment forecast report of citronellol industry in China (2022 Edition)
Restore backup data on S3 compatible storage with br
ESP series pin description diagram summary
Leetcode question brushing (5.31) string
FairGuard游戏加固:游戏出海热潮下,游戏安全面临新挑战
Summary of phased use of sonic one-stop open source distributed cluster cloud real machine test platform
从 SQL 文件迁移数据到 TiDB