当前位置: 动力学知识库 > 问答 > 编程问答 >

Sectioning off image in python

问题描述:

I have made a program that is taking an image I uploaded and dividing it into quadrants to mess with the colour in each one differently. My problem is that I can't see to get the division right.

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)

when I run this (with intro code introducing image and all that) I get an error that says "TypeError: getHeight() takes 1 positional argument but 3 were given"

What does that mean? how do I fix it?

网友答案:

That error message means that the functions cImage.Image.getHeight and cImage.Image.getHeight are meant to be called without any positional arguments. They just return the height and width of the image. I don't understand what you were trying to achieve by passing in 0, 186, and 325. Here's an idea for how to fix up your code:

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)
分享给朋友:
您可能感兴趣的文章:
随机阅读: