Python Pillow(PIL 第三方模块)和 cv2 (opencv第三方模块)对图片的 resize 操作 (缩放图片大小)
PIL 模块的 resize 操作:
1. 从文件中读取图片,然后 resize 大小:
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image img=Image.open(r"1.jpg")
print("原图的height,weight分别为:", np.asarray(img).shape[:2])
plt.imshow(np.asarray(img))
plt.show() height, weight = (np.asarray(img).shape)[:2]
height = height//10
weight = weight//10
img2 = Image.Image.resize(img, (weight, height))
print("resized后图的height,weight分别为:", np.asarray(img2).shape[:2])
plt.imshow(np.asarray(img2))
plt.show()


2. 从字节码(Bytes)中读取图片,然后 resize 大小:
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
from io import BytesIO img = open("1.jpg", "rb").read() #读取序列化的二进制码
img = BytesIO( img )
img = Image.open( img )
print("原图的height,weight分别为:", np.asarray(img).shape[:2])
plt.imshow(np.asarray(img))
plt.show() height, weight = (np.asarray(img).shape)[:2]
height = height//10
weight = weight//10
img2 = Image.Image.resize(img, (weight, height))
print("resized后图的height,weight分别为:", np.asarray(img2).shape[:2])
plt.imshow(np.asarray(img2))
plt.show()
---------------------------------------------------
CV2 模块的 resize 操作:
读入图像
使用函数cv2.imread()来读取图像。图像应该在工作目录中,或者应该给出图像的完整路径。
imread(filename[, flags]) -> retval
函数imread从指定文件加载图像并返回一个numpy.ndarray对象类型像素值。 如果图像无法读取(由于文件丢失,权限不当,格式不受支持或格式无效),函数返回一个空矩阵
第二个参数是一个标志,用于指定应读取图像的方式。
- cv2.IMREAD_COLOR:加载彩色图像。图像的任何透明度都将被忽略。这是默认标志。 flags=1
- cv2.IMREAD_GRAYSCALE:以灰度模式加载图像 flags=0
- cv2.IMREAD_UNCHANGED:加载包含Alpha通道的图像 flags=-1
注意
而不是这三个标志,你可以简单地传递整数1,0或-1。
取自于: https://blog.csdn.net/hubingshabi/article/details/80144706
CV2 读取图片, CV2展示图片:
import matplotlib.pyplot as plt
import numpy as np
import cv2
# rgb图
img=cv2.imread(r"1.jpg", 1)
# 灰度图
#img=cv2.imread(r"1.jpg", 0) print("原图的height,weight分别为:", np.asarray(img).shape[:2])
#plt.imshow(np.asarray(img))
#plt.show()
cv2.imshow("img", mat=img)
cv2.waitKey (0) height, weight = (img.shape)[:2]
height = height//3
weight = weight//3
img2 = cv2.resize(img, (weight, height))
print("resized后图的height,weight分别为:", img2.shape[:2])
#plt.imshow(np.asarray(img2))
#plt.show()
cv2.imshow("img2", mat=img2)
cv2.waitKey (0)
cv2.destroyAllWindows()
CV2 读取图片, matplotlib展示图片: 把cv2的bgr转换为rgb,然后展示。 [...,::-1]
import matplotlib.pyplot as plt
import numpy as np
import cv2
# rgb图
img=cv2.imread(r"1.jpg", 1)
# 灰度图
#img=cv2.imread(r"1.jpg", 0) print("原图的height,weight分别为:", np.asarray(img).shape[:2])
plt.imshow(np.asarray(img)[...,::-1])
plt.show() height, weight = (img.shape)[:2]
height = height//3
weight = weight//3
img2 = cv2.resize(img, (weight, height))
print("resized后图的height,weight分别为:", img2.shape[:2])
plt.imshow(np.asarray(img2)[...,::-1])
plt.show()
-----------------------------------------------------------------
参考网址:
https://blog.csdn.net/sinat_26917383/article/details/78559709
https://blog.csdn.net/hubingshabi/article/details/80144706
----------------------------

1.jpg
注:上面的代码均使用该图片(1.jpg)做测试。
--------------------------------
Python Pillow(PIL 第三方模块)和 cv2 (opencv第三方模块)对图片的 resize 操作 (缩放图片大小)的更多相关文章
- Python用Pillow(PIL)进行简单的图像操作
Python用Pillow(PIL)进行简单的图像操作 颜色与RGBA值 计算机通常将图像表示为RGB值,或者再加上alpha值(通透度,透明度),称为RGBA值.在Pillow中,RGBA的值表示为 ...
- python base64 编解码,转换成Opencv,PIL.Image图片格式
二进制打开图片文件,base64编解码,转成Opencv格式: # coding: utf-8 import base64 import numpy as np import cv2 img_file ...
- 图像处理基本工具——Python 环境下的 Pillow( PIL )
由于笔者近期的研究课题与图像后处理有关,需要通过图像处理工具对图像进行变换和处理,进而生成合适的训练图像数据.该系列文章即主要记录笔者在不同的环境下进行图像处理时常用的工具和库.在 Python 环境 ...
- python Pillow 图片处理模块,好强大有没有
python Pillow 图片处理模块,好强大有没有 Pillow 需要给 python 另外安装 第一个用法:https://www.cnblogs.com/ibingshan/p/1105739 ...
- 【Python】PIL模块
Python自建库,在爬虫等基础应用中更加简单好记,做整理以备自查. 目录 Image模块 open类.Save类.format类.Mode类.convert类.Size类.Info类.new类.Co ...
- ❤️【Python从入门到精通】(二十六)用Python的PIL库(Pillow)处理图像真的得心应手❤️
您好,我是码农飞哥,感谢您阅读本文,欢迎一键三连哦. 本篇重点介绍Python处理图像的标准库PIL库,处理图像真的的很方便. 干货满满,建议收藏,需要用到时常看看. 小伙伴们如有问题及需要,欢迎踊跃 ...
- 【python】import 模块、包、第三方模块
xx.py文件,称为模块(module),把不同模块归整到一起的文件夹,叫做包(package) 不同包下的模块可以重名,但是都不能和系统内建模块重名 包里面一定要有个__init__.py文件,否则 ...
- 使用Python的PIL模块来进行图片对比
使用Python的PIL模块来进行图片对比 在使用google或者baidu搜图的时候会发现有一个图片颜色选项,感觉非常有意思,有人可能会想这肯定是人为的去划分的,呵呵,有这种可能,但是估计人会累死, ...
- Python基础之模块:5、 第三方模块 requests模块 openpyxl模块
目录 一.第三方模块的下载与使用 1.什么是第三方模块 2.如何安装第三方模块 方式一:pip工具 方式二:pycharm中下载 3.注意事项 1.报错并有警告信息 2.报错,提示关键字 3.报错,无 ...
- python基础篇 13-模块的导入 安装第三方模块
一.模块 一个python文件就是一个模块 标准模块(内置模块) 第三方模块 需要自己安装的 自己写的 需要导入的 import 一个模块的实质: 实际上就是把一个py文件从头到尾执行了一遍,main ...
随机推荐
- windows 命令行调整跃点数
先用 route print -4 命令找到接口号: 接口列表10...00 ff 51 c4 53 b4 ......TAP-Windows Adapter V918...18 c0 4d 29 5 ...
- pyenv-win 替换国内镜像源
前情提要 今天心血来潮想学一学python 然后因为python版本众多,了解到了pyenv这个python版本管理器 在github下载好pyenv以后,打算先安装一个稳定的版本 pyenv ins ...
- RSS 解析:全球内容分发的利器及使用技巧
使用 RSS 可以将最新的网络内容从一个网站分发到全球数千个其他网站. RSS 允许快速浏览新闻和更新. RSS 文档示例 <?xml version="1.0" encod ...
- Jenkins发布服务报错Fatal error: put encountered an exception while uploading磁盘空间不足处理 No space left on device
Jenkins发布服务报错Fatal error: put encountered an exception while uploading磁盘空间不足处理 No space left on devi ...
- SDL3 入门(3):三角形
SDL3 提供了 SDL_RenderGeometry 函数绘制几何图形,用法和 OpenGL 差不多,先定义顶点数据,然后根据顶点数据绘制几何图形. 绘制三角形的代码如下: std::array&l ...
- CLR via C# 笔记 -- 数组(16)
1. 数组隐式继承 System.Array,所以数组是引用类型.变量包含的是对数组的引用,而不是包含数据本身的元素. 2. 数组协变性.将数组从一种类型转换为另一种类型. string[] sa = ...
- 小米节假日API, 查询调休
小米的节假日API, 用于查询一年中的第X天是否正在放假或是在调休. 在浏览器中打开保存下来, 一年只需要调用一次即可. https://api.comm.miui.com/holiday/holid ...
- 一款开源、免费、现代化风格的WPF UI控件库 - ModernWpf
前言 今天大姚给大家分享一款开源(MIT License).免费.现代化风格的WPF UI控件库:ModernWpf. 项目介绍 ModernWpf是一个开源项目,它为 WPF 提供了一组现代化的控件 ...
- Docker部署JavaWeb项目(Tomcat环境)
一.环境准备 1.首先准备一台Centos 7的虚拟机并安装Docker. 2.准备好Tomcat.JDK的安装包以及该java项目的war包. 二.编写Dockerfile Dockerfile是一 ...
- Servlet3.0+SpringBoot2.X注解Listener常用监听器
监听器:应用启动监听器,会话监听器,请求监听器 作用: ServletContextListener 应用启动监听 HttpSessionLisener 会话监听 ServletRequestList ...