5th Mathematics Module #2 Eureka Math Fraction Review Exit Ticket #1

Python's Pillow which is a fork of the discontinued Python Imaging Library (PIL) is a powerful library that is capable of adding image processing capabilities to your python code. Pillow offers many modules that ease the process of working and modifying images.

In this commodity, we volition accept a look at the ImageDraw module of this library. ImageDraw provides a diversity of methods to, equally its name suggests, draw on images. With the aid of this module, we can draw lines, circles, rectangles and, even write and format text on an paradigm.

Drawing common shapes on image

The image nosotros will be using can be displayed using PIL as follows:

Python

from PIL import Image, ImageDraw

img = Image. open ( 'img_path.png' )

img.show()

Output

The output image is every bit follows

We tin depict shapes and figures on an Image using the Draw method by firstly creating a Draw object.

Drawing a rectangle on the paradigm:

For drawing a rectangle, we utilize the rectangle cartoon method of the ImageDraw module:

Syntax: ImageDraw.rectangle(xy, fill, outline, width)

The parameters for this method are:

  • xy : Corresponds to the tuple of set of points in the upper left corner and lower right coordinates enclosing your shape. The points are passed in a tuple as follows :  (upper left x-coordinate, upper left y-coordinate, lower right 10-coordinate, lower right y-coordinate)
  • fill : Corresponds to the tuple of RGB colour values to fill the shape with.
  • outline : Corresponds to the tuple of RGB colour values assigned for the shape's boundary.
  • width : Integer value corresponding to the thickness of the boundary of the shape.NOTE: The parameters are like across various shape drawing methods.

Python

from PIL import Paradigm, ImageDraw

img = Image. open up ( 'img_path.png' )

describe = ImageDraw.Draw(img)

depict.rectangle(xy = ( l , 50 , 150 , 150 ),

fill = ( 0 , 127 , 0 ),

outline = ( 255 , 255 , 255 ),

width = five )

img.bear witness()

Output:

Output image of the rectangle method

Drawing an ellipse(circle) on the image:

For drawing an ellipse shape, nosotros use the ellipse method of the ImageDraw methods:

Syntax: ImageDraw.ellipse(xy, fill, outline, width)

The co-ordinates you volition provide in xy will human activity as a box in which the circle will be enclosed.

Python

from PIL import Image, ImageDraw

img = Image. open ( 'img_path.png' )

draw = ImageDraw.Describe(img)

depict.circle(xy = ( fifty , 50 , 150 , 150 ),

fill = ( 0 , 127 , 0 ),

outline = ( 255 , 255 , 255 ),

width = five )

img.bear witness()

Output:

Output epitome of the ellipse method

Cartoon a line on the image:

For drawing a line, we use the line method of the ImageDraw methods:

Syntax: lImageDraw.ine(xy, make full, width)

Here, the outline parameter is not considered and the width will determine how long the line should be.

Python

from PIL import Image, ImageDraw

img = Image. open up ( 'img_path.png' )

draw = ImageDraw.Draw(img)

draw.line(xy = ( l , 150 , 150 , fifty ),

make full = ( 0 , 128 , 0 ), width = 5 )

img.bear witness()

Output:

Output epitome of the line method

Drawing a polygon on the prototype:

We tin depict a polygon of the desired shape by using the polygon method of the ImageDraw methods:

Syntax: ImageDraw.polygon(xy, fill, outline)

The xy tuple parameter will comprise co-ordinates based on the number of sides you want for your shape. Here, the width parameter is not valid.

Python

from PIL import Image, ImageDraw

img = Prototype. open ( 'img_path.png' )

draw = ImageDraw.Draw(img)

depict.polygon(xy = ( 50 , 150 , 150 , 50 ),

fill up = ( 0 , 128 , 0 ),

outline = ( 255 , 255 , 255 ))

img.prove()

Output:

Output prototype of the polygon method

Similarly, we can depict another shapes using these methods:

  1. Arc: ImageDraw.arc(xy, starting time, end, fill, width)
  2. Chord (Bow-shape): ImageDraw.chord(xy, outset, end, fill, outline, width)
  3. Signal: ImageDraw.bespeak(xy, fill)
  4. Pieslice: ImageDraw.pieslice(xy, start, finish, fill, outline, width)

The start and cease parameters correspond to the degree of angles in clockwise direction, which volition exist continued with a line.

Writing text on prototype:

Using our Draw object we can also write text on an image. It tin can be done using the Text method:

Syntax: ImageDraw.text(xy, text, fill up, font, ballast, spacing, align, direction, features, language, stroke_width, stroke_fill, embedded_color)

We will besides be using ImageFont from PIL to use the desired font for our text.

Python

from PIL import Paradigm, ImageDraw, ImageFont

img = Image. open ( 'img_path.png' )

fnt = ImageFont.truetype( "Pillow/Tests/fonts/FreeMono.ttf" , 20 )

draw = ImageDraw.Draw(img)

depict.text(xy = ( 25 , 160 ),

text = "Hi, Geeks!" ,

font = fnt,

fill = ( 0 , 127 , 0 ))

img.show()

Output:

Output image of the text method


verdewheive.blogspot.com

Source: https://www.geeksforgeeks.org/python-pillow-imagedraw-module/

0 Response to "5th Mathematics Module #2 Eureka Math Fraction Review Exit Ticket #1"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel