Python读取保存图像文件
Python处理图像数据时通常需要对图像文件进行读取、保存等操作,因此将现有的方法归纳了一下。
1. PIL
依赖包:Pillow
安装:pip install Pillow
源码:
1 import numpy as np
2 from PIL import Image
3 import matplotlib.pyplot as plt
4
5 image = Image.open("image.jpg")
6
7 image_resize = image.resize((128, 128))
8 image_array = np.array(image_resize)
9 image_output = Image.fromarray(image_array)
10 image_output.save("image_output.jpg")
11
12 plt.imshow(image_output)
13 plt.axis("off")
14 plt.show()
2. opencv
依赖包:opencv-python
安装:pip install opencv-python
源码:
1 import cv2
2
3 image = cv2.imread("image.jpg")
4
5 image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
6 image_resize = cv2.resize(image, (128, 128))
7
8 cv2.imwrite("image_output.jpg", image_gray)
9 cv2.namedWindow("img_out")
10 cv2.imshow('img_out',image_gray)
11 cv2.waitKey(0)
12 cv2.destroyAllWindows()
3. imageio
依赖包:imageio
安装:pip install imageio
源码:
1 from imageio import imread, imsave
2 from scipy import ndimage
3 import matplotlib.pyplot as plt
4
5 image = imread("image.jpg")
6
7 img_rote_20 = ndimage.rotate(image, 20)
8 imsave("image_out.jpg", img_rote_20)
9
10 plt.imshow(img_rote_20)
11 plt.axis("off")
12 plt.show()
4. skimage (recommend to use)
依赖包:scikit-image
安装:pip install scikit-image
源码:
1 from skimage import io
2 import matplotlib.pyplot as plt
3
4 image = io.imread("image.jpg")
5
6 io.imsave("image_output.jpg", image)
7
8 io.imshow(image)
9 plt.show()
5. matplotlib
依赖包:matplotlib
安装:pip install matplotlib
源码:
1 import matplotlib.image as mpimg
2 import matplotlib.pyplot as plt
3
4 image = mpimg.imread("image.jpg")
5
6 mpimg.imsave("image_output.jpg", image)
7
8 plt.imshow(image)
9 plt.axis("off")
10 plt.show()
7. keras (machine learning or deep learning)
依赖包:keras
安装:pip install keras
源码:
1 from keras.preprocessing import image
2
3 img = image.load_img("image.jpg", target_size=(128, 128))
4
5 image_array = image.img_to_array(img)
6 image_output = image.array_to_img(img)
7
8 image_output.save("image_output.jpg")
上述七种方法均可以对图像文件进行读取保存处理,对日常cv预处理图像非常有帮助。
Python读取保存图像文件的更多相关文章
- Python 读取图像文件的性能对比
Python 读取图像文件的性能对比 使用 Python 读取一个保存在本地硬盘上的视频文件,视频文件的编码方式是使用的原始的 RGBA 格式写入的,即无压缩的原始视频文件.最开始直接使用 Pytho ...
- python读取,显示,保存mnist图片
python处理二进制 python的struct模块可以将整型(或者其它类型)转化为byte数组.看下面的代码. # coding: utf-8 from struct import * # 包装成 ...
- python读取excel一例-------从工资表逐行提取信息
在工作中经常要用到python操作excel,比如笔者公司中一个人事MM在发工资单的时候,需要从几百行的excel表中逐条的粘出信息,然后逐个的发送到员工的邮箱中.人事MM对此事不胜其烦,终于在某天请 ...
- python 读取图片的尺寸、分辨率
#需要安装PIL模块 #encoding=gbk#--------------------------------------------------------------------------- ...
- 【转】Python——读取html的table内容
Python——python读取html实战,作业7(python programming) 查看源码,观察html结构 # -*- coding: utf-8 -*- from lxml.html ...
- Python读取excel中的图片
作为Java程序员,Java自然是最主要的编程语言.但是Java适合完成大型项目,对于平时工作中小的工作任务,需要快速完成,易于修改和调试,使用Java显得很繁琐,需要进行类的设计,打成jar包,出现 ...
- python读取指定字节长度的文本
软件版本 Python 2.7.13; Win 10 场景描述 1.使用python读取指定长度的文本: 2.使用python读取某一范围内的文本. Python代码 test.txt文本内包含的 ...
- 【Python】Python 读取csv的某行或某列数据
Python 读取csv的某行 转载 2016年08月30日 21:01:44 标签: python / csv / 数据 站长用Python写了一个可以提取csv任一列的代码,欢迎使用.Gith ...
- python读取 ini 配置文件
在详解python读取ini文件之前,我们先说明一个ini文件的组成: 一个ini文件是由多个section组成,每个section中以key=vlaue形式存储数据: 然后我们来使用python读取 ...
- python读取txt文件时报错UnicodeDecodeError: 'gbk' codec can't decode byte 0x8e in position 8: illegal multibyte sequence
python读取文件时报错UnicodeDecodeError: 'gbk' codec can't decode byte 0x8e in position 8: illegal multibyte ...
随机推荐
- WCH沁恒 CH37系列芯片选型以及常见问题的处理(CH376/CH378)
选型 型号 接口 功能 备注 电源 CH372 并口 USB_Device 全速 USB 设备接口,兼容 USB V2.0 3.1-3.6 4.2-5.4 CH374 SPI/并口 Host/Devi ...
- CTF-MISC比赛技巧总结(一)
CTF-MISC比赛技巧总结之隐写术 一.第一阶段(观):1.flag藏在文本文件里面,直接ctrl+F就可以查找到:2.flag被字符隔开,在头或尾上,这个时候就只用消除间隔字符就可以了:3.fla ...
- ABP Framework 手动升级指南:从6.0.1升级到7.0.0
ABP 7.0.0 正式版已经发布,ABP-Framework-All-In-One 项目同步升级. LeptonX Lite Theme 目前还没有包含在源码解决方案中,还是以 Nuget 包提供, ...
- VS2019注册码
Visual Studio 2019 Enterprise BF8Y8-GN2QH-T84XB-QVY3B-RC4DF Visual Studio 2019 Professional NYWVH-HT ...
- [LeetCode]二进制求和
题目 代码 class Solution { public: string addBinary(string a, string b) { int lenA = a.length(); int len ...
- printf()和scanf()的*修飾符
如果你不想預先設置字段的寬度,想通過程序來進行設定,則可以可以使用"*"來進行修飾字段的寬度,前提是在程序中要包含"*"和參數對應的值(比如%*d,那麽參數應該 ...
- angular---处于激活状态的路由加样式
- angular--路由导航三种方法
- 设置多个系统---vue-el-admin
1. 修改\src\settings.js const ppp= { title: 'XXXX System', titleZH: 'XXXX系統', flag: 'ppp' } const syst ...
- java入门与进阶P-2.3
判断 if语句 一个基本的if语句由一个关键字if开头,跟上在括号里的表示条件的逻辑表达式, 然后是一对大括号"{}"之间的若干条语句.如果表示条件的逻辑表达式的结果为true,那 ...