Python Basic Image manipulation
use Python for basic image manipulation and processing
Preface:
Most of the programs in the field of early computer vision are written in C + + in different situations. As computer hardware speeds up, researchers are thinking more about the efficiency and ease of writing code as they choose to implement the algorithmic language, rather than putting the algorithm's execution efficiency first in the early days. This leads directly to more and more researchers in recent years have chosen Python to implement the algorithm.
Today in the field of computer vision, more and more researchers are using Python to study, so it is necessary to learn the very easy use of Python in the field of image processing, this blog will show how to use Python's several well-known image processing library to complete the most basic image manipulation and processing.
using PIL for basic image Operations
PiL Introduction:
PIL (Python Imaging library python, image processing libraries) provides common image processing capabilities, as well as a number of useful basic image operations, such as scaling, cropping, rotation, color conversion, and more.
pil reading and storing images:
Using the functions in PIL, we can read data from most image format files and then write to the most common image format files. The most important module in PIL is Image.
In the following program I use PIL to read a JPG image and then save it as a PNG file after grayscale:
# -*- coding: utf-8 -*-fromimport Imageimport os#打开图像得到一个PIL图像对象img = Image.open("./source/test.jpg")#将其转为一张灰度图img = img.convert(‘L‘)#存储该张图片try: img.save("test.png")except IOError: print"cannot convert"
Test.jpg
Test.png
pil generate thumbnails:
# -*- coding: utf-8 -*-fromimport Imageimport os#打开图像得到一个PIL图像对象img = Image.open("./source/test.jpg")#创建最长边为128的缩略图img.thumbnail((128,128))#存储该张图片try: img.save("test.png")except IOError: print"cannot convert"
Test.png
pil adjusting dimensions and rotation:
# -*- coding: utf-8 -*-fromimport Imageimport os#打开图像得到一个PIL图像对象img = Image.open("./source/test.jpg")#修改图片大小,参数为一元组img = img.resize((100,200))#使图片逆时针选择45度img = img.rotate(45)#存储该张图片try: img.save("test.png")except IOError: print"cannot convert"
Test.png
pil copy and paste the image area:
#-*-Coding:utf-8-*-#-*-Coding:utf-8-*-FromPILImport Imageimport OS#打开图像得到一个PIL图像对象img = Image.open ("./source/test.jpg")#从img中裁剪指定区域Region = Img.crop (( -, -, -, -))#使裁剪部分逆时针选择145度Region = Region.rotate (145)#将该区域粘贴至指定区域Img.paste (Region, ( -, -, -, -));#存储该张图片Try: Img.save ("Test.png") except Ioerror:print"Cannot convert"
Test.png
In the use of tuples, the coordinate origin is the upper-left corner and the area is divided as shown
using Matplotlib for basic image Operations
matplotlib Introduction:
Matplotlib is a good class library for working with mathematical operations, charting, or plotting points, lines, and curves on an image, with more powerful drawing capabilities than PIL. Matplotlib can draw a good bar chart, pie chart, scatter plot, etc., but for most computer vision applications, only a few drawing commands are needed. For example, we want to use dots and lines to represent things, such as points of interest, correspondence points, and detected objects.
use Matplotlib to draw images, dots, lines
#-*-Coding:utf-8-*-FromPILImport imagefrom Pylab Import *#打开图像得到一个PIL图像对象img = Image.open ("./source/test.jpg")# Read the image into the arrayim = Array (img)# Drawing ImagesImshow (IM)# some pointsx = [ -, -, -, -]y = [ $, -, $, -]# Draw points with Red Star markersPlot (x, Y,' r* ')# Draw lines connecting the first two pointsPlot (x[:2],y[:2])# Add a caption to display the drawn imageTitle' plotting: ' Test.jpg ') Show ()
The show () command opens the graphical user interface (GUI) first, and then creates a new image window. The graphical user interface loops through the script and pauses until the last image window is closed. In each script, you can call the show () command only once, and usually at the end of the script.
You can also use the axis (' off ') command to not display the axes.
Run results
There are many options for controlling the color and style of the image as you draw.
Such as:
plot(x,y) #默认为蓝色实线plot(x,y,‘r*‘) #红色星状标记plot(x,y,‘go-‘) #带有圆圈标记的绿线plot(x,y,‘ks:‘) #带有正方形标记的黑色虚线
Mark |
Color |
' B ' |
Blue |
' G ' |
Green |
' R ' |
Red |
C |
Cyan |
' m ' |
Magenta |
' Y ' |
Yellow |
K |
Black |
' W ' |
White |
Mark |
Linetype |
‘-‘ |
Solid line |
‘–’ |
Dashed |
‘:’ |
Point line |
Mark |
Shape |
‘.’ |
Point |
' O ' |
Circle |
' s ' |
Square |
‘*’ |
Star-shaped |
+ |
Plus |
' X ' |
Fork Number |
drawing an image outline using matplotlib
Drawing the outline of an image (or contour lines of other two-dimensional functions) is useful in the work. Because drawing outlines requires the same threshold for pixel values for each coordinate [x, y], you first need to grayscale the image and then use contour to get the contour image
# -*- coding: utf-8 -*-from PIL import Imagefrom pylab import *# 读取图像到数组中,并灰度化im = array(Image.open(‘./source/test.jpg‘).convert(‘L‘))#显示时抛弃颜色信息gray()# 显示轮廓图像contour(im, origin=‘image‘)# 在原点的左上角显示axis(‘equal‘)#关闭坐标轴axis(‘off‘)show()
Run results
drawing histograms using Matplotlib
The histogram of the image is used to characterize the distribution of the image's pixel values. A certain number of small intervals (bin) are used to specify the range of pixel values, and each small interval gets the number of pixels that fall within that cell. The histogram of (grayscale) images can be plotted using the Hist () function:
The second parameter of the Hist () function specifies the number of cells between each cell. It is important to note that because Hist () only accepts one-dimensional arrays as input, we must flatten the image before we draw the histogram of the image. The flatten () method converts an arbitrary array into a one-dimensional array according to the row precedence criteria.
# -*- coding: utf-8 -*-fromimport Imagefromimport *# 读取图像到数组中,并灰度化im = array(Image.open(‘./source/test.jpg‘).convert(‘L‘))# 直方图图像hist(im.flatten(),128)# 显示show()
Run results
using matplotlib for interactive labeling
The Ginput () function in the Pylab library enables interactive labeling to mark some points or some training data.
# -*- coding: utf-8 -*-fromimport Imagefromimport *# 读取图像到数组中im = array(Image.open(‘./source/test.jpg‘))# 显示图像imshow(im)print‘Please click 3 points‘#获取点击并将点击坐标保存在[x,y]列表中x = ginput(3)#输出保存的数据print‘you clicked:‘,xshow()
The above script first draws an image, and then waits for the user to click three times in the image area of the drawing window. Ride
The coordinates [x, y] of these clicks are automatically saved in the X list.
Run results
youclicked[(295.22704081632651, 210.72448979591837), (405.43112244897952, 66.846938775510239), (439.1045918367347, 180.11224489795921)]
Conclusion:
This blog introduces some basic Python image operations, in addition to the above PiL and matplotlib, but also often use the numpy directly manipulate the image array to achieve the purpose of manipulating the image, using scipy to complete more complex calculations, I will take my learning process to record, Hope to be helpful to everyone ~
Python Basic Image manipulation