Python: Image Processing notes

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/Teddygogogo/article/details/83745638

A library

1, installed PIL or pillow library

from PIL import Image

2, built-in library

import random

# When the built-in library can not be found, you can File "Invalidate Cashes and Restart

Second, the function

1, loading pictures

img = Image.open('D:/Desktop/xxx.tif')

2, convert image formats

img.convert('L')

Mode image #: img.mode ()
# L: Luminance, it represents a grayscale image, 8-bit pixel, black and white
# RGB: 3x8 pixel bit
# RGBA: 4x8 pixel bit true color-plus-clear channel
# CMYK: 4x8 bit

3, to obtain pixel image

Act One:

img.getpixel(col, row)

# Col and when the row is not an integer, the rounding: img.getpixel (1.222, 999.8) = img.getpixel (1, 999)

Act II:

# Read all the pixels of the picture pix [column, row]

Range # pix is ​​pix [0,0] ~ pix [col-1, row-1]

pix = img.load()

# Call the relevant pixels

print(pix[col, row])

4, remainder operation%

I # n by m number of them taken: in the range of 0 ~ m-1

When # n = 0 or an integer multiple of m ans is 0, n m 1-ans is when m-1 =

years = n% m

5, timing

import time

t1=time.clock()

t2=time.clock()

t2_t1=t2-t1

 

Guess you like

Origin blog.csdn.net/Teddygogogo/article/details/83745638