Python 3.5 Pillow像素访问

2024-04-25 22:54:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我想写一个程序,比较两个图像(o小的,和大的)来检查小的是否在大的里面。在

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

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格式)的算法。在

我的问题是如何访问图像中的像素以及如何获得图像.宽度以及图像.高度我可以测试我的算法。在

我确实查了官方的枕头教程(http://pillow.readthedocs.io/en/3.1.x/handbook/tutorial.html),但我能找到的只是如何转身和想象,裁剪等等。在


Tags: in图像image程序算法forlendef
2条回答

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

import PIL
from PIL import Image

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

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

^{pr2}$

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

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

您可以根据this找到更多示例

相关问题 更多 >