Python:cImage,编写一个接受图像和另一个函数的函数

2024-09-27 23:24:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我应该写以下内容。在

Write the contract and implementation for a procedure generalTransform that takes an image and a procedure that transforms a pixel and returns a new image with all the original pixels transformed by the given procedure.

我们应该写其他函数,以及这项任务的一部分。我在下面列出我使用它们的清单。它们所做的都写在docString中。在

def sepiaTone(p):
    """ which calls for writing a pixel function for sepia toning"""
    red = p.getRed()
    green = p.getGreen()
    blue = p.getBlue()
    newR = (red * .393 + green*.769 + blue * .189)
    if newR > 254:
        newR = 255
    newG = (red * .349 + green*.686 + blue * .168)
    if newG > 254:
        newG = 255
    newB = (red * .272 + green*.534 + blue * .131)
    if newB > 254:
        newB = 255
    newPixel = (newR,newG,newB)
    return newPixel

一。在

^{pr2}$

一。在

def gammaHalf(p):
    """takes a pixel and performs a gamma correction with the gamma value of 0.5""""
    R = p.getRed()
    G = p.getGreen()
    B = p.getBlue()
    Y = 0.00117 * R + 0.00230 * G + 0.000447 * B
    Y2 = Y**.5
    U = -0.000577 * R - 0.00113 * G + 0.00171 * B
    V = 0.00241 * R - 0.00202 * G - 0.00039 * B
    R = 255 * (Y2 + 1.13983 * V)
    G = 255 * (Y2 - 0.39465 * U - 0.58060 * V)
    B = 255 * (Y2 + 2.03211 * U)
    if R > 254:
         R = 255
    if G > 254:
        G = 255
    if B > 254:
        B = 255
    newPixel = (R,G,B)
    return newPixel

一。在

def rotate(image):
    """takes an image and returns an image of the original image rotated 90 degrees clockwise"""
    pic = FileImage(image)
    oldw = pic.getWidth()
    oldh = pic.getHeight()
    myWin = ImageWin("Wild", oldw*2, oldh*2)
    pic.draw(myWin)
    newIm = EmptyImage(oldw,oldw)
    for row in range (oldh):
        for col in range(oldw):
            oldPx = pic.getPixel(col,row)
            newIm.setPixel(oldw-row,col,oldPx)

    newIm.setPosition(oldh+1,0)
    newIm.draw(myWin)

这就是我的问题所在。当我运行这个函数时,我得到一个错误

功能

def generalTransform(image, pro):
    """takes an image and a procedure that transforms a pixel and returns a new image with all the original pixels transformed by the given procedure."""
    pic = FileImage(image)
    h = pic.getHeight()
    w = pic.getWidth()
    myWin = ImageWin('Wild', w, h)
    pic.draw(myWin)
    newIm = EmptyImage (w,h)
    for row in range (h):
        for col in range (w):
            p = pic.    getPixel(col, row)
            pro(p)

    newIm.draw(myWin)

错误信息:我不知道怎么理解它。我哪里出错了?王牌.gif是记录的图像的正确名称。在

generalTransform("wildLogo.gif", rotate)
Traceback (most recent call last):
  File "<pyshell#31>", line 1, in <module>
    generalTransform("wildLogo.gif", rotate)
  File "C:\Python33\image.py", line 92, in generalTransform
    pro(p)
  File "C:\Python33\image.py", line 99, in rotate
    pic = FileImage(image)
  File "C:\Python33\lib\site-packages\cImage.py", line 398, in __init__
    super(FileImage, self).__init__(fname = thefile)
  File "C:\Python33\lib\site-packages\cImage.py", line 241, in __init__
    self.loadImage(fname)
  File "C:\Python33\lib\site-packages\cImage.py", line 270, in loadTkImage
    sufstart = fname.rfind('.')
AttributeError: 'Pixel' object has no attribute 'rfind'

Tags: andtheinimageforiflinefile

热门问题