Creating images.
RGB color space
RGB
Task 1: Guess the colors
Answers
Task 2 ”RGB"
PIL library
We will work on Google Colab: Installation
Image creation and drawing
Draw a green line from bottom-left to top-right with a width of 3 pixels:
Questions:
Task "Create rectangle"
Task "Triangle"
Task "Random circle"
Task "Half moon"
Task "Fence"
Reflection
331.19K

Programming 1 term 3 lesson (1) (2)

1. Creating images.

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

2. RGB color space

RGB color space or RGB color system, constructs all the colors from the
combination of the Red, Green and Blue colors.
The red, green and blue use 8 bits each, which have integer values from 0 to 255.
This makes 256*256*256=16777216 possible colors.
RGB ≡ Red, Green, Blue
Each pixel in the LED monitor displays colors this way, by combination of red, green
and blue LEDs (light emitting diodes).
When the red pixel is set to 0, the LED is turned off. When the red pixel is set to
255, the LED is turned fully on.
Any value between them sets the LED to partial light emission.

3. RGB

Did you know that every color on the screen can be represented using
an RGB (Red, Green, Blue) scheme. This code consists of three numbers
from 0 to 255 that indicate what saturation of red, green and blue is
used to recreate the color. For example, the RGB code for:
• Red - (255,0,0)
• Green - (0.255.0)
• Blue - (0,0,255)
• Yellow - (255,255,0)
• Orange - (255,165,0)

4.

• The graphic designer and programmer sometimes prefer to use a
different notation based on the hexadecimal RGB code, where each of
the three decimal values is converted to a two-digit hexadecimal
code, resulting in a six-digit (3×2) hexadecimal code. For example:
• Red: #FF0000
• Green: #00FF00
• Blue: #0000FF
• Yellow: #FFFF00
• Orange: #FFA500 (FF - red saturation, A5 - green saturation, 00 - blue
saturation).

5.

• Test your color identification skills, play the color mixing game, follow
the link: https://michaelbach.de/ot/col-match/index.html

6. Task 1: Guess the colors

• (0, 0, 0)
• (0, 255, 0)
• (255, 0, 0)
• (255, 255, 255)
• (0, 0, 255)
• (255, 0, 255)
• (255, 255, 0)
• (197, 83, 255)

7. Answers

• (0, 0, 0) black
• (0, 255, 0) green
• (255, 0, 0) red
• (255, 255, 255) white
• (0, 0, 255) blue
• (255, 0, 255) pink
• (255, 255, 0) yellow
• (197, 83, 255) purple

8. Task 2 ”RGB"

Task 2 ”RGB"
Check if the given RGB color code is valid or not
Given three numbers R, G and B as the color code for Red, Green and Blue respectively as
in the form of RGB color code. The task is to know whether the given color code is valid or
not.
RGB Format: The RGB(Red, Green, Blue) format is used to define the color of an HTML
element by specifying the R, G, B values range between 0 to 255. For example: RGB value
of Red color is (255, 0, 0), Green color is (0, 255, 0), Blue color is (0, 0, 255) etc.
color: rgb(R, G, B);
• Note: The color code is valid when all numbers are in the range [0, 255].
• Examples:
• Input: R=0, G=100, B=255
Output: True
Explanation: Every color is in range [0, 255]
• Input: R=0, G=200, B=355
Output: False
Explanation: Blue is not in range [0, 255]

9. 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

10. We will work on Google Colab: Installation

11. Image creation and drawing

Using the PIL library, you can create new images. The Image.new 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 = Image.new("RGB", (300, 300), (255, 0, 0))
print(im.size)
# image size (300, 300)
im.save("red_square.jpg")
to display image on the screen:
display(im)

12.

13.

• In order 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:

14.

To draw graphic primitives, Draw objects 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

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

16.

17.

18.

19. Questions:

1. Name the library for working with the Draw module.
2. Explain what the RGB color model means.
3. List the graphical functions of the module Draw and describe the
arguments of each.

20. Task "Create rectangle"

Task "Create rectangle"
• Write a program to create a red rectangle in *.jpg with size 400*200
pixels.

21. Task "Triangle"

Task "Triangle"
• Write a program to create a triangle in *.jpg. Put triangle in screen
size 500*500 like in the picture below.

22. Task "Random circle"

Task "Random circle"
• Write a program to create five circles with random places in *.jpg. All
circle should be to output in screen size 400*500 like in the pictureexample below.

23. Task "Half moon"

Task "Half moon"
• Write a program to create a half moon in *.jpg.

24. Task "Fence"

Task "Fence"
• Write a program to create a function to draw a fence in *.jpg like in
the picture below.
• Note: use loop

25. Reflection

• How can you demonstrate your understanding?
• How can you apply what you have learned?
English     Русский Rules