banner



What Size Mono Line For Fly Line Loop Repair

Cartoon Shapes on Images with Python and Pillow

Pillow provides a drawing module called ImageDraw that you can use to create simple 2nd graphics on your Image objects. According to Pillow's documentation, "you can use this module to create new images, comment or retouch existing images, and to generate graphics on the fly for web use."

If you need more advanced cartoon capabilities than what is included in Pillow, you lot tin can become a split up package chosen aggdraw.

You will focus on what comes with Pillow in this article. Specifically, you will learn most the following:

  • Mutual Parameters
  • Cartoon Lines
  • Drawing Arcs
  • Drawing Chords
  • Drawing Ellipses
  • Drawing Pie Slices
  • Cartoon Polygons
  • Drawing Rectangles

When cartoon with Pillow, it uses the aforementioned coordinate system that you have been using with the rest of Pillow. The upper left corner is all the same (0,0), for case. If you draw exterior of the epitome bounds, those pixels will exist discarded.

If you lot want to specify a color, you can use a series of numbers or tuples as you lot would when using PIL.Epitome.new(). For "i", "L", and "I" images, use integers. For "RGB" images, use a iii-tuple containing integer values. You may likewise utilize the color names that are supported past Pillow that y'all learned almost in chapter two.

Mutual Parameters

When you get to utilise the various drawing methods, you will discover that they have a lot of common parameters that they share. Rather than explain the aforementioned parameters in every department, y'all will larn nigh them up-front end!

xy

Most of the cartoon methods take an xy parameter that sets a rectangular area in which to draw a figure. This tin be defined in the following two means:

  • ((upper left 10, upper left y), (lower right x, lower correct y)) or only ((x1, y1), (x2, y2))
  • A box tuple of (x1, y1, x2, y2)

When it comes to cartoon a line, polygon, or point, multiple coordinates are specified in either of these ways:

  • (x1, y1, x2, y2, x3, y3...)
  • ((x1, y1), (x2, y2), (x3, y3)...)

The line() method will describe a straight line, connecting each point. The polygon() will draw a polygon where each point is connected. Finally, the point() will draw a indicate of one-pixel at each point.

make full

The parameter, make full, is used to prepare the colour that will make full the shape. The mode you set the fill is determined by the manner of the image:

  • RGB: Ready each color value (0-255) using (R, One thousand, B) or a color proper noun
  • L (grayscale): Prepare a value (0-255) equally an integer

The default is None or no fill.

outline

The outline sets the border color of your drawing. Its specification is the same as the one yous use for fill.

The default is None, which means no border.

Now that yous know about the common parameters, you lot can move on and larn how to beginning drawing!

Drawing Lines

The kickoff type of cartoon you will acquire about is how to depict lines in Pillow. All shapes are made upwardly of lines. In Pillow'due south example, a line is drawn by telling Pillow the showtime and catastrophe coordinates to draw the line between. Alternatively, you can pass in a series of XY coordinates and Pillow will draw lines to connect the points.

Following is the line() method definition:

def line(self, xy, fill=None, width=0, articulation=None):     """Draw a line, or a connected sequence of line segments."""

You can run into that it accepts several different parameters. Y'all learned what some of these parameters mean in the previous section. The width parameter is used to control the width of the lines.

Before y'all learn how to utilise joint, y'all should learn how to draw lines without it. But first, you will need an image to draw on. You lot will use this epitome of 1 of the Madison County bridges:

Madison County Covered Bridge

Madison County Covered Bridge

At present go open up your Python editor and create a new file named draw_line.py  and add this lawmaking to it:

# draw_line.py  import random from PIL import Image, ImageDraw   def line(image_path, output_path):     image = Prototype.open(image_path)     draw = ImageDraw.Draw(image)     colors = ["red", "light-green", "blue", "yellow",               "majestic", "orange"]      for i in range(0, 100, 20):         draw.line((i, 0) + epitome.size, width=five,                    fill=random.choice(colors))      image.save(output_path)  if __name__ == "__main__":     line("madison_county_bridge_2.jpg", "lines.jpg")

Hither you lot open the epitome in Pillow and and then laissez passer the Image object to ImageDraw.Draw(), which returns an ImageDraw object. Now you tin can draw lines on your image. In this case, yous use a for loop to draw five lines on the prototype. The showtime image starts at (0,0) in the starting time loop. Then the X position changes in each iteration. The endpoint is the size of the image.

Yous apply the random module to cull a random color from a listing of colors. When you run this code, the output volition look something like this:

Lines drawn on an image

Lines drawn on an image

At present you tin try creating a series of points and cartoon lines that way. Create a new file named draw_jointed_line.py and put this lawmaking in your file:

# draw_jointed_line.py  from PIL import Image, ImageDraw   def line(output_path):     image = Image.new("RGB", (400, 400), "cherry")     points = [(100, 100), (150, 200), (200, l), (400, 400)]     depict = ImageDraw.Describe(paradigm)     depict.line(points, width=15, fill="green", articulation="curve")     image.salve(output_path)  if __name__ == "__main__":     line("jointed_lines.jpg")

This time you lot create an image using Pillow rather than drawing on one of your own. Then you create a list of points. To brand the line connections look nicer, you can set the joint parameter to "bend". If you await at the source code for the line() method, you volition find that "curve" is the just valid value to requite information technology other than None. This may alter in a future version of Pillow.

When you run this lawmaking, your image will wait like this:

Jointed lines

Drawing jointed lines

Now try removing the articulation parameter from your lawmaking and re-run the case. Your output will now look like this:

Lines without joints

Lines without joints

By setting articulation to "bend", the output will be slightly more pleasing to the eye.

Now you're ready to learn about drawing arcs with Pillow!

Drawing Arcs

An arc is a curved line. You lot can draw arcs with Pillow too. Hither is the arc() method specification:

def arc(cocky, xy, showtime, end, make full=None, width=ane):     """Describe an arc."""

An arc() tin can also be made using xy points. The start parameter defines the starting angle, in degrees. The cease parameter tells Pillow what the ending angle is, which is also in degrees. The other two parameters are ones that have already been introduced.

To run across how you might describe an arc, create a new file named draw_arc.py and add this code to it:

# draw_arc.py  from PIL import Prototype, ImageDraw   def arc(output_path):     image = Epitome.new("RGB", (400, 400), "white")     draw = ImageDraw.Draw(image)     draw.arc((25, fifty, 175, 200), starting time=30, terminate=250, fill="green")      draw.arc((100, 150, 275, 300), offset=20, end=100, width=5,               fill="yellow")      image.salvage(output_path)  if __name__ == "__main__":     arc("arc.jpg")

In this code, yous create a new epitome with a white groundwork. And so you create your Draw object. Next, yous create two unlike arcs. The start arc will be filled with green. The second arc will be filled in yellowish, simply its line width volition be 5. When you lot draw an arc, the fill is referring to the arc's line colour. Yous aren't filling the arc itself.

When yous run this lawmaking, your output epitome will look like this:

Drawing arcs

Drawing arcs

Try irresolute some of the parameters and re-running the code to run into how you can change the arcs yourself.

Now let's move on and learn about drawing chords!

Drawing Chords

Pillow also supports the concept of chords. A chord is the same as an arc except that the endpoints are connected with a directly line.

Hither is the method definition of chord():

def chord(self, xy, start, end, fill=None, outline=None, width=1):     """Depict a chord."""

The simply deviation here is that you can also add an outline color. This colour can be specified in whatever of the ways that you can specify a fill color.

Create a new file and name information technology draw_chord.py. So add together this code and so you can see how you make chords yourself:

# draw_chard.py  from PIL import Image, ImageDraw   def chord(output_path):     image = Image.new("RGB", (400, 400), "green")     draw = ImageDraw.Describe(paradigm)     draw.chord((25, l, 175, 200), start=thirty, end=250, fill="red")      describe.chord((100, 150, 275, 300), start=20, end=100, width=five, fill="yellow",                 outline="bluish")      prototype.salve(output_path)  if __name__ == "__main__":     chord("chord.jpg")

This example will depict ii chords on a light-green image. The first chord is filled in with a red color. The second chord is filled in with xanthous only is outlined in bluish. The blue outline has a width of v.

When you run this code, yous will create the following epitome:

Drawing chords

Drawing chords

That looks pretty good. Become alee and play around with this case too. You lot'll soon master chord making with Pillow with a picayune do.

Now let's keep and larn most drawing ellipses!

Drawing Ellipses

An ellipse, or oval, is drawn in Pillow by giving information technology a bounding box (xy). Y'all take seen this several other times in previous sections.

Here is the ellipse() method definition:

def ellipse(self, xy, fill up=None, outline=None, width=1):     """Draw an ellipse."""

The ellipse() lets you fill up it with a colour, add a colored border (outline) and alter the width of that outline.

To see how you lot tin create an ellipse(), make a new file named draw_ellipse.py and add this lawmaking to it:

# draw_ellipse.py  from PIL import Image, ImageDraw   def ellipse(output_path):     paradigm = Prototype.new("RGB", (400, 400), "white")     draw = ImageDraw.Draw(image)     draw.ellipse((25, l, 175, 200), fill="red")      depict.ellipse((100, 150, 275, 300), outline="black", width=5,                  fill="yellow")      image.relieve(output_path)  if __name__ == "__main__":     ellipse("ellipse.jpg")

In this code, you create a dainty white epitome via the new() method. And then you depict a red ellipse on summit of it. Finally, you lot depict a 2d ellipse that is filled with yellow and outlined in black where the outline width is gear up to 5.

When you run this code, the image it creates will await like this:

Drawing ellipses

Cartoon ellipses

Y'all can create ovals and circles using ellipse(). Give information technology a try and see what yous can do with it.

Now let's find out how to create pie slices!

Cartoon Pie Slices

A pie slice is the same as arc()), just also draws straight lines between the endpoints and the center of the bounding box.

Here is how the pieslice() method is divers:

def pieslice(cocky, xy, start, end, fill up=None, outline=None, width=1):     """Describe a pieslice."""

You have used all of these parameters in other drawings. To review, fill adds color to the inside of the pieslice() while outline adds a colored border to the figure.

To first practicing this shape, create a new file named draw_pieslice.py and add together this code to your file:

# draw_pieslice.py  from PIL import Prototype, ImageDraw   def pieslice(output_path):     image = Image.new("RGB", (400, 400), "grey")     draw = ImageDraw.Depict(image)     draw.pieslice((25, 50, 175, 200), get-go=30, terminate=250, fill up="green")      depict.pieslice((100, 150, 275, 300), start=xx, end=100, width=5,                    outline="yellow")      image.save(output_path)  if __name__ == "__main__":     pieslice("pieslice.jpg")

In this code, y'all generate a grey image to draw on. Then you create two pie slices. The first pieslice() is filled in with green. The second one is not filled in, but it does accept a yellow outline. Annotation that each pieslice() has a different starting and ending degree.

When yous run this code, you volition get the following paradigm:

Drawing pie slices

Drawing pie slices

With a footling work, you could create a pie graph using Pillow! You should play effectually with your code a flake and change some values. You will quickly learn how to make some nice pie slices of your own.

Now let'southward find out how to draw polygons with Pillow!

Drawing Polygons

A polygon is a geometric shape that has a number of points (vertices) and an equal number of line segments or sides. A square, triangle, and hexagon are all types of polygons. Pillow lets you create your own polygons. Pillow's documentation defines a polygon like this: The polygon outline consists of straight lines between the given coordinates, plus a direct line betwixt the concluding and the first coordinate.

Here is the code definition of the polygon() method:

def polygon(self, xy, make full=None, outline=None):     """Draw a polygon."""

All of these parameters should be familiar to you now. Go alee and create a new Python file and name it draw_polygon.py. Then add this code:

# draw_polygon.py  from PIL import Image, ImageDraw   def polygon(output_path):     image = Epitome.new("RGB", (400, 400), "grey")     describe = ImageDraw.Draw(image)     draw.polygon(((100, 100), (200, fifty), (125, 25)), fill="green")      draw.polygon(((175, 100), (225, 50), (200, 25)),                   outline="xanthous")      image.save(output_path)  if __name__ == "__main__":     polygon("polygons.jpg")

This code volition create a grey image like the terminal example in the previous section. It will then create a polygon that is filled with the color dark-green. And then it volition create a 2d polygon and outline information technology in xanthous without filling information technology.

In both of the drawings, you are supplying 3 points. That will create ii triangles.

When you run this lawmaking, you lot will get this output:

Drawing polygons

Drawing polygons

Attempt changing the code by adding additional points to one or more of the polygons in the code in a higher place. With a little do, you'll be able to create complex polygons quickly with Pillow.

Drawing Rectangles

The rectangle() method allows you lot to draw a rectangle or square using Pillow. Here is how rectangle() is defined:

def rectangle(self, xy, fill=None, outline=None, width=1):     """Draw a rectangle."""

Yous can laissez passer in ii tuples that define the commencement and ending coordinates to describe the rectangle. Or you can supply the iv coordinates as a box tuple (iv-item tuple). And so y'all can add an outline, fill it with a color, and change the outline'south width.

Create a new file and name it draw_rectangle.py. Then fill it in with this code and then you can start cartoon rectangles:

# draw_rectangle.py  from PIL import Image, ImageDraw   def rectangle(output_path):     image = Epitome.new("RGB", (400, 400), "blueish")     draw = ImageDraw.Describe(paradigm)     describe.rectangle((200, 100, 300, 200), fill up="red")     describe.rectangle((50, fifty, 150, 150), fill="dark-green", outline="yellow",                    width=3)     image.save(output_path)  if __name__ == "__main__":     rectangle("rectangle.jpg")

This code volition create a bluish image that is 400x400 pixels. Then information technology volition draw two rectangles. The offset rectangle volition be filled with red. The second volition be filled with green and outlined with yellow.

When you run this code, you lot will become this image as output:

Drawing rectangles

Drawing rectangles

Aren't those lovely rectangles? You can alter the rectangle's points to create thinner or wider rectangles. You could also modify the outline width that y'all add to the rectangles.

Wrapping Up

You lot can use Pillow to add shapes to your images. This can be helpful for adding outlines to your images, highlighting 1 or more portions of your image, and more.

In this article, you learned well-nigh the following topics:

  • Common Parameters
  • Drawing Lines
  • Drawing Arcs
  • Drawing Chords
  • Drawing Ellipses
  • Drawing Pie Slices
  • Drawing Polygons
  • Drawing Rectangles

Yous can practice a lot with the shapes that are provided by Pillow. You should have these examples and change them to examination them out with your ain photos. Requite it a try and see what you lot can come up up with!

Related Reading

  • Drawing Text on Images with Python and Pillow
  • PySimpleGUI: Cartoon Text on Images with a Python GUI

Pillow: Paradigm Processing with Python

Buy now on Leanpub

Source: https://www.blog.pythonlibrary.org/2021/02/23/drawing-shapes-on-images-with-python-and-pillow/

Posted by: kernrourts.blogspot.com

0 Response to "What Size Mono Line For Fly Line Loop Repair"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel