Jython图像处理

rog*_*r34 5 python image-manipulation jython jes

该程序应该采用图像的轮廓,然后将其分成不同的象限,然后对其进行着色,例如Andy Warhol Marilyn Monroe图片.

直到"Warholize"功能的每个功能都有效,但是在我不知道该怎么做c=getPixel(picEdge,x,y)warholize功能下它会被卡住.任何帮助将不胜感激.它应该做"让c为位于x,y的picEdge中像素的颜色"

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
Run Code Online (Sandbox Code Playgroud)

Yve*_*omb 10

我将差异和强度函数放入makeOutline函数中.我从makeOutline函数调用了warholize函数.
此外,您需要获取单独象限的颜色,在这里,我刚刚使用了getRed像素来查看它是黑色还是白色(全彩色或不是全彩色).

在这种情况下,我使用了100的阈值来优化效果.我已经在makeOutline函数中设置了阈值,您可以根据需要使用它.

  def main():
  pic= makePicture( pickAFile() )
  show(pic)
  newPic= makeOutline(pic )
  show(newPic)
Run Code Online (Sandbox Code Playgroud)

PIC

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
Run Code Online (Sandbox Code Playgroud)

Warholized pic

您可以根据需要调整要颜色的象限.