Python Image.ImageDraw.Draw.point
时间: 2023-10-07 11:08:36 浏览: 397
`ImageDraw.Draw.point()` 是 Python Imaging Library (PIL) 中的一个方法,它可以用于在图像上绘制一个单一像素点。该方法需要传入一个包含两个整数的元组,表示要绘制的像素点的坐标,以及一个可选的颜色参数。如果不指定颜色参数,则默认使用黑色。具体的使用方法可以参考 PIL 的文档。
相关问题
from PIL import Image, ImageDraw # 将图片平移并旋转 gray2 = Image.fromarray(src) width, height = gray2.size # 计算中心点和X轴角度 center = (max_point[0], max_point[1]) angle = np.arctan2(point2[1] - max_point[1], point2[0] - max_point[0]) * 180 / np.pi img_translated = gray2.transform((width, height), Image.AFFINE, (1, 0, center[0] - width/2, 0, 1, center[1] - height/2), resample=Image.BICUBIC) img_translated_rotated = img_translated.rotate(angle, resample=Image.BICUBIC, expand=True) #img_translated_rotated.show() #裁剪 img4 = Image.fromarray(src) width1, height1 = img4.size width2, height2 = img_translated_rotated.size left = (width2 - width1 )/2 top = (height2 - height1 )/2 right = (width2 - width1 )/2 + width1 bottom = (height2 - height1 )/2 + height1 cropped_image = img_translated_rotated.crop((left, top, right, bottom )) import cv2 GRID_STEP = distance/2 # 设置1010栅格(暂时尝试) grid_num_x = 10 grid_num_y = 10 def transform_point_set(points, max_point, distance, angle): # 平移向量 translation_vector = np.array(2: draw.ellipse((point[0]-5, point[1]-5, point[0]+5, point[1]+5), fill="red", outline="red") x, y = max_point[0], max_point[1] #注意这里坐标轴的顺序 # 显示图片 img.show()操作之后点和在图上原本的位置不再重合,请给出调试代码
你可以在代码中添加以下调试代码,来观察点的位置是否正确:
1. 在平移向量计算完成后添加以下代码:
```python
# 绘制平移后的点
for point in points:
draw.ellipse((point[0]-5, point[1]-5, point[0]+5, point[1]+5), fill="blue", outline="blue")
```
2. 在旋转完成后添加以下代码:
```python
# 绘制旋转后的点
for point in points:
rotated_point = tuple(np.dot(np.array([[np.cos(angle), -np.sin(angle)], [np.sin(angle), np.cos(angle)]]), np.array(point) - np.array(max_point)) + np.array(max_point))
draw.ellipse((rotated_point[0]-5, rotated_point[1]-5, rotated_point[0]+5, rotated_point[1]+5), fill="green", outline="green")
```
这样可以在图片上绘制出平移前、平移后、旋转后的点,方便观察点的位置是否正确。
from PIL import Image, ImageDraw # 将图片平移并旋转 gray2 = Image.fromarray(src) width, height = gray2.size # 计算中心点和X轴角度 center = (max_point[0], max_point[1]) angle = np.arctan2(point2[1] - max_point[1], point2[0] - max_point[0]) * 180 / np.pi img_translated = gray2.transform((width, height), Image.AFFINE, (1, 0, center[0] - width/2, 0, 1, center[1] - height/2), resample=Image.BICUBIC) img_translated_rotated = img_translated.rotate(angle, resample=Image.BICUBIC, expand=True) #img_translated_rotated.show() #裁剪 img4 = Image.fromarray(src) width1, height1 = img4.size width2, height2 = img_translated_rotated.size left = (width2 - width1 )/2 top = (height2 - height1 )/2 right = (width2 - width1 )/2 + width1 bottom = (height2 - height1 )/2 + height1 cropped_image = img_translated_rotated.crop((left, top, right, bottom )) import cv2 GRID_STEP = distance/2 # 设置1010栅格(暂时尝试) grid_num_x = 10 grid_num_y = 10 def transform_point_set(points, max_point, distance, angle): # 平移向量 translation_vector = np.array([distance * np.cos(anglenp.pi/180), distance * np.sin(anglenp.pi/180)]) # 旋转矩阵 rotation_matrix = np.array([[np.cos(anglenp.pi/180), -np.sin(anglenp.pi/180)], [np.sin(anglenp.pi/180), np.cos(angle*np.pi/180)]]) # 将点集转换为 numpy 数组 point_array = np.array(points) max_point_array = np.array(max_point) # 对点集进行平移和旋转 point_array = (point_array - max_point_array) @ rotation_matrix + max_point_array + translation_vector # 将 numpy 数组转换为列表 points2 = point_array.tolist() return points2 points2 = transform_point_set(points, max_point, distance, angle) print(points2) #第2.5部分(用作确认检验) from PIL import Image, ImageDraw #裁剪 img4 = Image.fromarray(src) width1, height1 = img4.size width2, height2 = img_translated_rotated.size left = (width2 - width1 )/2 top = (height2 - height1 )/2 right = (width2 - width1 )/2 + width1 bottom = (height2 - height1 )/2 + height1 cropped_image = img_translated_rotated.crop((left, top, right, bottom )) # 导入图片() img_array = np.asarray(cropped_image) img = Image.fromarray(img_array) draw = ImageDraw.Draw(img) for point in point
这是一段 Python 代码,主要是对图片进行平移和旋转,并进行裁剪,然后将一组点坐标进行平移和旋转后输出。具体实现过程中,代码使用了 PIL 库和 OpenCV 库。首先通过 PIL 库中的 Image 类将源图片转换为灰度图像 gray2,然后计算出中心点和旋转角度,通过 transform 和 rotate 方法对图片进行平移和旋转操作。接着通过 crop 方法进行裁剪,将裁剪后的图片转换为 numpy 数组 img_array。最后使用 PIL 库中的 ImageDraw 类和 for 循环对一组点坐标进行平移和旋转,并输出平移和旋转后的点坐标。
阅读全文