Python Pillow - ImageDraw.point() 函数



ImageDraw.point() 方法用于绘制点,即图像上指定坐标处的单个像素。Python Pillow 库在其 ImageDraw 模块中提供此 point() 方法,使用 Draw() 类创建的 Draw 对象在图像上绘制点。

语法

以下是该函数的语法:

ImageDraw.point(xy, fill=None)

参数

以下是此函数参数的详细信息:

  • xy - 此参数表示要绘制的点的坐标。它可以指定为 2 元组序列,例如 [(x, y), (x, y), ...],或者数值序列,例如 [x, y, x, y, ...]。每个元组或数值对对应于点的 (x, y) 坐标。

  • fill - 用于点的颜色。此参数指定绘制点的颜色。

示例

示例 1

在此示例中,点绘制在指定的坐标处,填充颜色设置为黄色。

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...')

输出

The points are drawn successfully...

输出图像

points on image

示例 2

以下示例演示了使用循环在黑色背景上以网格图案绘制点。

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...')

输出

The points are drawn successfully...

输出图像

black background

示例 3

此示例演示如何使用 point() 函数在随机位置绘制更多点。

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...')

输出

The points are drawn successfully...
white background
python_pillow_function_reference.htm
广告