Scaling Part of a Picture

倖福魔咒の 提交于 2019-12-30 07:41:15

问题


I want to scale up a part of a picture, in this example, a nose.

I have a function to select the part of the picture I want to enlarge.

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)

I also have a function to enlarge a picture:

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

eg.
From:

To:

I have tried many things, but cannot work out how to combine the two to enlarge part of a picture, leaving the rest of the picture in tact.

This is what the resulting picture should look like (as ridiculous as it is),

I have been practicing on small images, as the program can take so long to execute, it is not viable to work with larger images, at this stage, meaning the results are sketchy, but will at least show if it works.


回答1:


I'm still not sure I understand what you're trying to do, but I think it's something like this: You want to copy and paste the nose, instead of cut and paste, and you want the pasted copy to be doubled in the same peculiar way as your second example.

So, there will be a 10x10 nose in the middle of the face, plus a 20x20 washed-out nose to the bottom right.


First, to copy and paste, you just have to copy the pixels to the old and new positions, instead of only to the new position:

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)

Now, to enlarge the newly-pasted copy, you just need to double the offset. In other words, the first pixel at 49,59 goes to 149,159, but the pixel at 50,60 goes to 151,161, and the pixel at 51,61 goes to 153,163, and so on.

So, what you want is to get the distance from 49,59, double it, add it back to 49,59, and then move it by 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:


This is just for the record and for fun, not an answer...

But as mentioned 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?"), which is quite ridiculous as a scaling algorithm...

A lot more interesting yet basic approach to scale an image is the 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)



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

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

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

Other interesting algorithms here.


Here is a basic implementation of the Eagle Algorithm (works fine with images with a low number of different colors):

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

Original:

Nearest Neighbor:

Eagle:


Enjoy!



来源:https://stackoverflow.com/questions/17487427/scaling-part-of-a-picture

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!