Python PIL Image图片显示系列

时间:2022-03-13 04:36:51

1. PIL Image图片显示

  在使用PIL函数中的Image方法读取图片时,对于图片的shape,可能有不少宝宝存在疑惑。是什么疑惑了?就是image = Image.open(image_path),当你执行print(image.size)时,你得到的是图片的真实维度:W * H(宽和高),但在后续的使用中,我们使用的却是Image的数组形式,此时的维度是: H * W(高和宽)。怎么查询了它的数组形式了,再加一行代码,image = np.array(image),即可输出print(image.shape)查看维度,此时就是:H * W * C格式。

  但在Image转Tensor过程中,图片的格式会由: H * W * C的格式转为: C * H * W格式。

2. 例子

2.1 Code

 import numpy as np
import torchvision.transforms as transforms
from PIL import Image def image_open():
# 图片路径,相对路径
image_path = "./fusion_datasets/2.jpg"
# 读取图片
image = Image.open(image_path)
# 输出图片size
print("image_shape: ", image.size)
# 后续使用的numpy数组形式的格式
image_array = np.array(image)
print("image_array: ", image_array.shape)
# 显示图片
image.show()
# 转为tensor
input_transform = transforms.Compose([
transforms.ToTensor(),
])
image = input_transform(image).unsqueeze(0)
# 输出图片转为tensor后的格式
print("image_tensor: ", image.shape) if __name__ == '__main__':
image_open()

2.2 结果显示

原图片属性:

Python PIL Image图片显示系列

图片信息打印:

Python PIL Image图片显示系列

显示图片:

Python PIL Image图片显示系列

3. 总结

  努力去爱周围的每一个人,付出,不一定有收获,但是不付出就一定没有收获! 给街头卖艺的人零钱,不和深夜还在摆摊的小贩讨价还价。愿我的博客对你有所帮助(*^▽^*)(*^▽^*)!

  如果客官喜欢小生的园子,记得关注小生哟,小生会持续更新(#^.^#)(#^.^#)。

Python PIL Image图片显示系列的更多相关文章

  1. python PIL图像处理-图片上添加文字

    首先需要安装库pillow cmd安装命令:pip install pillow 安装完后,编写脚本如下: from PIL import Image, ImageDraw, ImageFont de ...

  2. python PIL实现图片合成

    在项目中需要将两张图片合在一起.遇到两种情况,一种就是两张非透明图片的合成, 一种是涉及到透明png的合成. 相关API见 http://pillow.readthedocs.io/en/latest ...

  3. python PIL比较图片像素

    # -*- coding: utf-8 -*- from PIL import Image from pylab import * def compare_pic_L(pic1,pic2): #打开第 ...

  4. python实现读取并显示图片的两种方法

    https://www.cnblogs.com/lantingg/p/9259840.html 在 python 中除了用 opencv,也可以用 matplotlib 和 PIL 这两个库操作图片. ...

  5. 基于Python PIL实现简单图片格式转化器

    基于Python PIL实现简单图片格式转化器 目录 基于Python PIL实现简单图片格式转化器 1.简介 2.前期资料准备 2.1.1如何实现图片格式转换? 2.1.2如何保存需要大小的图片? ...

  6. Python,PIL压缩裁剪图片

    自己写了用来压缩 DC 照片的,批量处理整目录文件,非常方便.需要安装 PIL #!/usr/bin/env python import Image import os import os.path ...

  7. python base64 编解码,转换成Opencv,PIL.Image图片格式

    二进制打开图片文件,base64编解码,转成Opencv格式: # coding: utf-8 import base64 import numpy as np import cv2 img_file ...

  8. Python PIL

    Python PIL PIL (Python Image Library) 库是Python 语言的一个第三方库,PIL库支持图像存储.显示和处理,能够处理几乎所有格式的图片. 一.PIL库简介 1. ...

  9. python PIL 图像处理操作

    python PIL 图像处理 # 导入Image库 import Image # 读取图片 im = Image.open("1234.jpg") # 显示图片 im.show( ...

随机推荐

  1. 你买了多少ERP?

    企业使用ERP(或是其他管理系统),有三种模式:自开发.采购.租赁(SAAS).不知道企业的各级管理者们,是否想过这个问题——我们到底买(做.租)了多少“量”的ERP呢? 这个问题也许不是那么好回答. ...

  2. Windows下安装 msysGit 以及初始化 Git server环境

    Windows下git工具msysGit使用以及Git server初始化 Windows下git工具,这里选择msysGit,版本为msysGit-netinstall-1.8.1.2-previe ...

  3. 注册dll

    unit Unit1; interface uses  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, Syste ...

  4. the5fire博客对接微信公众平台接口 | the5fire的技术博客

    the5fire博客对接微信公众平台接口 | the5fire的技术博客 the5fire博客对接微信公众平台接口

  5. HTML DOM应用案例2

    <html> <head> <title>day03</title> <script type="text/javascript&quo ...

  6. 2019-04-19 EasyWeb数据源问题

    问题描述:今天增加了个新模块,为了让各个模块自己保留自己的数据源配置,所以把数据源配置类放入自己模块包中,但是进行请求发现数据源会先选择默认的数据源,导致报错说主数据源下不存在表:当我把配置文件放回统 ...

  7. Convolutional Neural Network Architectures for Matching Natural Language Sentences

    interaction  n. 互动;一起活动;合作;互相影响 capture vt.俘获;夺取;夺得;引起(注意.想像.兴趣)n.捕获;占领;捕获物;[计算机]捕捉 hence  adv. 从此;因 ...

  8. Spring Boot 构建电商基础秒杀项目 &lpar;一&rpar; 项目搭建

    SpringBoot构建电商基础秒杀项目 学习笔记 Spring Boot 其实不是什么新的框架,它默认配置了很多框架的使用方式,就像 maven 整合了所有的 jar 包, Spring Boot ...

  9. 安装虚拟环境和Flask

    一.Flask 使用前准备 一. 安装及创建虚拟环境 1. 安装虚拟环境 win + R -> cmd -> pip install virtualenv -> 出现 Success ...

  10. python中下划线

    引用:https://blog.csdn.net/tcx1992/article/details/80105645?from=timeline Python中下划线的5种含义 class A(obje ...