在Jython / Python中复制图片时,如何在每行中复制X像素

时间:2013-01-15 03:28:29

标签: python image-manipulation jython

我正在Jython中编写一个代码,它会将一张图片的一部分复制到一张空图片中,但是我想让它复制(比方说)每下一行减少10个像素。我认为我没有意义,让我用一个例子来解释。一张100像素×100像素的图片,程序会将第一行(100像素)像素复制到新图片中,但对于第二行像素,我希望它只复制90像素,然后为第三行80像素,等等上。

这里我有一个代码可以复制部分图片,但它会复制一个正方形。那么我应该添加什么来让它做我想做的事情。我猜我应该对for x in range做些什么,但我不知道是什么。

def copyPic():
  file=pickAFile()
  oldPic=makePicture(file)
  newPic=makeEmptyPicture(getWidth(oldPic),getHeight(oldPic))
  xstart=getWidth(oldPic)/2
  ystart=getHeight(oldPic)/2
    for y in range(ystart,getHeight(oldPic)):
       for x in range(xstart, (getWidth(oldPic))):
         oldPixel=getPixel(oldPic,x,y)
         colour=getColor(oldPixel)
         newPixel=getPixel(newPic,x,y)
         setColor(newPixel,colour)
  explore(newPic)

2 个答案:

答案 0 :(得分:1)

你的代码看起来肯定会复制图像的右下角1/4 ...制作一个三角形的部分(如果我理解你的问题,只是一个有角度的部分)需要每次都减少X最大值......如:

def copyPic():
  file=pickAFile()
  oldPic=makePicture(file)
  newPic=makeEmptyPicture(getWidth(oldPic),getHeight(oldPic))
  xstart=getWidth(oldPic)/2
  ystart=getHeight(oldPic)/2

  # The next line gets the max value x can be (width of pic)
  xmax = getWidth(oldPic)

    for y in range(ystart,getHeight(oldPic)):

       # Now loop from the middle (xstart) to the end (xmax)
       for x in range(xstart, xmax):

         oldPixel=getPixel(oldPic,x,y)
         colour=getColor(oldPixel)
         newPixel=getPixel(newPic,x,y)
         setColor(newPixel,colour)

       # Now the x-loop has finished for this line (this value of y)
       # so reduce xmax by 10 (or whatever value) ready for the next line
       xmax = xmax - 10

       # Then you should do some checking in your code to ensure
       # xmax is not < xstart... here is something crude that should work
       if xmax < xstart:
           xmax = xstart

  explore(newPic)

我认为您的代码会采用这样的图像:

+------------+
|   1     2  |
|            |
|   3     4  |
|            |
+------------+

并给出:

+-----+
|  4  |
|     |
+-----+

因为你的X循环长度总是相同的

每次如图所示减少x,你应该得到这样的东西:

+-----+
|  4 /
|  /
+-

这个编码不是很好,我可以重写整个事情......但是如果你只是学习python,那么至少我对你的代码所做的修改应该与你已经拥有的一致,并且应该很容易跟随。我希望它有所帮助,如果你需要,可以随时要求澄清。

干杯

PS:我看到你问了两次 - 你不应该问两次相同的问题,因为它会分开答案,并且让那些试图在以后找到这样的答案的人更难......

答案 1 :(得分:0)

混淆QR扫描器的一种简单方法是用随机单元格替换代码的三个定位正方形。已经完成了 columns: targetName: label: Target Entity choices: "@finance.custom.loadSubcategoriesByCategory('category')" 的形式,这是最小形式。您的函数image3.png将添加三个定位方块和将其与活动单元格分开的白色单元格。然后addSquares(smallPic)将展开生成的图像并保存。