Piloow
Piloow
• 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
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
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
image.save('C:\\**location**\\Desktop\\
My_first_image_with_line.jpg')
Task 1
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
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
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.
https://www.youtube.com/watch?v=5QR-dG68eNE&ab_cha
nnel=ClearCode