【问题标题】:Scaling Part of a Picture缩放图片的一部分
【发布时间】:2013-11-02 19:23:56
【问题描述】:

我想放大图片的一部分,在这个例子中是鼻子。

我有一个功能可以选择图片中要放大的部分。

def copyAndPaste(picture):
  height = getHeight(picture)
  width = getWidth(picture)
  newPicture = makeEmptyPicture(width, height)
  for x in range(width):
    for y in range(height):
      pxl = getPixel(picture,x,y)
      if (x>48 and x<59) and (y>58 and y<71):
        newPxl =getPixel(newPicture, #?,#?)
      else:
        newPxl = getPixel(newPicture, x,y)
      color = getColor(pxl)
      setColor(newPxl,color)

  return newPicture

def d():    
  f=pickAFile()
  picture=makePicture(f)        
  newPicture = copyAndPaste(picture)        
  writePictureTo(newPicture, r"D:\FOLDER\0Pic4.jpg")
  explore (newPicture)

我还有放大图片的功能:

def Enlarge(picture):
  height = getHeight(picture)
  width = getWidth(picture)
  newPicture = makeEmptyPicture(width*2, height*2)
  x1=0
  for x in range(0,width):
    y1=0
    for y in range(0,height):
      pxl = getPixel(picture,x,y)
      newPxl = getPixel(newPicture, x1,y1)
      color = getColor(pxl)
      setColor(newPxl,color)

      y1=y1+2
    x1=x1+2

  return newPicture

例如。
来自:

收件人:

我尝试了很多东西,但无法弄清楚如何将两者结合起来放大图片的一部分,而保留图片的其余部分。

这就是生成的图片应该看起来的样子(尽管很荒谬),

我一直在练习小图像,因为程序可能需要很长时间才能执行,所以在这个阶段处理大图像是不可行的,这意味着结果是粗略的,但至少会显示它是否有效。

【问题讨论】:

    标签: python jython image-scaling jes


    【解决方案1】:

    我仍然不确定我是否理解你想要做什么,但我认为它是这样的:你想 复制和粘贴鼻子,而不是 剪切和粘贴,并且您希望粘贴的副本以与第二个示例相同的特殊方式翻倍。

    所以,脸中间会有一个 10x10 的鼻子,右下角有一个 20x20 的褪色鼻子。


    首先,要复制和粘贴,您只需将像素复制到旧位置和新位置,而不是仅复制到新位置:

    def copyAndPaste(picture):
      height = getHeight(picture)
      width = getWidth(picture)
      newPicture = makeEmptyPicture(width+100, height+100)
      for x in range(width):
        for y in range(height):
          pxl = getPixel(picture,x,y)
          color = getColor(pxl)
          if (x>48 and x<59) and (y>58 and y<71):
            newPxl =getPixel(newPicture, x+100,y+100)
            setColor(newPxl,color)
          newPxl = getPixel(newPicture, x,y)
          setColor(newPxl,color)
    

    现在,要放大新粘贴的副本,您只需将偏移量加倍即可。换句话说,49,59 处的第一个像素变为 149,159,但 50,60 处的像素变为 151,161,51,61 处的像素变为 153,163,依此类推。

    所以,你想要的是从 49,59 得到距离,将其加倍,将其加回 49,59,然后将其移动 100,100:

          if (x>48 and x<59) and (y>58 and y<71):
            newPxl =getPixel(newPicture, (x-49)*2+49+100,(y-59)*2+59+100)
            setColor(newPxl,color)
    

    【讨论】:

      【解决方案2】:

      这只是为了记录和娱乐,不是答案......

      但是正如提到的abarnert ("Are you sure they just want you to leave 3 white pixels for every copied pixel, rather than copying the same pixel 4 times?"),这作为缩放算法是相当荒谬的......

      Nearest Neighbor Algorithm 是一种更有趣但更基本的图像缩放方法。

      def EnlargeNearestNeighbor(picture, multiplier):
      
        w1 = getWidth(picture)
        h1 = getHeight(picture)
        w2 = getWidth(picture) * multiplier
        h2 = getHeight(picture) * multiplier
      
        x_ratio = w1/float(w2)
        y_ratio = h1/float(h2)
      
        newPicture = makeEmptyPicture(w2, h2)
      
        for x in range(0, w2):
          for y in range(0, h2):
            newPx = getPixel(newPicture, x, y)
      
            px = floor(x*x_ratio);
            py = floor(y*y_ratio);
      
            oldPx = getPixel(picture, int(px), int(py))
            setColor(newPx, getColor(oldPx))
      
        return newPicture
      
      file = pickAFile()
      picture = makePicture(file)
      pic = EnlargeEagle(picture)
      
      pic2 = EnlargeNearestNeighbor(picture, 3)
      



      ........................... ............................ ....................................


      其他有趣的算法here


      这是Eagle Algorithm 的基本实现(适用于不同颜色数量较少的图像):

      def EnlargeEagle(picture):
      
        w = getWidth(picture)
        h = getHeight(picture)
        w2 = getWidth(picture)*2
        h2 = getHeight(picture)*2
      
        newPicture = makeEmptyPicture(w2, h2)
      
        x2 = 0
        for x in range(1, w-1):
          y2 = 0
          for y in range(1, h-1):
      
            oldPxS = getPixel(picture, x-1, y-1)
            oldPxT = getPixel(picture, x, y-1)
            oldPxU = getPixel(picture, x+1, y-1)
      
            oldPxV = getPixel(picture, x-1, y)
            oldPxC = getPixel(picture, x, y)
            oldPxW = getPixel(picture, x+1, y)
      
            oldPxX = getPixel(picture, x-1, y+1)
            oldPxY = getPixel(picture, x, y+1)
            oldPxZ = getPixel(picture, x+1, y+1)
      
            newPx1 = getPixel(newPicture, x2, y2)
            newPx2 = getPixel(newPicture, x2+1, y2)
            newPx3 = getPixel(newPicture, x2, y2+1)
            newPx4 = getPixel(newPicture, x2+1, y2+1)
      
            # Step 1
            c = getColor(oldPxC)
            setColor(newPx1, c)
            setColor(newPx2, c)
            setColor(newPx3, c)
            setColor(newPx4, c)
      
            # Step 2      
            if (getColor(oldPxV) == getColor(oldPxS)) and (getColor(oldPxS) == getColor(oldPxT)):
               setColor(newPx1, getColor(oldPxS))
      
            if (getColor(oldPxT) == getColor(oldPxU)) and (getColor(oldPxU) == getColor(oldPxW)):
               setColor(newPx2, getColor(oldPxU))
      
            if (getColor(oldPxV) == getColor(oldPxX)) and (getColor(oldPxX) == getColor(oldPxY)):
               setColor(newPx3, getColor(oldPxX))
      
            if (getColor(oldPxW) == getColor(oldPxZ)) and (getColor(oldPxZ) == getColor(oldPxY)):
               setColor(newPx4, getColor(oldPxZ))
      
      
      
        y2 += 2
      x2 += 2
      

      原文:

      最近邻:

      鹰:


      享受吧!

      【讨论】:

        猜你喜欢
        • 2018-09-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-22
        • 2012-12-01
        • 2017-11-20
        • 2016-04-29
        相关资源
        最近更新 更多