未加星标

An Intro to the Python Imaging Library / Pillow

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二03 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

The python Imaging Library or PIL allowed you to do image processing in Python. The original author, Fredrik Lundh, wrote one of my favorite Python blogs when I first started learning Python. However PIL’s last release was way back in 2009 and the blog also stopped getting updated. Fortunately, there were some other Python folks that came along and forked PIL and called their project pillow. The pillow project is a drop-in replacement for PIL that also supports Python 3, something PIL never got around to doing.

Please note that you cannot have both PIL and pillow installed at the same time. There are some warnings in their documentation that list some differences between PIL and pillow that get updated from time to time, so I’m just going to direct you there instead of repeating them here since they will likely become out of date.

Install pillow

You can install pillow using pip or easy_install. Here’s an example using pip:

pip install Pillow

Note that if you are on linux or Mac, you may need to run the command with sudo.

Opening Images
An Intro to the Python Imaging Library / Pillow

Pillow makes it easy to open an image file and display it. Let’s take a look:

from PIL import Image image = Image.open('/path/to/photos/jelly.jpg') image.show()

Here we just import the Image module and ask it to open our file. If you go and read the source, you will see that on Unix, the open method saves the images to a temporary PPM file and opens it with the xv utility. On my Linux machine, it opened it with ImageMagick, for example. On windows, it will save the image as a temporary BMP and open it in something like Paint.

Getting Image Information

You can get a lot of information about an image using pillow as well. Let’s look at just a few small examples of what we can extract:

>>> from PIL import Image >>> image = Image.open('/path/to/photos/jelly.jpg') >>> r, g, b = image.split() >>> histogram = image.histogram() [384761, 489777, 557209, 405004, 220701, 154786, 55807, 35806, 21901, 16242] >>> exif = image._getexif() exif {256: 1935, 257: 3411, 271: u'Panasonic', 272: u'DMC-LX7', 274: 1, 282: (180, 1), 283: (180, 1), 296: 2, 305: u'PaintShop Pro 14.00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', 306: u'2016:08:21 07:54:57', 36867: u'2016:08:21 07:54:57', 36868: u'2016:08:21 07:54:57', 37121: '\x01\x02\x03\x00', 37122: (4, 1), 37381: (124, 128), 37383: 5, 37384: 0, 37385: 16, 37386: (47, 10), 40960: '0100', 40961: 1, 40962: 3968, 40963: 2232, 41495: 2, 41728: '\x03', 41729: '\x01', 41985: 0, 41986: 0, 41987: 0, 41988: (0, 10), 41989: 24, 41990: 0, 41991: 0, 41992: 0, 41993: 0, 41994: 0}

In this example, we show how to extract the RGB (red, green, blue) values from the image. We also learn how to get a histrogram from the image. Note that I truncated the output a bit as the histogram’s output was much larger. You could graph the histrogram using another Python package, such as matplotlib. Finally, the example above demonstrates how to extract the EXIF information from the image. Again, I have shortened the output from this method a bit as it contained way too much information for this article.

Cropping Images

You can also crop images with pillow. It’s actually quite easy, although it may take you a little trial and error to figure it out. Let’s try cropping our jellyfish photo:

>>> from PIL import Image >>> image = Image.open('/path/to/photos/jelly.jpg') >>> cropped = image.crop((0, 80, 200, 400)) >>> cropped.save('/path/to/photos/cropped_jelly.png')

You will note that all we need to do is open the image and then call its crop method. You will need to pass in the x/y coordinates that you want to crop too, i.e. (x1, y1, x2, y2). In pillow, the 0 pixel is the top left pixel. As you increase your x value, it goes to the right. As you increase the y value, you go down the image. When you run the code above, you’ll end up with the following image:


An Intro to the Python Imaging Library / Pillow

That’s a pretty boring crop. I want to crop the jellyfish’s “head”. To get the right coordinates quickly, I used Gimp to help me figure out what coordinates to use for my next crop.

>>> from PIL import Image >>> image = Image.open('/path/to/photos/jelly.jpg') >>> cropped = image.crop((177, 882, 1179, 1707)) >>> cropped.save('/path/to/photos/cropped_jelly2.png')

If we run this code, we’ll end up with the following cropped version:


An Intro to the Python Imaging Library / Pillow

That’s much better!

Using Filters
An Intro to the Python Imaging Library / Pillow
Original Jellyfish

There are a variety of filters that you can use in pillow to apply to your images. They are contained in the ImageFilter module. Let’s look at a couple of them here:

>>> from PIL import ImageFilter >>> from PIL import Image >>> image = Image.open('/path/to/photos/jelly.jpg') >>> blurred_jelly = image.filter(ImageFilter.BLUR) >>> blurred_jelly.save('/path/to/photos/blurry_jelly.png')

This will blur the jellyfish photo slightly. Here’s the result I got:


An Intro to the Python Imaging Library / Pillow
Blurry Jellyfish

Of course, most people like their images sharper rather than blurrier. Pillow has your back. Here’s one way to sharpen the image:

>>> from PIL import ImageFilter >>> from PIL import Image >>> image = Image.open('/path/to/photos/jelly.jpg') >>> blurred_jelly = image.filter(ImageFilter.SHARPEN) >>> blurred_jelly.save('/path/to/photos/sharper_jelly.png')

When you run this code, you’ll end up with the following:


An Intro to the Python Imaging Library / Pillow
Sharper Jellyfish

You can also use the ImageEnhance module for sharpening your photos, among other things.

There are other filters that you can apply too, such as DETAIL, EDGE_ENHANCE, EMBOSS, SMOOTH, etc. You can also write your code in such a way that you can apply multiple filters to your image.

You will likely need to download the images above to really be able to compare the differences in the filters.

Wrapping Up

You can do much more with the pillow package than what is covered in this short article. Pillow supports image transforms, processing bands, image enhancements, the ability to print your images and much more. I highly recommend reading the pillow documentation to get a good grasp of everything you can do.

Related Reading Python pillow official website Pillow documentation Wikipedia article on the Python Imaging Library PyPI: PIL

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

主题: LinuxPythonWindowsEDGE
分页:12
转载请注明
本文标题:An Intro to the Python Imaging Library / Pillow
本站链接:http://www.codesec.net/view/480147.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(44)