缩放图片的一部分

Yve*_*omb 1 python jython jes image-scaling

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

我有一个功能来选择我要放大的图片部分.

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

复制粘贴鼻子

我还有一个放大图片的功能:

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

例如.
从:

原始图片

至:

扩大图片

我已经尝试了很多东西,但是无法弄清楚如何将两者结合起来放大图片的一部分,让图片的其余部分保持清晰.

这就是生成的图片应该是什么样的(尽管很荒谬),

鼻子扩大了

我一直在练习上的小图片,因为程序可能需要这么长时间来执行,这是不可行的有较大的图像工作,在这个阶段,这意味着结果是粗略的,但会在它的工作原理,至少说明.

aba*_*ert 5

我仍然不确定我理解你要做什么,但我认为它是这样的:你想要复制并粘贴鼻子,而不是剪切和粘贴,你希望粘贴的副本加倍和你的第二个例子一样奇特.

因此,脸部中间会有一个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)
Run Code Online (Sandbox Code Playgroud)

现在,要放大新粘贴的副本,您只需要将偏移量加倍.换句话说,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)
Run Code Online (Sandbox Code Playgroud)