0

Pythonで画像をポスタライズするコードをいくつか書きました。

ループして、赤、緑、青の値を個別に取得しています。これを簡単にする方法はありますか?

私が持っているハードルは、各 RGB 値を個別に処理する必要があり、RGB の各ピクセルの値を調べて変更する方法を見つけられず、以下よりも面倒になることです。

このバージョンは動作します:

def posterize(pic):
  w=getWidth(pic)
  h=getHeight(pic)
  newPic=makeEmptyPicture(w,h)
  for x in range (0,w):
    for y in range (0,h):
      p=getPixel(pic,x,y)
      newPxl=getPixel(newPic,x,y)
      r=getRed(p)
      g=getGreen(p)
      b=getBlue(p)
      if (r<64):
        newR= 31
      if (r>63 and r<128):
        newR= 95
      if (r>127 and r<192):
        newR= 159
      if (r>191 and r<256):
        newR= 223

      if (g<64):
        newG= 31
      if (g>63 and g<128):
        newG= 95
      if (g>127 and g<192):
        newG= 159
      if (g>191 and g<256):
        newG= 223

      if (b<64):
        newB= 31
      if (b>63 and b<128):
        newB= 95
      if (b>127 and b<192):
        newB= 159
      if (b>191 and b<256):
        newB= 223

      setColor(newPxl, makeColor(newR,newG,newB))

  return (newPic)

これは私が持っている新しいコードですが、RGB の値を変更していません。

def posterize(pic):
  w=getWidth(pic)
  h=getHeight(pic)
  newPic=makeEmptyPicture(w,h)
  for x in range (0,w):
    for y in range (0,h):
      p=getPixel(pic,x,y)
      newPxl=getPixel(newPic,x,y)
      r=getRed(p)
      g=getGreen(p)
      b=getBlue(p)
      Value = [r,g,b]
      for i in Value:
        if (i<64 ):
          i = 31
        if (i>63 and i<128):
          i=  95
        if (i>127 and i<192):
          i =  159
        if (i>191 and i<256):
          i = 223

        setColor(newPxl,makeColor(r,g,b))

  return (newPic)
4

1 に答える 1

0

私の論理が間違っていた場所についてのいくつかのガイダンスがあります。問題を解決しました。

def posterize(pic):
  w=getWidth(pic)
  h=getHeight(pic)
  newPic=makeEmptyPicture(w,h)
  for x in range (0,w):
    for y in range (0,h):
      p=getPixel(pic,x,y)
      newPxl=getPixel(newPic,x,y)
      r=getRed(p)
      g=getGreen(p)
      b=getBlue(p)
      r = int( posterizeColor(r))
      g = int( posterizeColor(g))
      b =int( posterizeColor(b))

      setColor(newPxl, makeColor(r,g,b) )

  return (newPic)

def posterizeColor(i):
  if (i<64 ):
    return 31
  if (i>63 and i<128):
    return  95
  if (i>127 and i<192):
    return 159
  if (i>191 and i<256):
    return 223
于 2013-07-20T14:59:19.280 に答える