Python错误消息中的图像变亮和变暗

时间:2014-02-19 15:50:30

标签: python image python-2.7 for-loop graphics

from graphics import*
#Author:
#Purpose: to lighten and darken images
#filename: lab3.py

#Purpose: to get infromation from user and send to function
#inputs:none
#outputs: picture
def main():
    choice=raw_input('press 1 to light,2 to darken')
    value=raw_input('by how much: ')
    name=raw_input('Enter name of image: ')
    temp=Image(Point(0,0),name)
    width= temp.getWidth()
    height= temp.getHeight()
    pic = Image(Point(width/2,height/2),name) 
    win=GraphWin("Original",width, height)
    if choice=='1':
        s=1- float (value)
        grey2(name,choice,pic,s).draw(win)
    if choice=='2':
        s= float(value)
        grey1(name,choice,pic,s).draw(win)
#Purpose:to darken image
#inputs: name,choice,pic,s
#outputs: return newPic
def grey1(name, choice, pic,s):
    newPic=pic.clone()
    for i in range (newPic.getHeight()):#goes through every pixel
        for f in range (newPic.getWidth()): 
            g=newPic.getPixel(f,i)
            newPic.setPixel(f,i,color_rgb(g[0]*s,g[1]*s,g[2]*s))
    return newPic
#purpose: to lighten image
#inputs: name,choice,pic,s
#outputs: return newPIc
def grey2(name, choice, pic,s):
    newPic=pic.clone()
    for i in range (newPic.getHeight()):#goes through every pixel
        for f in range (newPic.getWidth()): 
            g=newPic.getPixel(f,i)
            newPic.setPixel(f,i,color_rgb(s*g[0]+(255*s),g[1]*s+(255*s),g[2]*s+(255*s)))
    return newPic


main()

好吧所以我想要它,所以如果有人输入1并输入介于0和1之间的值,它会使图像变亮,如果有人输入2并输入0到1之间的值,它会使图像变暗(我觉得这个有效吗? )

这是我给出的方程式,为什么这不起作用? 阴影(变暗)     如果像素p1的值=(r1,g1,b1)并且期望的变化量是s(< = 1.0)     然后是新像素的值,p2 =     r2 = r1 * s     g2 = g1 * s     b2 = b1 * s

Tint (lighten) 
If the value of pixel p1 = (r1, g1, b1) and the amount of desired change is t (<= 1.0) 
Then the value of the new pixel, p2 = 
r2 = (t * r1) + (255 * (1.0 - t)) 
g2 = (t * g1) + (255 * (1.0 - t)) 
b2 = (t * b1) + (255 * (1.0 - t)) 

0 个答案:

没有答案