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

时间:2013-11-13 04:03:00

标签: python

我应该写下面的内容。

  

编写程序generalTransform的合同和实现,该程序采用图像和转换像素的过程,并返回包含由给定过程转换的所有原始像素的新图像。

我们应该编写其他函数以及此作业的一部分。当我使用它时,我列出了下面的那些。他们所做的是写在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

def gammaTwo(p):
    """takes a pixel and performs a gamma correction with the gamma value of 2.0."""
    R = p.getRed()
    G = p.getGreen()
    B = p.getBlue()
    Y = 0.00117 * R + 0.00230 * G + 0.000447 * B
    Y2 = Y**2
    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 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)

错误消息:我不知道该怎么做。我哪里错了? wildLogo.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'

0 个答案:

没有答案