Python提取两张图片中像素坐标相同的部分
使用PIL库来实现提取两张图片中像素坐标相同的部分。\n\n以下是一个示例程序:\n\npython\nfrom PIL import Image\n\ndef extract_same_pixels(image1_path, image2_path):\n # 打开并加载图片\n image1 = Image.open(image1_path)\n image2 = Image.open(image2_path)\n\n # 获取图片的尺寸\n width, height = image1.size\n\n # 创建新的图片对象\n new_image = Image.new("RGB", (width, height))\n\n # 遍历每个像素点\n for x in range(width):\n for y in range(height):\n # 获取像素坐标相同的像素值\n pixel1 = image1.getpixel((x, y))\n pixel2 = image2.getpixel((x, y))\n \n # 判断像素值是否相同\n if pixel1 == pixel2:\n new_image.putpixel((x, y), pixel1)\n \n # 保存提取的部分为新图片\n new_image.save("result.png")\n\n# 示例用法\nextract_same_pixels("image1.png", "image2.png")\n\n\n上述程序将提取两张图片中像素坐标相同的部分,并保存为名为"result.png"的新图片。
原文地址: https://www.cveoy.top/t/topic/qiA5 著作权归作者所有。请勿转载和采集!