Manipulação de imagem

../_images/34575689432_3de8e9a348_k_d.jpg

Most image processing and manipulation techniques can be carried outeffectively using two libraries: Python Imaging Library (PIL) and Open SourceComputer Vision (OpenCV).

A brief description of both is given below.

Python Imaging Library

ThePython Imaging Library, or PILfor short, is one of the core libraries for image manipulation in Python. Unfortunately,its development has stagnated, with its last release in 2009.

Luckily for you, there’s an actively-developed fork of PIL calledPillow – it’s easier to install, runs onall major operating systems, and supports Python 3.

Instalação

Before installing Pillow, you’ll have to install Pillow’s prerequisites. Findthe instructions for your platform in thePillow installation instructions.

After that, it’s straightforward:

$ pip install Pillow

Exemplo

fromPILimportImage,ImageFilter#Read imageim=Image.open('image.jpg')#Display imageim.show()#Applying a filter to the imageim_sharp=im.filter(ImageFilter.SHARPEN)#Saving the filtered image to a new fileim_sharp.save('image_sharpened.jpg','JPEG')#Splitting the image into its respective bands, i.e. Red, Green,#and Blue for RGBr,g,b=im_sharp.split()#Viewing EXIF data embedded in imageexif_data=im._getexif()exif_data

There are more examples of the Pillow library in thePillow tutorial.

Open Source Computer Vision

Open Source Computer Vision, more commonly known as OpenCV, is a more advancedimage manipulation and processing software than PIL. It has been implementedin several languages and is widely used.

Instalação

In Python, image processing using OpenCV is implemented using thecv2 andNumPy modules. Theinstallation instructions for OpenCVshould guide you through configuring the project for yourself.

NumPy can be downloaded from the Python Package Index(PyPI):

$ pip install numpy

Exemplo

importcv2#Read Imageimg=cv2.imread('testimg.jpg')#Display Imagecv2.imshow('image',img)cv2.waitKey(0)cv2.destroyAllWindows()#Applying Grayscale filter to imagegray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)#Saving filtered image to new filecv2.imwrite('graytest.jpg',gray)

There are more Python-implemented examples of OpenCV in thiscollection oftutorials.