该程序应该拍摄图像的轮廓,然后将其划分为不同的象限,然后对其进行着色,例如Andy Warhol Marilyn Monroe图片。
直到“Warholize”功能的每个功能都可以工作,但是我不确定该怎么做,但它卡在c=getPixel(picEdge,x,y)
函数下的warholize
上。任何帮助将不胜感激。应该执行“让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
最佳答案
我将差值和强度函数放入makeOutline函数中。我从makeOutline函数中调用了warholize函数。
另外,您需要获取单独象限的颜色,在这里,我刚刚使用了getRed像素来查看它是黑色还是白色(是否为全彩色)。
在这种情况下,我使用阈值100来优化效果。我已经在makeOutline函数中设置了阈值,您可以根据需要使用它。
def main():
pic= makePicture( pickAFile() )
show(pic)
newPic= makeOutline(pic )
show(newPic)
def makeOutline(pic ):
picEdge=makeEmptyPicture(getWidth(pic),getHeight(pic))
for x in range (0, getWidth(pic)-1):
for y in range (0, getHeight(pic)-1):
here=getPixel(picEdge,x,y)
down = getPixel(pic,x,y+1)
right = getPixel(pic, x+1,y)
hereL=(getRed(here)+getGreen(here)+getBlue(here))/3
downL=(getRed(down)+getGreen(down)+getBlue(down))/3
rightL=(getRed(right)+getGreen(right)+getBlue(right))/3
if abs (hereL-downL)>100 and abs(hereL-rightL)>100:
setColor(here,black)
if abs (hereL-downL)<=100 or abs(hereL-rightL)<=100:
setColor(here,white)
warholizedPic=warholize(picEdge)
return warholizedPic
def warholize(picEdge):
w= getWidth( picEdge )
h= getHeight( picEdge )
picNew= makeEmptyPicture( w, h )
for x in range(0,w/2):
for y in range (0,h/2):
px=getPixel(picEdge,x,y)
r=getRed(px)
pxNew=getPixel(picNew,x,y)
if r >0:
setColor(pxNew,blue)
else:
setColor(pxNew,yellow)
for x in range (w/2,w):
for y in range (h/2,h):
px=getPixel(picEdge,x,y)
r=getRed(px)
pxNew=getPixel(picNew,x,y)
if r >0:
setColor(pxNew,yellow)
else:
setColor(pxNew,blue)
for x in range(0,w/2):
for y in range (h/2,h):
px=getPixel(picEdge,x,y)
r=getRed(px)
pxNew=getPixel(picNew,x,y)
if r >0:
setColor(pxNew,green)
else:
setColor(pxNew,red)
for x in range (w/2,w):
for y in range (0,h/2):
px=getPixel(picEdge,x,y)
r=getRed(px)
pxNew=getPixel(picNew,x,y)
if r >0:
setColor(pxNew,red)
else:
setColor(pxNew,green)
return picNew
您可以根据需要调整要上色的象限。