How do you use the rectangle function in python?

Assuming you want an array of 0 and 1 values, the same shape as x, you can use numpy.where:

In [8]: x = np.linspace(-10, 10, 100)
In [9]: np.where(abs(x)<=0.5, 1, 0)
Out[9]: 
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0])

So your function could be:

from numpy import linspace, sqrt, sin, exp, convolve, abs, where
...
def rect(x):
    return where(abs(x)<=0.5, 1, 0)

How do you use the rectangle function in python?

How do you use the rectangle function in python?

In this article, we show how to draw a rectangle in Python using the OpenCV module.

OpenCV allows a user to create a wide variety of shapes, including rectangles, squares, circles, etc.

Python has a built-in rectangle() function, which allows us to add a rectangle to an image, usually a blank one. We create this blank image with numpy. Then using OpenCV, we add our rectangle shape.

There are many parameters that go into the rectangle() function, which allow us to control various aspects of it.

This includes its location (where it is on the blank image such as to the left, to the right, middle, upper, lower, etc.). It also includes its size, color, border thickness, and whether it's filled or unfilled.

In this article, we're going to create 2 types of rectangles, one where the center is unfilled and one where the center is filled.

How to Create an Unfilled Rectangle

So the first type of rectangle we will create is one where the center is unfilled.

The rectangle we will create is shown below.

How do you use the rectangle function in python?

The code to create this rectangle which is shown above is shown below.

Let's now go over this code.

First, we import all the modules we need which is cv2 (OpenCV), numpy (to create a blank image), and matplotlib (to get grided axes).

Next, we create a blank white image and store this in the variable, whiteblankimage. This is done using numpy.

We now have a blank image (like a canvass) in which we can now draw our rectangle shape.

Next, we use the cv2.rectangle() function to create our rectangle on this white blank image.

The first parameter that we must feed into the cv2.rectangle() function is the image we want to draw it on. We will be using what we created with numpy, whiteblankimage. This provides a perfect clean white background, in which we can place our rectangle shape.

The second parameter we must specify is where the rectangle starts. pt1 represents the upper left border of the rectangle. We want our rectangle, in this example, to begin at the point, (100,200). If you're not sure where to begin, show the blank image with grids first before beginning the rectangle. Then you can see the dimensions of where you want this image to start and end.

The third parameter we must specify is where the rectangle ends. pt2 represents the lower right border of the rectangle. We want our rectangle, in this example, to end at the point, (400,300).

Next, in the fourth parameter, we must specify the color of the rectangle. We specify the color with the RGB format. Since in this example our rectangle is blue, this is represented by (0,0,255).

In the last parameter, the fifth parameter, we specify the thickness of the border of the rectangle. In this case, it's 10, which is relatively thick for a border.

How to Create a Filled Rectangle

Next, we show how to create a rectangle whose interior is filled.

With no dimensions changed from the first image, the only change being that the interior is filled with color, this produces the following image shown below.

How do you use the rectangle function in python?

The only difference between this rectangle and the other unfilled rectangle shown earlier is this rectangle is the thickness parameter of the cv2.rectangle() function.

To make a filled rectangle or any shape in Python, set the thickness attribute to -1.

The full code to this is shown below.

And this is how to draw a rectangle, unfilled or filled, in Python using the OpenCV module.

Related Resources

Python's turtle package can be used to draw various shapes on a canvas. Turtle allows the programmer to move a pen in a specified direction by specifying its heading and distance. Turtle has no built-in primitive for drawing a rectangle or square. However it is easy to create a function to draw a rectangle(or square) in turtle.

The following python program defines a custom function for drawing rectangles using turtle. Note that the centre of the turtle canvas is at 0,0. Hence if you draw just a dot at (0,0) it will be at the centre of the drawing canvas. The function draw_rectangle() requires the co-ordinates of the top left of the rectangle. You also need to specify width, height, thickness and color of the rectangle.

The following example draws a blue rectangle which is 200 pixels in width and 100 pixels in height. We have specified the top left of the rectangle as (-100,50) to position the rectangle at the centre of the canvas. The rectangle drawn has a thickness of 5 pixels.

import turtle
turtle.setup(500,500)
board = turtle.Turtle()

# draws a rectangle given top left position of a rectangle
def draw_rectangle(board,x,y,width,height,size,color):
  board.pencolor(color)
  board.pensize(size)
  board.setheading(0)

  board.up()
  board.goto(x,y)
  board.down()
  # draw top
  board.forward(width)
  # draw right
  board.right(90)
  board.forward(height)
  # draw bottom
  board.right(90)
  board.forward(width)
  # draw left
  board.right(90)
  board.forward(height)
  board.end_fill()


# in turtle, the centre of the canvas is 0,0
# hence we position the rectangle in the center
# note that we need to pass the top left co-ordinates of rectangle
# draws rectangle with 200 pixels in width and 100 pixels in height
draw_rectangle(board,-100,50,200,100,5,"blue")
turtle.done()

The following the sample output from the program,

How do you use the rectangle function in python?

Following python program is a different version which also allows us to specify a fill color for the rectangle. The example is identical to the previous one except that we use a different color to fill the rectangle.

import turtle
turtle.setup(500,500)
board = turtle.Turtle()

# draws a rectangle given top left position of a rectangle
def draw_filled_rectangle(board,x,y,width,height,size,color,fill):
  board.fillcolor(fill)
  board.pencolor(color)
  board.pensize(size)
  board.setheading(0)

  board.begin_fill()
  board.up()
  board.goto(x,y)
  board.down()
  # draw top
  board.forward(width)
  # draw right
  board.right(90)
  board.forward(height)
  # draw bottom
  board.right(90)
  board.forward(width)
  # draw left
  board.right(90)
  board.forward(height)
  board.end_fill()


# in turtle, the centre of the canvas is 0,0
# hence we position the rectangle in the center
# note that we need to pass the top left co-ordinates of rectangle
# draws rectangle with 200 pixels in width and 100 pixels in height
# also specifies the rectangle color and the fill color
draw_filled_rectangle(board,-100,50,200,100,5,"blue","green")
turtle.done()

The following the sample output from the program showing a filled rectangle,

How do you use the rectangle function in python?

The following program uses looping to draw a two dimensional matrix of identical black squares. Note that we are using black as fill color and white as the stroke color for the rectangles.

import turtle
turtle.setup(500,500)
board = turtle.Turtle()

# draws a rectangle given top left position of a rectangle
def draw_filled_rectangle(board,x,y,width,height,size,color,fill):
  board.fillcolor(fill)
  board.pencolor(color)
  board.pensize(size)
  board.setheading(0)

  board.begin_fill()
  board.up()
  board.goto(x,y)
  board.down()
  # draw top
  board.forward(width)
  # draw right
  board.right(90)
  board.forward(height)
  # draw bottom
  board.right(90)
  board.forward(width)
  # draw left
  board.right(90)
  board.forward(height)
  board.end_fill()

# draw a 7x7 matrix of black squares
for x in range(1,8):
  for y in range(1,8):
    draw_filled_rectangle(board,-200+x*20,y*20,20,20,2,"white","black")

turtle.done()

The following the sample output of 7x7 matrix of black squares from the python turtle program,

How do you use the rectangle function in python?

What is the use of rectangle function?

rectangle function is used to draw a rectangle. Coordinates of left top and right bottom corner are required to draw the rectangle.

How do you make a rectangle box in Python?

Rectangle in OpenCV Python : cv2..
image – It is the image on which the rectangle has to be drawn..
pt1 – Vertex of the rectangle at the top left corner..
pt2 – Vertex of the rectangle at the bottom right corner..
color – It is the color of the rectangle line in RGB..
thickness – It is the thickness of the line..

How many arguments are there in drawing a rectangle in Python?

The rect function takes four parameters: x, y, width and height.