简体   繁体   English

如何在Python中打开图像

[英]How to open up an Image in Python

I am new to Stackoverflow and still learning python but I am attempting to begin a project of mine. 我是Stackoverflow的新手,仍然在学习python,但是我试图开始我的项目。 I plan on creating a filter for images using python. 我计划使用python创建图像过滤器。 The first step I would like to learn is how to open up an image in python. 我想学习的第一步是如何在python中打开图像。 This is what I have so far: 这是我到目前为止的内容:

from PIL import Image
im = Image.open('Jordan.jpg')
im.show()

Am I on the right track? 我在正确的轨道上吗? Iam using Python 2.7 使用Python 2.7的Iam

the most natural python object that corresponds to an image is a multi-dimensional array; 与图像相对应的最自然的python对象是多维数组; that's the most natural representation and it's also the best data structure to manipulate an image, as you intend to do by creating a filter. 这是最自然的表示方式,也是处理图像的最佳数据结构,正如您打算通过创建过滤器来做的那样。

Python has an array module, however, for creating and manipulating multi-dimensional arrays (eg, creating a filter), i highly recommend installing two python libraries NumPy and SciPy either from the links supplied here or pip installing them: Python有一个数组模块,但是,用于创建和处理多维数组(例如,创建过滤器),我强烈建议从此处提供的链接中安装两个python库NumPySciPy或通过pip安装它们:

pip install numpy
pip install scipy


>>> from scipy import misc as MSC
>>> imfile = "~/path/to/an/image.jpg"

use the imread function in scipy's miscellaneous module, which in fact just calls PIL under the hood; 在scipy的其他模块中使用imread函数,该模块实际上只是在后台调用PIL; this function will read in jpeg as well as png images 此功能将读取jpeg以及png图像

>>> img = MSC.imread(imfile)

verify that it is a NumPy array 验证它是一个NumPy数组

>>> isinstance(img, NP.ndarray)
True 

a color image has three axes: X (width) Y (height), and Z (the three elements that comprise an RGB tuple; 彩色图像具有三个轴:X(宽度),Y(高度)和Z(构成RGB元组的三个元素;

first two axes are the pixel width and height of the image, the last dimension should be '3' (or '4' for an 'png' image which has an alpha channel) 前两个轴是图像的像素宽度和高度,最后一个尺寸应为“ 3”(对于具有Alpha通道的“ png”图像,则为“ 4”)

>>> img.shape
(200, 200, 3)

selecting one pixel roughly from the image center (a single RGB 3-tuple) 从图像中心大致选择一个像素(单个RGB三元组)

>>> img[100,100,:]
array([83, 26, 17], dtype=uint8)

if you call Matplotlib's imshow method and pass in this NumPy array, it will render the image 如果您调用Matplotlib的imshow方法并传递此NumPy数组,它将渲染图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM