Python-image Module Basic Operation __python

Source: Internet
Author: User

Reproduced from: http://www.aichengxu.com/python/39904.htm

Python-image basic image processing operations, there is a need for friends can refer to.

The most common image manipulation library in Python is



Image Library (PIL), function, although it can not be compared with the MATLAB, but still more powerful, nonsense to fill up, write notes.

1. First you need to import the desired image library:

Import Image

2. Read a picture:

Im=image.open ('/home/picture/test.jpg ')

3. Display a Picture:

Im.show ()

4. Save Picture:

Im.save ("Save.gif", "gif") #保存图像为gif格式

5. Create a new picture:

Image.new (Mode,size)

Image.new (Mode,size,color)

Chestnuts: newimg = image.new ("RGBA", (640,480), (0,255,0))
Newimg.save ("Newimg.png", "PNG")

6. Two picture add:

Image.blend (img1,img2,alpha) # here Alpha represents img1 and Img2 scaling parameters

7. Point operation:

Im.point (function) #, which takes a parameter and executes the function on every point in the picture
For example: Out=im.point (lambdai:i*1.5) #对每个点进行50% of the strengthening

8. View image information:
Im.format, Im.size, Im.mode

9. Picture cropping:
box= (100,100,500,500)

#设置要裁剪的区域

Region=im.crop (Box) #此时, Region is a new image object.

10. Image pasting (merging)

Im.paste (Region,box) #粘贴box大小的region到原先的图片对象中.

11. Channel Separation:
R,g,b=im.split () #分割成三个通道, at which point the r,g,b is three image objects respectively.

12. Channel Merging:
Im=image.merge ("RGB", (b,g,r)) #将b, r two channels to flip.

13. Change the size of the image:
Out=img.resize ((128,128)) #resize成128 *128 pixel size

14. Rotate the Image:
Out=img.rotate (#逆时针旋转45度)

There are more convenient:
Region = Region.transpose (image.rotate_180)

15. Image conversion:
out = Im.transpose (image.flip_left_right)

#左右对换.

out = Im.transpose (Image.flip_top_bottom)

#上下对换

16. Image type Conversion:
Im=im.convert ("RGBA")

17. Get the value of a pixel position:
Im.getpixel ((4,4))

18. Write the value of a pixel position:
Img.putpixel ((4,4), (255,0,0))

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.