Python Pillow - ImageDraw.point() Function



The ImageDraw.point() method is used to draw points, which are individual pixels, at specified coordinates on an image. The Python Pillow library provides this point() method within its ImageDraw module to draw points on images using the Draw object created by the Draw() class.

Syntax

Following is the syntax of the function −

ImageDraw.point(xy, fill=None)

Parameters

Here are the details of this function parameters −

  • xy − This parameter represents the coordinates of the points to be drawn. It can be specified as a sequence of either 2-tuples like [(x, y), (x, y), ...] or numeric values like [x, y, x, y, ...]. Each tuple or pair of numeric values corresponds to the (x, y) coordinates of a point.

  • fill − The color to use for the points. This parameter specifies the color of the drawn points.

Examples

Example 1

In this example, points are drawn at specified coordinates, and the fill color is set to yellow.

from PIL import Image, ImageDraw

# Create a new image with a white background
image = Image.new("RGB", (700, 300), "green")
draw = ImageDraw.Draw(image)

# Specify coordinates for points
point_coordinates = [(100, 100), (200, 100), (100, 200), (200, 200),
   (150, 100), (200, 100), (150, 200), (200, 200),
   (250, 100), (300, 100), (250, 200), (300, 200),
   (350, 100), (400, 100), (350, 200), (400, 200),
   (450, 100), (500, 100), (450, 200), (500, 200),
   (550, 100), (600, 100), (550, 200), (600, 200)]

# Set fill color for the points
point_fill_color = "yellow"

# Draw points on the image
draw.point(point_coordinates, fill=point_fill_color)

# Display the image
image.show()

print('The points are drawn successfully...')

Output

The points are drawn successfully...

Output Image

points on image

Example 2

The following example demonstrates drawing points on a black background in a grid pattern using a loop.

from PIL import Image, ImageDraw

# Create a new image with a black background
image_width, image_height = 700, 300
image = Image.new("RGB", (image_width, image_height), "black")
draw = ImageDraw.Draw(image)

# Set fill color for the points
point_fill_color = "yellow"

# Define step size and range for coordinates
step = 20
x_range = range(step, image_width, step)
y_range = range(step, image_height - step, step)

# Draw points on the image
for n in x_range:
   for x in y_range:
      draw.point((n, x), fill=point_fill_color)

# Display the image
image.show()

print('The points are drawn successfully...')

Output

The points are drawn successfully...

Output Image

black background

Example 3

This example demonstrates how to use the point() function to draw more number of points in random positions.

from PIL import Image, ImageDraw
import random

# Create a new image with a white background
image_width, image_height = 700, 300
image = Image.new("RGB", (image_width, image_height), "white")
draw = ImageDraw.Draw(image)

# Set fill color for the points
point_fill_color = "blue"

# Draw 1000 randomly positioned points on the image
num_points = 200
for temp in range(num_points):
   x = random.randint(0, image_width - 1)
   y = random.randint(0, image_height - 1)
   draw.point((x, y), fill=point_fill_color)

# Display the image
image.show()
print('The points are drawn successfully...')

Output

The points are drawn successfully...
white background
python_pillow_function_reference.htm
Advertisements