简体   繁体   English

通过变量输入jython / python缩放图像

[英]scale up and down image by variable input jython/python

Pretty new to programming and wasn't able to find any ways of doing this without the use of PIL etc, if I am wrong please point me in the right direction! 编程的新手,如果不使用PIL等就找不到任何方法,如果我错了,请指出正确的方向!

I need to modify the following code so that it will copy the image to TGT at a size chosen by the scale input. 我需要修改以下代码,以便它将图像按比例输入选择的尺寸复制到TGT。 At this stage I have gotten it so that the picture copies onto a canvas which size is determined by the scale input but cannot workout how to make the image itself scale. 在此阶段,我已将其复制到画布上,该画布的大小由比例输入确定,但无法锻炼如何使图像本身按比例缩放。

Any help is greatly appreciated. 任何帮助是极大的赞赏。

def driver():
    pic = makePicture(pickAFile())
    repaint(pic)
    modifiedPic = scale(pic,2.0)
    repaint(modifiedPic)

def scale(srcPic,scale):
    xStart = 0
    yStart = 0
    xEnd = getWidth(srcPic)
    yEnd = getHeight(srcPic)
    width = getWidth(srcPic)
    height = getHeight(srcPic)
    tgt = makeEmptyPicture(width*int(scale),height*int(scale))

    for x in range(0,width):
        for y in range(0,height):
            pxSrc=getPixel(srcPic,x,y)
            pxTgt=getPixel(tgt,x,y)
            setColor(pxTgt,getColor(pxSrc))

    srcWidth = xEnd -xStart
    srcHeight = yEnd - yStart

    tgtWidth = int(srcWidth*scale)
    tgtHeight = int(srcHeight*scale)

    for y in range(0,tgtHeight):
        for x in range(0,tgtWidth):
            pxSrc=getPixel(srcPic,int(x/scale)+xStart,int(y/scale)+yStart)
            pxTgt=getPixel(tgt,int(xStart*scale),int(yStart*scale))
            setColor(pxTgt,getColor(pxSrc))
    return tgt

Try this 尝试这个

def scale(srcPic,scale):
  width = getWidth(srcPic)
  height = getHeight(srcPic)
  tgt = makeEmptyPicture(width*int(scale),height*int(scale))

  # get the pixel from original source
  for x in range(0, width):
    for y in range(0,height):
      pxSrc=getPixel(srcPic,x,y)

      #copy source pixel by scale amount along x and y axis
      for i in range(x * int(scale), x * int(scale) + int(scale)):
        for j in range(y * int(scale), y * int(scale) + int(scale)):
          pxTgt=getPixel(tgt,i,j)
          setColor(pxTgt,getColor(pxSrc))

  return txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM