首页 > 解决方案 > Python-3,我的程序没有显示负面图像

问题描述

所以我需要按照教科书中的功能,将图像制作为负片并显示负片图像。我已经尝试更改一些内容以复制以前的功能,看看这是否会改变任何东西,比如输入我想要否定的图像。它编译并运行良好,没有显示任何错误它只是没有显示我的图像的负面影响,所以我不知道是什么问题。

from cImage import *
def negativePixel(oldPixel):
    newRed = 255 - oldPixel.getRed()
    newGreen = 255 - oldPixel.getGreen()
    newBlue = 255 - oldPixel.getBlue()
    newPixel = Pixel(newRed, newGreen, newBlue)
    return newPixel`



def MakeNegative(imageFile):
    oldImage = FileImage(imageFile)
    width = oldImage.getWidth()
    height = oldImage.getHeight()

    myImageWindow = ImageWin("Negative Image", width * 2, height)
    oldImage.draw(myImageWindow)
    newIn = EmptyImage(width, height)

    for row in range(height):
        for col in range(width):
            oldPixel = oldImage.getPixel(col, row)
            newPixel = negativePixel(oldPixel)
            newIn.setPixel(col, row, newPixel)
newIn.setPosition(width + 1, 0)
newIn.draw(myImageWindow)
myImageWindow.exitOnClick()

标签: pythonpython-3.xcimage

解决方案


您的代码没有为我编译或运行;我修复了一些问题 - 缩进,import image(不是cImage),不调用MakeNegative(),参数乱序等。这对我有用。我在 Ubuntu 18.04、Python 3.6.9、cImage-2.0.2、Pillow-7.2.0 上。

from image import *
def negativePixel(oldPixel):
    newRed = 255 - oldPixel.getRed()
    newGreen = 255 - oldPixel.getGreen()
    newBlue = 255 - oldPixel.getBlue()
    newPixel = Pixel(newRed, newGreen, newBlue)
    return newPixel



def MakeNegative(imageFile):
    oldImage = FileImage(imageFile)
    width = oldImage.getWidth()
    height = oldImage.getHeight()

    myImageWindow = ImageWin(width * 2, height, "Negative Image")
    oldImage.draw(myImageWindow)
    newIn = EmptyImage(width, height)

    for row in range(height):
        for col in range(width):
            oldPixel = oldImage.getPixel(col, row)
            newPixel = negativePixel(oldPixel)
            newIn.setPixel(col, row, newPixel)

    newIn.setPosition(width + 1, 0)
    newIn.draw(myImageWindow)
    myImageWindow.exitOnClick()

MakeNegative('Lenna_test_image.png')

在此处输入图像描述


推荐阅读