两个图片颜色RGB相同就画箭头连线 python源码
时间: 2023-09-17 14:11:57 浏览: 54
以下是一个简单的 Python 程序,它可以将两张图片中 RGB 值相同的像素用箭头连起来。需要使用 Pillow 库来读取和处理图片。
```python
from PIL import Image, ImageDraw
# 读取两张图片
img1 = Image.open('image1.png')
img2 = Image.open('image2.png')
# 获取图片的宽度和高度
width, height = img1.size
# 创建一个新的图片
new_img = Image.new('RGB', (width, height), color='white')
# 创建画布
draw = ImageDraw.Draw(new_img)
# 遍历每个像素,如果 RGB 相同就画箭头
for x in range(width):
for y in range(height):
pixel1 = img1.getpixel((x, y))
pixel2 = img2.getpixel((x, y))
if pixel1 == pixel2:
draw.arrow((x, y, x+5, y+5), fill=pixel1)
# 保存图片
new_img.save('result.png')
```
这个程序首先读取两张图片,然后创建一个新的图片作为输出结果。然后遍历每个像素,如果两张图片中对应像素的 RGB 值相同,就在新的图片上画一条箭头。最后将结果保存到 result.png 文件中。