缩放图片的一部分 [英] Scaling Part of a Picture

查看:115
本文介绍了缩放图片的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

收件人:

我已经尝试了很多事情,但是无法弄清楚如何将两者结合起来以放大图片的一部分,而使其余图片保持完整.

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.

推荐答案

我仍然不确定我是否了解您要执行的操作,但是我认为这是这样的:您要复制并粘贴而不是剪切和粘贴,而是希望粘贴的副本以与第二个示例相同的特殊方式加倍.

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.

因此,脸中央有10x10的鼻子,右下角有20x20的鼻子被洗掉.

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)

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

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.

因此,您想要得到的距离是49,59,将其加倍,再加回到49,59,然后再移动100,100:

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)

这篇关于缩放图片的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆