Image Processing in Python with Pillow

Introduction

A lot of applications use digital images, and with this there is usually a need to process the images used. If you are building your application with Python and need to add image processing features to it, there are various libraries you could use. Some popular ones are OpenCVscikit-imagePython Imaging Library and Pillow.

We won’t debate on which library is the best here, they all have their merits. This article will focus on Pillow, a library that is powerful, provides a wide array of image processing features, and is simple to use.

Pillow is a fork of the Python Imaging Library (PIL). PIL is a library that offers several standard procedures for manipulating images. It’s a powerful library, but hasn’t been updated since 2011 and doesn’t support Python 3. Pillow builds on this, adding more features and support for Python 3. It supports a range of image file formats such as PNG, JPEG, PPM, GIF, TIFF and BMP. We’ll see how to perform various operations on images such as cropping, resizing, adding text to images, rotating, greyscaling, e.t.c using this library.

Installation and Project Setup

Before installing Pillow, there are some prerequisites that must be satisfied. These vary for different operating systems. We won’t list the different options here, you can find the prerequisites for your particular OS in this installation guide.

After installing the prerequisite libraries, you can install Pillow with `pip:

$ pip install Pillow

To follow along, you can download the images (coutesy of Unsplash) that we’ll use in the article. You can also use your own images.

All examples will assume the required images are in the same directory as the python script file being run.

The Image Object

A crucial class in the Python Imaging Library is the Image class. It is defined in the Image module and provides a PIL image on which manipulation operations can be carried out. An instance of this class can be created in several ways: by loading images from a file, creating images from scratch or as a result of processing other images. We’ll see all these in use.

To load an image from a file, we use the open() function in the Image module passing it the path to the image.

from PIL import Image

image = Image.open('unsplash_01.jpg')

If successful, the above returns an Image object. If there was a problem opening the file, an IOError exception will be raised.

After obtaining an Image object, you can now use the methods and attributes defined by the class to process and manipulate it. Let’s start by displaying the image. You can do this by calling the show() method on it. This displays the image on an external viewer (usually xv on Unix, and the Paint program on Windows).

image.show()

You can get some details about the image using the object’s attributes.

# The file format of the source file.
print(image.format) # Output: JPEG

# The pixel format used
read more

介绍一个python视频处理库:moviepy

   处理视频是一个常见的需求。那么在python中如何用代码处理视频呢?最近我无意间发现了一个很好用的python视频处理库moviepy,其使用起来简单易用,而且功能比较强大,这里记录一下分享给大家。

  • What is moviepy?

   以下的介绍来自moviepy的官方文档

moviepy是一个用于视频编辑的python模块,其可以用来对视频进行一些基础的操作(比如剪切,连接,插入标题等),视频创作(比如非线性编辑),视频处理以及给视频增加一些炫酷的特效等。它可以读写大多数常见的视频格式,包括GIF。

  • How to install moviepy?

   moviepy的安装非常简单,直接使用命令pip install moviepy即可。当然你也可以选择从源码安装,下载源码包,解压缩,然后切换到含有setup.py的目录,执行 python setup.py install 即可。

   moviepy的底层依赖于Numpy(用于高效数值处理),imageio(图像处理), Decorator,tqdm(用于显示进度条)。它在Windows/Mac/Unix环境下应该都是可以工作的,支持python2.7+以及python3。moviepy底层的视频读写依赖于ffmpeg,它会在你安装moviepy的时候自动下载,如果你已经安装了ffmpeg或者想使用特定版本的ffmpeg,你可以从moviepy/config_defaults.py中指定。

   需要注意的是,还有一些可选模块可以安装,比如ImageMagic。这是一个开源的图像处理库,支持对超过200种格式的图像进行编辑处理,详细的用法可以自行参考官网文档。如果你希望向视频中添加文字,就必须安装这个库。它也可以作为生成GIF的后端,但不是必须的。如果你在Windows下安装这个库就比较简单,可以直接使用可执行文件安装;如果是Unix用户,可以选择源码编译安装,官网文档有详细的说明,不再赘述。

  • When to use moviepy?

   如果你属于以下情况,可以考虑使用moviepy:

  1. 你有很多视频需要处理,或者你需要用很复杂的方式处理;
  2. 你希望在web服务器上自动生成videos或者GIF(Django,Flask等);
  3. 你希望能够自动化一些冗长乏味的工作,比如插入标题,图像场景分割,做一些视频结尾的特效,添加副标题等;
  4. 你希望可以使用code实现一些新奇的,其他视频编辑软件都无法做到的特效;
  5. 你希望可以从其他python库(比如matplotlib,mayavi,gizeh,scikit-images等)生成的图像中生成动画。

   如果你是属于下面的这些情况,使用moviepy未必是最好的选择:

  1. 你仅仅需要做逐帧的视频分析(比如面部检测等),这种情况下你也是可以使用moviepy的,但是更建议使用imageio,opencv,simplecv等,因为这些库就是专门干这件事情的。
  2. 你仅仅想对视频进行转化,或者想把一系列的图片转化为视频,你最好直接使用ffmpeg(或者avconv,mencoder等),因为这些工具做这些转化会更快,而且更省内存。

   moviepy的优点和缺点

  1. 优点:
    简单和直观。基础的操作一行代码就可以搞定,对于新手来说非常友好。
    灵活。你对于视频或者音频的每一帧都有着完全的控制权,创作你自己的特效非常容易。
    便携。代码主要依赖numpy和ffmpeg(这两者都是跨平台的),所以moviepy几乎可以在所有的平台以及几乎所有的python版本上运行。
  2. 局限性:
    无法处理视频流。比如无法实时处理来自摄像头的视频流等。当然,本来这就不是moviepy定位要解决的问题。
    *处理大量视频,音频可能会很吃力**。在同时处理大量(>100)的视频,音频,图片的时候可能会有内存问题,这个后面应该会修复。
  • Basic operations of moviepy
  1. Example code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Import everything needed to edit video clips
from moviepy.editor import *
# Load girl.mp4 and select the subclip 00:00:00 – 00:00:02
clip = VideoFileClip(“sources/girl.mp4”).subclip(0,2)
# Reduce the audio volume (volume x 0.8)
clip = clip.volumex(0.8)
# Generate a text clip. You can customize the font, color, etc.
txt_clip = TextClip(“beautiful girl”,fontsize=20,color=‘red’)
# Say that you want it to appear 1.5s at the center of the screen
txt_clip = txt_clip.set_pos(‘top’).set_duration(1.5)
# Overlay the text clip on the first video clip
video = CompositeVideoClip([clip, txt_clip])
# Write the result to a file (many options available !)
video.write_videofile(“sources/girl_edited.mp4”)
  1. How moviepy works?

   moviepy底层使用ffmpeg读取和导出视频以及音频文件。它使用ImageMagic对视频插入文字以及生成GIF(可选)。对于不同的媒体文件的处理是通过python高效的数值计算库numpy来完成的。一些特效处理等使用到了python的图像处理库比如PIL,scikit-images以及科学计算库scipy等。下图1简单说明了moviepy的工作流程:

read more