![]() To make the line connections look nicer, you can set the joint parameter to “curve”. This time you create an image using Pillow rather than drawing on one of your own. Points = ĭraw.line(points, width=15, fill="green", joint="curve") Create a new file named draw_jointed_line.py and put this code in your file: # draw_jointed_line.py Now you can try creating a series of points and drawing lines that way. When you run this code, the output will look something like this: Lines drawn on an image You use the random module to choose a random color from a list of colors. Then the X position changes in each iteration. The beginning image starts at (0,0) in the first loop. In this case, you use a for loop to draw five lines on the image. Here you open up the image in Pillow and then pass the Image object to ImageDraw.Draw(), which returns an ImageDraw object. Line("madison_county_bridge_2.jpg", "lines.jpg") Now go open up your Python editor and create a new file named draw_line.py and add this code to it: # draw_line.pyĬolors = ["red", "green", "blue", "yellow", You will use this image of one of the Madison County bridges: Madison County Covered Bridge But first, you will need an image to draw on. The width parameter is used to control the width of the lines.īefore you learn how to use joint, you should learn how to draw lines without it. You learned what some of these parameters mean in the previous section. ![]() You can see that it accepts several different parameters. """Draw a line, or a connected sequence of line segments.""" Alternatively, you can pass in a series of XY coordinates and Pillow will draw lines to connect the points.įollowing is the line() method definition: def line(self, xy, fill=None, width=0, joint=None): ![]() In Pillow’s case, a line is drawn by telling Pillow the beginning and ending coordinates to draw the line between. The first type of drawing you will learn about is how to draw lines in Pillow. Now that you know about the common parameters, you can move on and learn how to start drawing! Drawing Lines The default is None, which means no border. Its specification is the same as the one you use for fill. The outline sets the border color of your drawing. L (grayscale): Set a value (0-255) as an integer.RGB: Set each color value (0-255) using (R, G, B) or a color name.The way you set the fill is determined by the mode of the image: The parameter, fill, is used to set the color that will fill the shape. Finally, the point() will draw a point of 1-pixel at each point. The polygon() will draw a polygon where each point is connected. The line() method will draw a straight line, connecting each point. When it comes to drawing a line, polygon, or point, multiple coordinates are specified in either of these ways: ((upper left x, upper left y), (lower right x, lower right y)) or simply ((x1, y1), (x2, y2)).This can be defined in the following two ways: Most of the drawing methods have an xy parameter that sets a rectangular area in which to draw a figure. Rather than explain the same parameters in every section, you will learn about them up-front! xy When you go to use the various drawing methods, you will discover that they have a lot of common parameters that they share. You may also use the color names that are supported by Pillow that you learned about in chapter 2. For “RGB” images, use a 3-tuple containing integer values. For “1”, “L”, and “I” images, use integers. If you want to specify a color, you can use a series of numbers or tuples as you would when using (). If you draw outside of the image bounds, those pixels will be discarded. The upper left corner is still (0,0), for example. When drawing with Pillow, it uses the same coordinate system that you have been using with the rest of Pillow. Specifically, you will learn about the following: You will focus on what comes with Pillow in this article. ![]() If you need more advanced drawing capabilities than what is included in Pillow, you can get a separate package called aggdraw. According to Pillow’s documentation, “you can use this module to create new images, annotate or retouch existing images, and to generate graphics on the fly for web use.” Pillow provides a drawing module called ImageDraw that you can use to create simple 2D graphics on your Image objects. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |