Color and RGBA values
Computer programs usually represent colors in images as RGBA. Its value represents a set of numbers that specify the values of red, green, blue, and transparency in the color, which are integers from 0 to 255.
Red represents (255,0,0255). In the module pilot, the RGBA value represents the tuple of four integer values. Among these colors, red has the largest value, without green and blue. And the transparency value is the largest, indicating that it is completely opaque.
Green (0255,0255), blue (0,0255255), white is the combination of various colors (255255,255255), black (0,0,0255), green (0128,0255), gray (128255), yellow (255255,0255).
Provide the function imagecolor in the pilot module Getcolor() # takes a string as the first parameter and RGBA as the second parameter
from PIL import ImageColor
ImageColor.getcolor('red','RGBA ') # gets the RGBA value of the color
Coordinates and box tuples
The pixels of the image are specified using x and y coordinates, which respectively specify the horizontal and vertical positions of the pixels in the image. The x coordinates gradually increase from right to left, the y downward component increases, and the origin uses (0,0)
The pilot function needs to create a rectangular primitive parameter, so the pilot needs the original group of four integer coordinates to represent an area in the image. The order is divided into:
- Left: the leftmost x coordinate of the rectangle
- Top: change the leftmost y coordinate of the rectangle
- Right: represents the x coordinate of the rightmost pixel of the rectangle. This integer must be larger than the integer on the left
- Bottom: the y coordinate of a pixel below the bottom edge of the rectangle. This integer must be larger than the integer on the top edge
Image manipulation:
fro PIL import Image catIm=Image.open()#The path of the incoming picture can also be the current path, and the picture name can be directly passed in. catIm.size #The number of pixels that contain the height and width of the picture width,height=catPhoto.size#Get height and width catIm.filename#Represents the file name of the original file catIm.format#Gets the format of the file catIm.save()#Save file im=image.new('RGBA',(100,200),'red')#Create an image object, 100 pixels wide, 200 pixels wide, with a red background #Note that when there is no choice, the default is black. croppedIm=catIm.crop((100,100,100,100))#The incoming tuple rectangle is the area to be cropped, including the pixels of the left column and top row, but excluding the pixels of the right column and bottom row catCopyIm=catIm.copy()#Copy image paste()Method call image image#The first parameter is a 'source' image object, and the second parameter contains x and y coordinate tuples, indicating the position of the upper left corner when the source image object is pasted to the image object. catIm.rotate(90).save('1.png')#Use the rotate method to rotate. The filter parameter is an integer, which is the degree of rotation, the second parameter is an optional parameter, the expand keyword parameter, and turn will enlarge the image catIm.rotate(6,expand=True).save('1.png') catIm.transpose(Image,FILE_TOPBOTTOM).save('2.png')#Like rotate, a new image object is created. The transfer () method will get the 'mirror flip' of the image of the function #The color of a single pixel can be obtained and set by getpixel and putpixel methods. They both receive a tuple representing the x and y coordinates of the pixel. The putpixel method accepts a tuple as the color of the pixel.
Project instance
Add logo
- Load logo image
- Loop through all in the work target png and jpg file
- Check whether the picture is wider than or higher than 300 pixels
- If yes, reduce the larger of the width or height to 300 pixels and scale down the other dimension
- Add the head of the logo on the corner
- Save the changed image to another folder
code: - Open a picture file as an image object
- Loop through OS Listdir ('.') returns a string
- Get the height and width of the image through the size attribute
- Calculate the height and width of the adjusted image
- Call the resize() method to resize the image
- Call the paste() method to paste the logo
- Call the save() method to save the changes, using the original file name.
import PIL import Image file_size=300 logo_filename='catlogo.png' logoIm=Image.open(file_size) logoWidth,logoHeight=logoIm.size for filename in os.listdir('.') if not (filename.endwith(.png) or filename.endwith(.jpg)) or filename == logo_filename: im=Image.open(filename) width,height=im.size if width > file_size and height > file_size if width > height: height=int((file_size/width)*height) width=file_size else: width=int((file_size/height)*width) height=file_size print('resizing %s...'%(filename)) im=im.resize((width,height)) print('adding logo to %s...' %(filename)) im.paste(logoIm,(width-logoWith,height-logoHeight),logoIm) im.save(os.path.join('withLogo',filenname))
Painting on an image
form PIL import Image ImageDrow im=Image.new('RABA',(200,200),'white')#Create a new image draw=ImageDrow(im)
Point: the point(xy,fill) # method draws a single pixel. The xy parameter represents the list of points to be drawn, which represents the list of coordinate tuples of X and y, such as [(x, y), (x, y)...], The fill parameter is the color of the point
Line: the line(xy,fill,width) # method draws a line or a line of some columns. xy is either a tuple list or an integer list [x1,y1,x2,y2,...], Either a tuple list [(x, y), (x1, Y1)], the fill parameter is the RGBA tuple or color name, and the width parameter is the width of the line. If specified, the default parameter 1 is used
Square: the rectangle(xy,fill,outline) method draws a rectangle. The xy parameter is a tuple rectangle in the form of: (left, top, right, bottom). Fill is an optional parameter of color, and the outline parameter is the color of the rectangular outline. The optional outline parameter is the color of the rectangular outline
Ellipse: the ellipse(xy,fill,outline) method draws an ellipse. If the height and width of the ellipse are the same, the method will draw a circle. The xy parameter is a rectangular tuple (left,top,right,bottom), indicating the rectangle exactly containing the ellipse.
Polygon: polygon(xy,fill,outline) method to draw arbitrary polygon. The xy parameter is a tuple list or an integer list, indicating the connection point of the polygon. Fill is also the internal color, and outline is the outline color of the polygon.
Draw instance:
form PIL import ImagrDrow,Image im=Imagr.new('RGDA',(200,200),'white') draw=ImageDraw.Draw(im) draw.line[(0,0),(199,0),(199,199),(0,199),(0.0),fill='black'] draw.rectangle((20,30,60,60),fill='bule') draw.ellipse((120,30,160,60),fill='red') draw.polygon(((57,87),(79,62),(94,62),(120,90),(103,113)),fill='brown') for i in range(100,200,10) draw.line([(i,0),(200,i-100)],fill='green') im.save('drawing.png')
Draw text
The imageDraw object also has a text method for drawing text on an image. The text() method contains four parameters, xy, text, fill, and font
- The xy parameter is a tuple of two integers, specifying the upper left corner of the text area
- The test parameter is the text string you want to write
- The optional parameter fill is the color of the text
- The optional parameter front is an imagefront object, which is used to set the color, font and size of the text.
Project example:
from PIL import Image,ImageDraw,ImageFont import os im= Image.new('RGBA',(200,200),'white') draw=ImageDraw.Draw(im) draw.txt((20,150),'hello',fill='purple') fontsFolder='FONT_FOLDER'#e.g.'/Library/Fonts' arialFont=ImageFont.truetype(os.path.join(fontsFolder,'arial.ttf'),32) draw.text((100,150),'Howdy',fill='gray',font=arialFont) im.save('text.png')