Python. PIL library determine standard colors by RGB code apply graphic primitives to create drawings. use commands of module Image in PIL library (load, create, size, save) to manipulate images; 

PIL library

The PIL (Python image library), or rather its Pillow modification, is used to work with images.

To install the library, you need to run the pip install <Module name> command:

Win + R --> cmd -->

c:\.....>pip install pillow

Image creation and drawing

Using the PIL library, you can create new images. The function takes an RGB palette type, a tuple with the size of the new image, and a color to fill the image with.
The following example creates a 300 x 300 pixels im image object filled with red:

from PIL import Image
im ="RGB", (300, 300), (255, 0, 0))
print(im.size)   # image size (300, 300)"red_square.jpg")

In order for the system to understand where to create the object, you need to save the *.py file. The image will be created in the same directory (folder).

After executing the program folder, we get the following image:

To draw graphic primitives, Draw object from the PIL.ImageDraw module. This object has many tools for creating graphic primitives: lines, ellipses, points, rectangles, arcs, etc.

color = (R, G, B) # R, G, B - integers from 0 to 255

Draw line:

draw.line((x0, y0, x1, y1), fill=color, width=n) 

Draw a green line from bottom-left to top-right with a width of 3 pixels:

from PIL import Image, ImageDraw
new_image ="RGB", (200, 300), (0, 0, 0)) # create image
draw = ImageDraw.Draw(new_image)                    # on image create picture for drawing
draw.line((0, 300, 200, 0), fill=(0, 255, 0), width=3) # draw line'green_line.png', "PNG")             # save image to PNG format

Draw ellipse

draw.ellipse((x0, y0, x1, y1), fill=color, width=n) 

Draw rectangle

draw.rectangle((x0, y0, x1, y1), fill=color, outline=color)

Draw polygon

draw.polygon(((x0, y0), (x1, y1), (x2, y2), (x3, y3), (x4, y4)), color)


1. Name the library for working with the Draw module.

2. Explain what RGB color model means.

3. List graphical functions of the module Draw and describe the arguments of each.





Категория: Programming languages | Добавил: bzfar77 (08.09.2022)
Просмотров: 303 | Теги: Python, size, Load, PIL, Save, create, Draw | Рейтинг: 5.0/1
Всего комментариев: 0