当前位置:网站首页>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()
边栏推荐
- 移位运算符
- Synchronized solves problems caused by sharing
- Zhong Xuegao, who cannot be melted, cannot escape the life cycle of online celebrity products
- China high purity silver nitrate Market Research and investment strategy report (2022 Edition)
- Rviz仿真时遇到机器人瞬间回到世界坐标原点的问题及可能原因
- MySQL learning record 11jdbcstatement object, SQL injection problem and Preparedstatement object
- pcd转ply后在meshlab无法打开,提示 Error details: Unespected eof
- JVM 快速入门
- 游戏解包的危害及资源加密的重要性
- 从 CSV 文件迁移数据到 TiDB
猜你喜欢
![[research materials] 2021 China online high growth white paper - Download attached](/img/51/bea6179e4fac88f8b550b4213a2bca.jpg)
[research materials] 2021 China online high growth white paper - Download attached

【MySQL】日志

MySQL learning record 11jdbcstatement object, SQL injection problem and Preparedstatement object

hcip--mpls

Online yaml to CSV tool

2022.02.13 - NC003. Design LRU cache structure

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

化不掉的钟薛高,逃不出网红产品的生命周期

Chinese Remainder Theorem (Sun Tzu theorem) principle and template code

延迟初始化和密封类
随机推荐
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
hcip--mpls
Deep learning: derivation of shallow neural networks and deep neural networks
[cloud native] teach you how to build ferry open source work order system
Verrouillage [MySQL]
Let the bullets fly for a while
2022 Inner Mongolia latest construction tower crane (construction special operation) simulation examination question bank and answers
从表中名称映射关系修改视频名称
Nacos Development Manual
Unified ordering background interface product description Chinese garbled
1. Color inversion, logarithmic transformation, gamma transformation source code - miniopencv from zero
JS native implementation shuttle box
按位逻辑运算符
Use dumping to back up tidb cluster data to S3 compatible storage
Zhong Xuegao, who cannot be melted, cannot escape the life cycle of online celebrity products
Research Report on Market Research and investment strategy of microcrystalline graphite materials in China (2022 Edition)
JVM 快速入门
vulnhub hackme: 1
堆排序详解
All the ArrayList knowledge you want to know is here