python - 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()
解决方案
您的代码没有为我编译或运行;我修复了一些问题 - 缩进,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')
推荐阅读
- c++ - “类不存在默认构造函数”即使我有构造函数?
- python - Python模块中全局变量中使用的补丁方法
- node.js - 如何在 node-api 和 angular 两端加密-解密有效负载/数据?
- python - 读取 json 文件 pandas 发生异常:ValueError arrays must be all be the same length
- android - FirestorePagingOptions 不会从 firestore 加载任何数据
- python - 无效的块标签错误。无法弄清楚我在哪里错过了结束块
- python - 我想建立一个 docker 环境,但是我得到了 ERROR: unsatisfiable constraints: python (missing):
- c# - 如何通过使用日期作为弹性搜索的字符串比较来获取文档?
- javascript - JS 从函数中获取未定义但服务器返回有效的 JSON
- android - 在 Xamarin Android Webview 中将 WebHistory 项目数组转换为字符串数组