在 python 中分割图像

Sectioning off image in python

我制作了一个程序,可以拍摄我上传的图像并将其分成象限,以不同方式处理每个象限中的颜色。我的问题是我看不到正确的除法。

import cImage as image

img = image.Image("/home/users/groth1/Downloads/selfie.JPG")

newimg = image.EmptyImage(img.getWidth(), img.getHeight())

win = image.ImageWin(title="Pic",width=img.getWidth(),height=img.getHeight())

for row in range(img.getHeight(0,186)):  #Negative Q1
    for col in range(img.getWidth(0,325)):
       p = img.getPixel(col, row)

    newred = 255 - p.getRed()
    newgreen = 255 - p.getGreen()
    newblue = 255 - p.getBlue()

    newpixel = image.Pixel(newred, newgreen, newblue)

    img.setPixel(col, row, newpixel)

当我 运行 这个(使用介绍代码介绍图像和所有其他内容)时,我收到一条错误消息 "TypeError: getHeight() takes 1 positional argument but 3 were given" 这意味着什么?我该如何解决?

该错误消息意味着函数 cImage.Image.getHeightcImage.Image.getHeight 应该在没有任何位置参数的情况下被调用。他们只是 return 图片的高度和宽度。我不明白你试图通过传入 0186325 来实现什么。这里有一个关于如何修复代码的想法:

xQuadrantBoundary = img.getWidth()//2
yQuadrantBoundary = img.getHeight()//2

for row in range(img.getHeight()):  #Negative Q1
    for col in range(img.getWidth()):
        p = img.getPixel(col, row)

        if row < yQuadrantBoundary:
            if col < xQuadrantBoundary:
                # Change this to what you want to happen to the colors in the upper left
                newred = 255 - p.getRed()
                newgreen = 255 - p.getGreen()
                newblue = 255 - p.getBlue()
            elif col >= xQuadrantBoundary:
                # Change this to what you want to happen to the colors in the upper right
                newred = 255 - p.getRed()
                newgreen = 255 - p.getGreen()
                newblue = 255 - p.getBlue()
        elif row >= yQuadrantBoundary:
            if col < xQuadrantBoundary:
                # Change this to what you want to happen to the colors in the lower left
                newred = 255 - p.getRed()
                newgreen = 255 - p.getGreen()
                newblue = 255 - p.getBlue()
            elif col >= xQuadrantBoundary:
                # Change this to what you want to happen to the colors in the lower right
                newred = 255 - p.getRed()
                newgreen = 255 - p.getGreen()
                newblue = 255 - p.getBlue()

        newpixel = image.Pixel(newred, newgreen, newblue)

        img.setPixel(col, row, newpixel)