是否可以在python中更改PixelAccess的大小?

时间:2018-07-16 10:26:35

标签: python image crop

我没有使用PIL的crop()函数(只是为了好玩)而裁剪a,但是我找不到缩小PixelAccess大小的方法。

我可以更改所需部分的颜色,但不能仅获得所需部分。

任何人都可以指导我如何从图像中仅获取所需的部分吗?

from PIL import Image

i = Image.open('astro.bmp')
pixels = i.load()
width, height = i.size

all_pixels = []
print(type(pixels))

#image resolution is 1024 x 768
for x in range(width) :
    for y in range(height) :
        ap = pixels[x, y]
        all_pixels.append(ap)

center_x = 40
center_y = 40
crop_width = 180
crop_height = 180


#for cropping
for x in range(crop_width) :
    center_y = 40
    for y in range(crop_height) :
        pixels[center_x, center_y] = all_pixels[768 * center_x + center_y]
        center_y += 1
    center_x += 1

center_x = 40
# to change color of the required region
for x in range(crop_width) :
    center_y = 40
    for y in range(crop_height) :
        pixels[center_x, center_y] = 253
        center_y += 1
    center_x += 1

i.save('out.bmp')

0 个答案:

没有答案