繁体   English   中英

Python 3.5-枕头-像素配件

[英]Python 3.5 - Pillow - Pixel Acces

我想编写一个比较2张图像(小图像和大图像)的程序,以检查小图像是否在大图像内。

知道可以将图像与数组进行比较后,我编写了以下算法:

big_image = [
    [1,2,3,4,5,6,7,8,9],
    [10,11]
]

small_image = [
    [1,2],
    [10,11]
]
big_result = []
def check(small_image,  big_image):
    for i in range(len(small_image)):
        for j in range(len(small_image[i])):
            if small_image[i][j] ==  big_image[i][j]:
                result = (i,j)
                big_result.append(result)
    return(big_result)

print(check(small_image, big_image))

它按预期打印了[(0, 0), (0, 1), (1, 0), (1, 1)]

之后,我安装了枕头模块以在2个实际图像(.bmp格式)上测试算法。

我的问题是如何访问图像中的像素以及如何获取image.width和image.height,以便可以测试算法。

我确实检查了官方枕头教程( http://pillow.readthedocs.io/en/3.1.x/handbook/tutorial.html ),但是我所能找到的就是如何转身并成像,裁剪等。

对于图像尺寸,您可以执行以下操作:

import PIL
from PIL import Image

img = Image.open('Imagename.jpg').convert('RGB')
width, height = img.size

要访问像素,PIL具有.load(),如下所示:

pixels = img.load()
for x in range(width):
    for y in range(height):
        pixels[x, y] = (0, 100, 200) #an rgb value
img.show()

您可以使用width, height = im.size来查找宽度和高度,如以下示例所示:

from PIL import Image
im = Image.open("lena.bmp")
width, height = im.size
print(width, height)

您可能会根据找到更多示例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM