热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

python图像处理库_使用Python图像处理库处理图像

python图像处理库Inmypreviousarticleontime-savingtipsforPythonists,ImentionedthatPythonisalangua

python图像处理库

In my previous article on time-saving tips for Pythonists, I mentioned that Python is a language that can inspire love in its users.

在我以前关于Python 节省时间技巧的文章中 ,我提到Python是一种可以激发用户爱的语言。

One reason for this is the number of time-saving libraries available for this language. A nice example is the Python Imaging Library (PIL), which is the focus of this article.

造成这种情况的原因之一是可以使用这种语言的省时的库数量很多。 一个很好的例子是Python Imaging Library(PIL) ,这是本文的重点。

您可以使用PIL做什么 (What You Can Do with PIL)

PIL is a free library that adds image processing capabilities to your Python interpreter, supporting a range of image file formats such as PPM, PNG, JPEG, GIF, TIFF and BMP.

PIL是一个免费的库,它为您的Python解释器添加了图像处理功能,支持多种图像文件格式,例如PPM,PNG,JPEG,GIF,TIFF和BMP。

PIL offers several standard procedures for image processing/manipulation, such as:

PIL提供了几种用于图像处理/操纵的标准程序,例如:

  • pixel-based manipulations

    基于像素的操作
  • masking and transparency handling

    遮罩和透明处理
  • filtering (for example, blurring, contouring, smoothing, edge detection)

    过滤(例如,模糊,轮廓绘制,平滑,边缘检测)
  • image enhancement (for example, sharpening, brightness adjustment, contrast)

    图像增强(例如,锐化,亮度调整,对比度)
  • geometrical, color and other transforms

    几何,颜色和其他变换
  • adding text to images

    向图像添加文本
  • cutting, pasting and merging images

    剪切,粘贴和合并图像
  • creating thumbnails.

    创建缩略图。

PIL和枕头 (PIL and Pillow)

One issue with PIL is that its most recent version, 1.1.7, was released in 2009, and only supports Python 1.5.2–2.7. Although the PIL site promises a forthcoming version for Python 3.X, its last commit was in 2011, and it appears that development has been discontinued.

PIL的一个问题是其最新版本1.1.7于2009年发布,仅支持Python 1.5.2–2.7。 尽管PIL网站承诺即将发布适用于Python 3.X的版本,但其最后一次提交是在2011年,并且看来开发已经停止。

Fortunately, all is not lost for Python 3.X users. A project called Pillow has forked the PIL repository and added Python 3.X support. Given that most readers will probably be working with Python 3.X, I'll focus on the Pillow update in this article.

幸运的是,Python 3.X用户并不会丢失所有内容。 一个名为Pillow的项目已经分叉了PIL存储库,并添加了Python 3.X支持。 鉴于大多数读者可能会使用Python 3.X,因此本文将重点介绍Pillow更新。

安装枕 (Installing Pillow)

Since Pillow supports versions of Python back to Python 2.4, I'll only focus on installing Pillow and not the older version of PIL.

由于Pillow支持Python版本回到Python 2.4,因此我将只专注于安装Pillow,而不是较早版本的PIL。

在Mac上使用Python (Python on a Mac)

I'm currently writing this tutorial on a Mac OS X Yosemite 10.10.5, and thus will describe how to install Pillow on a Mac OS X machine. But, don't worry, I'll provide a link at the end of this section that describes how to install Pillow on other operating systems.

我目前正在Mac OS X Yosemite 10.10.5上编写本教程,因此将描述如何在Mac OS X机器上安装Pillow。 但是,不用担心,我将在本节末尾提供一个链接,该链接描述如何在其他操作系统上安装Pillow。

I just want to note here that Mac OS X comes with Python pre-installed. However, the version most likely will be prior to 3.X.

我只想在此指出Mac OS X预先安装了Python。 但是,该版本最有可能在3.X之前。

For instance, on my machine, when I run $ python --version in the terminal, I get Python 2.7.10.

例如,在我的机器上,当我在终端中运行$ python --version时,我得到Python 2.7.10

Python和pip (Python and pip)

A very easy way to install Pillow is through pip.

安装Pillow的一种非常简单的方法是通过pip 。

If you don't have pip installed on your machine, simply type the following command in your terminal, and you're all done:

如果您的计算机上未安装pip,只需在终端中键入以下命令,即可完成操作:

$ sudo easy_install pip

Now, to install Pillow, simply type the following in your terminal:

现在,要安装Pillow,只需在终端中键入以下内容:

$ sudo pip install pillow

That was easy, wasn't it?

那很容易,不是吗?

As I promised, for installing Pillow on other operating systems, you can find the instructions for that here.

如我所承诺的,要在其他操作系统上安装Pillow,您可以在此处找到相关说明。

一些例子 (Some Examples)

In this section, I'll demonstrate a few simple things we can do with PIL.

在本节中,我将演示我们可以使用PIL进行的一些简单操作。

I'll perform these tests on the following image:

我将在以下图像上执行这些测试:

brick house

If you'd like to follow along with these examples, download the image.

如果您想遵循这些示例,请下载图片。

读取图像 (Read an image)

This is the most basic operation in an image processing task, since to process an image, you must read it first. With PIL, this can be easily accomplished as follows:

这是图像处理任务中最基本的操作,因为要处理图像,必须先阅读它。 使用PIL,可以很容易地完成以下操作:

from PIL import Image
img = Image.open('brick-house.png')

Notice here that img is a PIL image object, created by the open() function, which is part of the PIL Image module.

注意, img是一个由open()函数创建的PIL图像对象,它是PIL Image模块的一部分。

You can also read already open files, or from a string, or from a tar archive.

您也可以从字符串或tar存档中读取已打开的文件。

将图像转换为灰度,显示并保存 (Convert an image to grayscale, display it, and save it)

The file brick-house.png is a color image. To convert it to grayscale, display it, and then save the new grayscale image, you can simply do the following:

文件brick-house.png是彩色图像。 要将其转换为灰度 ,显示它,然后保存新的灰度图像,您只需执行以下操作:

from PIL import Image
img = Image.open('brick-house.png').convert('L')
img.show()
img.save('brick-house-gs','png')

Notice that we have used three main functions to perform this operation: convert(), show(), and save(). Since we want to convert to a grayscale image, the L parameter was used with convert().

注意,我们使用了三个主要函数来执行此操作: convert() , show()save() 。 由于我们要转换为灰度图像,因此将L参数与convert()

Here is the returned image:

这是返回的图像:

brick house grayscale version

转换为其他图像类型 (Convert to another image type)

The image we are working on is of type png. Say that you want to convert it to another image type, for instance jpg. This operation can be done using the save() function we used to save our result (write output to disk) in the above subsection:

我们正在处理的图像是png类型。 假设您要将其转换为其他图像类型,例如jpg 。 可以使用上面小节中用来保存结果(将输出写入磁盘)的save()函数来完成此操作:

from PIL import Image
img = Image.open('brick-house.png')
img.save('brick-image','jpeg')

调整图像大小 (Resize an image)

The size (dimensions) of our original image is 440 x 600px. If we want to resize it, and make it of size 256 x 256px, this can be done as follows:

我们原始图像的大小(尺寸)为440 x 600像素。 如果我们要调整大小,并使其尺寸为256 x 256像素,则可以按以下步骤进行操作:

from PIL import Image
img = Image.open('brick-house.png')
new_img = img.resize((256,256))
new_img.save('brick-house-256x256','png')

This produces a new square image:

这将产生一个新的正方形图像:

brick house resized

As you can see, this squeezes the image into the desired dimensions rather than cropping it, which may not be what you want. You can, of course, also crop the image while retaining the proper aspect ratio.

如您所见,这会将图像压缩到所需的尺寸,而不是裁剪图像,这可能不是您想要的。 当然,您也可以在保留适当的宽高比的同时裁剪图像。

结论 (In Conclusion)

This quick introduction is just intended to scratch the surface of PIL and to demonstrate how easily some complex image processing tasks can be accomplished in Python through the PIL library.

这份快速入门只是为了了解PIL的表面,并演示如何通过PIL库在Python中轻松完成某些复杂的图像处理任务。

The many other operations you can perform using this library are described in the comprehensive Pillow documentation, where you can read more details about the issues described above, along with handy tutorials.

全面的Pillow文档中介绍了可以使用此库执行的许多其他操作,您可以在其中阅读有关上述问题的更多详细信息以及方便的教程。

I hope this introduction has inspired you to try out image manipulation with Python. Have fun!

我希望这篇介绍会启发您尝试使用Python进行图像处理。 玩得开心!

翻译自: https://www.sitepoint.com/manipulating-images-with-the-python-imaging-library/

python图像处理库



推荐阅读
  • 高效解决应用崩溃问题!友盟新版错误分析工具全面升级
    友盟推出的最新版错误分析工具,专为移动开发者设计,提供强大的Crash收集与分析功能。该工具能够实时监控App运行状态,快速发现并修复错误,显著提升应用的稳定性和用户体验。 ... [详细]
  • 本文详细介绍如何使用Python进行配置文件的读写操作,涵盖常见的配置文件格式(如INI、JSON、TOML和YAML),并提供具体的代码示例。 ... [详细]
  • 深入理解Tornado模板系统
    本文详细介绍了Tornado框架中模板系统的使用方法。Tornado自带的轻量级、高效且灵活的模板语言位于tornado.template模块,支持嵌入Python代码片段,帮助开发者快速构建动态网页。 ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • Python自动化处理:从Word文档提取内容并生成带水印的PDF
    本文介绍如何利用Python实现从特定网站下载Word文档,去除水印并添加自定义水印,最终将文档转换为PDF格式。该方法适用于批量处理和自动化需求。 ... [详细]
  • 本文详细解析了Python中的os和sys模块,介绍了它们的功能、常用方法及其在实际编程中的应用。 ... [详细]
  • RecyclerView初步学习(一)
    RecyclerView初步学习(一)ReCyclerView提供了一种插件式的编程模式,除了提供ViewHolder缓存模式,还可以自定义动画,分割符,布局样式,相比于传统的ListVi ... [详细]
  • 本文探讨了 Spring Boot 应用程序在不同配置下支持的最大并发连接数,重点分析了内置服务器(如 Tomcat、Jetty 和 Undertow)的默认设置及其对性能的影响。 ... [详细]
  • golang常用库:配置文件解析库/管理工具viper使用
    golang常用库:配置文件解析库管理工具-viper使用-一、viper简介viper配置管理解析库,是由大神SteveFrancia开发,他在google领导着golang的 ... [详细]
  • 本文详细介绍了Java中org.neo4j.helpers.collection.Iterators.single()方法的功能、使用场景及代码示例,帮助开发者更好地理解和应用该方法。 ... [详细]
  • 根据最新发布的《互联网人才趋势报告》,尽管大量IT从业者已转向Python开发,但随着人工智能和大数据领域的迅猛发展,仍存在巨大的人才缺口。本文将详细介绍如何使用Python编写一个简单的爬虫程序,并提供完整的代码示例。 ... [详细]
  • 尽管深度学习带来了广泛的应用前景,其训练通常需要强大的计算资源。然而,并非所有开发者都能负担得起高性能服务器或专用硬件。本文探讨了如何在有限的硬件条件下(如ARM CPU)高效运行深度神经网络,特别是通过选择合适的工具和框架来加速模型推理。 ... [详细]
  • CentOS系统安装与配置常见问题及解决方案
    本文详细介绍了在CentOS系统安装过程中遇到的常见问题及其解决方案,包括Vi编辑器的操作、图形界面的安装、网络连接故障排除等。通过本文,读者可以更好地理解和解决这些常见问题。 ... [详细]
  • 利用决策树预测NBA比赛胜负的Python数据挖掘实践
    本文通过使用2013-14赛季NBA赛程与结果数据集以及2013年NBA排名数据,结合《Python数据挖掘入门与实践》一书中的方法,展示如何应用决策树算法进行比赛胜负预测。我们将详细讲解数据预处理、特征工程及模型评估等关键步骤。 ... [详细]
  • 采用IKE方式建立IPsec安全隧道
    一、【组网和实验环境】按如上的接口ip先作配置,再作ipsec的相关配置,配置文本见文章最后本文实验采用的交换机是H3C模拟器,下载地址如 ... [详细]
author-avatar
好人杨华_840
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有