首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python-3,我的程序没有显示负面的图像。

Python-3,我的程序没有显示负面的图像。
EN

Stack Overflow用户
提问于 2020-08-03 18:12:27
回答 1查看 72关注 0票数 0

因此,我需要按照我的教科书中的功能,使一个图像负,并显示负面的形象。我试着修改了一些东西,以复制之前的函数,看看这是否会改变,比如输入我想要的负面的图像。它编译并运行良好,没有错误,只是没有显示我的负面形象,所以我不知道是什么问题。

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-03 19:25:59

您的代码不是为我编译或运行的;我修复了一些东西--缩进、import image (不是cImage)、不调用MakeNegative()、参数无序等等。我在Ubuntu18.04,Python3.6.9,cImage-2.0.2,枕头-7.2.0上。

代码语言:javascript
复制
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')

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63234770

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档