[Basics of Python entry] Image processing

Image Processing

Color and pixel

colour

  The three primary colors of art are red, yellow and blue; the three primary colors of color and light are red, green and blue, so a color is usually expressed as an RGB value or RGBA value (A represents the Alpha channel, which determines the pixels passing through the image, which is the transparency).

name RGBA value name RGBA value
White (255, 255, 255, 255) Red (255, 0, 0, 255)
Green (0, 255, 0, 255) Blue (0, 0, 255, 255)
Gray (128, 128, 128, 255) Yellow (255, 255, 0, 255)
Black (0, 0, 0, 255) Purple (128, 0, 128, 255)

Pixel

  For an image represented by a sequence of numbers, the smallest unit is the small squares of a single color on the image. These small squares have clear positions and assigned color values, and the color and position of these small squares determine They are an indivisible unit, that is, pixels (pixels). Every image contains a certain amount of pixels, which determine the size of the image on the screen.

Process images with Pillow

Crop image

from PIL import Image
image = Image.open('algorithm.jpg')
rect = 80,20,310,360
image.crop(rect).show()

Generate thumbnail

from PIL import Image
image = Image.open('algorithm.jpg')
size=128,128
image.thumbnail(size)
image.show()

Scale and paste images

from PIL import Image
image1 = Image.open('algorithm.jpg')
image2 = Image.open('road.jpg')
rect=80,20,310,360
image2_head=image2.crop(rect)
width,height=image2.size
image1.paste(image2_head.resize((int(width/1.5),int(height/1.5))),(172,40))
image1.show()

Rotate and flip

from PIL import Image
image = Image.open('algorithm.jpg')
image.rotate(180).show()
image.transpose(Image.FLIP_LEFT_RIGHT).show()

Operating pixels

from PIL import Image
image = Image.open('algorithm.jpg')
for x in range(80,310):
    for y in range(20,360):
        image.putpixel((x,y),(128,128,128))
image.show()

Filter effect

from PIL import Image,ImageFilter
image = Image.open('algorithm.jpg')
image.filter(ImageFilter.CONTOUR).show()

Guess you like

Origin blog.csdn.net/qq_36477513/article/details/111316216