0% found this document useful (0 votes)
17 views21 pages

Piloow

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views21 pages

Piloow

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

What is an image?

An image is a visual representation


of something, while a digital image
is a binary representation of visual
data. These images can take the
form of photographs, graphics and
individual video frames. For this
purpose, an image is a picture that
was created or copied and stored in
electronic form.
Image processing
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.

12.6.2.4 create fi lters for image


processing.
What is Image Processing?

Image processing is a method of analyzing and


manipulating digital images.

In the programming world, we can process digital


images using various libraries or tools.

One of the popular tools of Python is Pillow.


Python Imaging Library

• https://www.geeksforgeeks.org/python-pillow-colors- on-an-im
age/

• https://www.javatpoint.com/python-pillow

• https://www.tutorialspoint.com/python_pillow/python_pillow_
quick_guide.htm

• https://www.youtube.com/watch?v=5QR-dG68eNE&ab_channe
l=ClearCode

Documentation

• https://pillow.readthedocs.io/en/stable/
Task 1

pip install pillow

If yes, uninstall using Install using pip


Check if Pillow library
pip uninstall pillow install pillow
is installed on your
command in command in
computer.
command line command line
RGB
RGB is a color model that uses
three base colors:
Red (red),
Green (green), and
Blue (blue).
In the RGB model, each color is
created by mixing these three
base colors in diff erent
proportions.
How does RGB works
Name Function Example
Useful image processing commands
Loads the image into memory.
load Usually called automatically when image = Image.open("example.jpg")
you open an image.

Creates a new image with the


image = Image.new("RGB", (width, height),
create specified dimensions and
color=(R,G,B))
background color.

Returns the dimensions of the


size print (image.size)
image as a tuple (width, height).

Saves the image to a file in the


save image.save("output.png")
specified format.
Name Function Example
Useful image processing commands
Draws a line
between two or
line draw.line((x1, y1, x2, y2), fill=(R,G,B), width=*)
more points in the
image.

Draws a line
draw.ellipse((x1, y1, x2, y2), outline=(R,G,B), fill=(R,G,B),
between two or
ellipse width=*)
more points in the
image.
Draws a line
rectangl between two or draw.rectangle((x1, y1, x2, y2), outline=(R,G,B),
e more points in the fill=(R,G,B), width=*)
image.
Draws a line
between two or draw.polygon([(x1, y1), (x2, y2), (x3, y3), ...],
polygon
more points in the outline=(R,G,B), fill=(R,G,B))
image.
Creating a new image
from PIL import Image

width, height = 500, 500

image = Image.new('RGB', (width, height), (255, 255, 255))

image.save('C:\\**location**\\Desktop\\My_first_image.jpg')
Parameter Description
Assigning the created image to the image
image =
variable.
Image.new A method for creating a new image.
'RGB' Color Model (Red, Green, Blue).
(width, height) The size of the image in pixels.
color = (255, 255, 255) The background color of the image (white).
Create an object to draw on the image
from PIL import Image, ImageDraw

width, height = 500, 500


image = Image.new('RGB', (width, height), (255, 255, 255))

draw = ImageDraw.Draw(image)#by this code snippet we will Create an object to draw on


the image

start = (50, 50)


end = (450, 50)

draw.line([start, end], fill=(0, 0, 0), width=5)

image.save('C:\\**location**\\Desktop\\
My_first_image_with_line.jpg')
Task 1

1. Create this square using Pillow with given parameters.


2. Size 200 x 200, color blue (try other colors using RGB).
3. And save it on desktop.
Task 2

Draw a line using Pillow.


Task 3
Create a beautiful
image using the
Pillow and your
creativity.
Task 4
1. Choose any image;

2. Determine the size of image


using Pillow;

3. Determine the color of the left-


top pixel using Pillow;

4. Crop the image, save it;

5. Also determine the color of the


left-top pixel, compare them;

6. Check the output with


Photoshop or online RGB
converter;
Image filter

To apply a filter to an
image in Python, we need
to go through all the
pixels in the image and
change each of them.
from PIL import Image

width, height = 200, 200


red_image = Image.new('RGB', (width, height), (255, 0, 0))

pixels = ***.load()

for y in range(height):
for x in range(width):
r, g, b = pixels[x, y]
inverted = (255 - r, 255 - g, 255 - b)
pixels[x, y] = inverted

red_image.save('red_to_negative_image.jpg')

red_image.show()
from PIL import Image, ImageDraw

image = Image.open('***.jpg')#if pic in your folder with .py


image = Image.load('C:\\Users\\Дастан\\Desktop\\PyImage\\***.jpg')#if pic export from
comp

width, height = image.size

draw = ImageDraw.Draw(image)

for x in range(width//2):
for y in range(height):
r, g, b = image.getpixel((x, y))
inverted = (255 - r, 255 - g, 255 - b)
draw.point((x, y), inverted)

image.show()
image.save('C:\\location\\Desktop\\PyImage\\
Task 5
• Choose any image.

• Determine the size of image using


Pillow

• Determine the color of the left-top


pixel using Pillow

• Check the output with Photoshop


(if you have)
Task 6

Create this rectangle using Pillow


with given parameters. Size 200
x 200, color blue (try other colors
using RGB). And save it on
desktop.
Capabilities of Pillow library

https://www.youtube.com/watch?v=5QR-dG68eNE&ab_cha
nnel=ClearCode

Watch a video to see other capabilities of library in


practice.

You might also like