Install Python image processing library in Apple Mac PiL

Source: Internet
Author: User
Tags image processing library

The development platform is a Mac and needs to use the Python image processing library pil, which records the installation process as well as the problems that arise.
The basic installation process is this, using the command pip for installation

The code is as follows Copy Code

$ pip Install PiL
Downloading/unpacking pil
Could not find no downloads that satisfy the requirement PIL
Some externally hosted files were ignored (use--allow-external pil to allow).
Cleaning up ...
No distributions at all found for PiL
Storing debug log for failure In/users/mylxsw/.pip/pip.log

Prompt to add--allow-external parameter

The code is as follows Copy Code

$ pip2.7 Install PiL--allow-external pil
Downloading/unpacking pil
Could not find no downloads that satisfy the requirement PIL
Some insecure and unverifiable files were ignored (use--allow-unverified pil to allow).
Cleaning up ...
No distributions at all found for PiL
Storing debug log for failure In/users/mylxsw/.pip/pip.log

Again the error, prompted to add--allow-unverified parameters

$ pip2.7 Install PiL--allow-external pil--allow-unverified
...
_imagingft.c:73:10:fatal error: ' freetype/fterrors.h ' File not found

#include <freetype/fterrors.h>

^
1 Error generated.
...

The Freetype/fterrors.h header file is missing, but the system is already installed and the scenario is found from StackOverflow:

The code is as follows Copy Code

$ ln-s/usr/local/include/freetype2/usr/local/include/freetype

Install again

The code is as follows Copy Code

$ pip2.7 Install PiL--allow-external pil--allow-unverified
Downloading/unpacking pil
...
Successfully installed PIL
Cleaning up ...

Let's look at some examples

Import the Image module. You can then load an image file by using the Open method in the image class. If the load file fails, a ioerror is generated, and if no error is returned, the Open function returns an Image object. Now we can examine the contents of the file through some object properties, namely:

The code is as follows Copy Code

>>> Import Image
>>> im = Image.open ("j.jpg")
>>> print Im.format, im.size, Im.mode

4 JPEG (440,%) RGB

Here are three attributes that we know about each one.

Format: Identifies the source format of an image, and if it is not read from a file, it is placed to the None value.

Size: A tuple returned with two elements with a value of pixel width and height.

Mode:rgb (True color image), plus, L (luminance), CMTK (pre-press image).

Now we can use some of the methods defined in the image class to manipulate the image instances that have been read. For example, display the most recently loaded image:

1 >>>im.show ()
2 >>>
Output artwork:

3. Function Overview.

3.1 Reading and Writing Images:open (Infilename), Save (Outfilename)

3.2 Cutting and pasting and merging Images:

Crop (): Extracts an image of a rectangle's size from an image. It receives a tuple of four elements as a parameter, and the elements are (left, upper, right, lower), and the origin of the coordinate system (0, 0) is the upper left-hand corner.

Paste ():

Merge ():

The code is as follows Copy Code

>>> box = (100, 100, 200, 200)
>>> region = im.crop (box)
>>> Region.show ()
>>> region = Region.transpose (image.rotate_180)
>>> Region.show ()
>>> im.paste (Region, Box)
>>> Im.show ()

The effect chart is:

Rotate a Picture:

  code is as follows copy code

def roll ( Image, Delta):
    "Roll an image Sideways"

    xsize, ysize = image.size

    delta = delta% xsize
    If delta = 0:return image

    part 1 = Image.crop ((0, 0, Delta, ysize))
    part2 = Image.crop ((delta, 0, Xsize, ysize))
  & nbsp Image.paste (Part2, (0, 0, Xsize-delta, ysize))
    image.paste (part1, Xsize-delta, 0, Xsize, ysize))

    return image


3.3 Geometrical transformations.
3.3.1 a simple geometric transformation.

The code is as follows Copy Code

>>>out = Im.resize ((128, 128)) #
>>>out = im.rotate #逆时针旋转 45 degree angle.
>>>out = Im.transpose (image.flip_left_right) #左右对换.
>>>out = Im.transpose (image.flip_top_bottom) #上下对换.
>>>out = Im.transpose (image.rotate_90) #旋转 90 degree angle.
>>>out = Im.transpose (image.rotate_180) #旋转 180 degree angle.
>>>out = Im.transpose (image.rotate_270) #旋转 270 degree angle.


The images after each adjustment are:

The images after each adjustment are:
Picture 1:
Picture 2:
Picture 3:
Picture 4:

3.3.2 Color space transform.

Convert (): This function can be used to convert an image to a different color mode.

3.3.3 image enhancement.

Filters: You can use the filter function in the ImageFilter module to use a series of predefined enhancement filters in the module.

The code is as follows Copy Code

>>> Import ImageFilter
>>> IMFilter = Im.filter (imagefilter.detail)
>>> Imfilter.show ()

3.4 Sequence images.

This is the dynamic graph that we often see, the most common suffix is. gif, plus FLI/FLC. The PIL library also provides some basic support for this animated format diagram. When we open this type of image file, PiL automatically loads the first frame of the image. We can move between frames using the Seek and tell methods.

The code is as follows Copy Code

Import Image
Im.seek (1) # Skip to the second frame

Try
While 1:
Im.seek (Im.tell () + 1)
# do something to IM
Except Eoferror:
Pass


3.5 More about the image file read.

The most basic way: Im = Image.open ("filename")

class file read: FP = open ("filename", "RB"); im = Image.open (fp)

String data reads: Import Stringio; im = Image.open (stringio.stringio (buffer))

Read from archive file: Import Tario; fp = Tario.tario ("Image.tar", "image/test/lena.ppm"); im = Image.open (fp)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.