6

这个程序应该把图像的轮廓,然后分成不同的象限,然后给它上色,比如安迪·沃霍尔玛丽莲·梦露的照片。

“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
4

1 回答 1

11

我将差异和强度函数放入 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

沃霍尔化图片

您可以随意调整要着色的象限。

于 2013-07-25T17:08:03.613 回答