【发布时间】:2011-01-21 04:21:09
【问题描述】:
这个程序应该把图像的轮廓,然后分成不同的象限,然后给它上色,比如安迪·沃霍尔玛丽莲·梦露的照片。
“Warholize”功能之前的每个功能都有效,但它卡在warholize 功能下的c=getPixel(picEdge,x,y) 上,我不知道该怎么做。任何帮助将不胜感激。它应该做“让 c 成为 picEdge 中 x,y 位置的像素的颜色”
def main():
pic= makePicture( pickAFile() )
show( pic )
threshold= 10
edgePic= makeOutline( pic, threshold )
warholize(pic)
show(warholize(pic))
def difference( a, b ):
if a > b :
return a - b
else:
return b - a
def intensity( px ) :
r= getRed( px )
g= getBlue( px )
b= getGreen( px )
avg= ( r + g + b ) / 3
return avg
def makeOutline( pic, threshold ):
w= getWidth( pic )
h= getHeight( pic )
edgePic= makeEmptyPicture( w, h )
for x in range(2,w-1) :
for y in range(2,h-1):
px= getPixel( pic, x, y )
pxLeft= getPixel( pic, x-1, y )
pxUp= getPixel( pic, x, y-1 )
leftDiff= difference( intensity(pxLeft), intensity(px) )
upDiff= difference( intensity(pxUp), intensity(px) )
if leftDiff > threshold or upDiff > threshold :
setColor( getPixel(edgePic,x,y), black )
def warholize(pic):
threshold=10
picEdge=makeOutline(pic,threshold)
w= getWidth( pic )
h= getHeight( pic )
picNew= makeEmptyPicture( w, h )
for x in range(0,w,2):
for y in range (0,h,2):
c=getPixel(picEdge,x,y)
px=getPixel(picNew,x/2,y/2)
if c is black:
setColor(px,blue)
else:
setColor(px,yellow)
return picNew
【问题讨论】:
-
如果每个函数都符合 warholize 函数,那么它是如何卡在 makeOutline 函数上的呢?你有错误吗?这些是什么?顺便说一句,据我所知,您正在使用未定义的阈值在 warholize 中调用 makeOutline。此外,在 makeOutline 中,在我看来,您的范围应该是 range(2,w) 和 range(2,h)。
-
我根据您的建议修复了阈值并且它有效但现在它卡在 c=getPixel(picEdge,x,y) 这应该抓取该像素的颜色但我不是确定怎么做。
-
您确定要在
main()中呼叫warholize()两次吗?
标签: python jython image-manipulation jes