在python中获取图像中心部分的问题

2024-04-23 18:59:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要采取一个图像的中心部分,并缩放它保持原来的图像相同的尺寸。 我已经能够缩放图像的一部分,保持图像的相同尺寸,但我不明白,因为这部分不是中心部分。你知道吗

代码如下:

def zoom(source,x):
#@param source:Picture;
#@param x:int; the value to scale up the image;


   target=makeEmptyPicture(getWidth(source),getHeight(source))

   width=getWidth(source)
   height=getHeight(source)

   Xseq = []
   Yseq = []

   colCentrale=x/2    #central column
   rigCentrale=x/2    #central row

   Xseq = []
   for sourceX in range(colCentrale*(width/x),(colCentrale*(width/x))+(width/x)):
       for n in range(0,x):
           Xseq = Xseq+[sourceX]

   Yseq = []
   for sourceY in range(rigCentrale*(height/x),(rigCentrale*(height/x))+(height/x)):
       for m in range(0,x):
           Yseq = Yseq+[sourceY]




   targetX=0
   for sourceX in Xseq:
       targetY=0
       for sourceY in Yseq:
           color = getColor(getPixel(source,sourceX,sourceY))
           setColor(getPixel(target,targetX,targetY),color)
           targetY = targetY + 1
       targetX = targetX + 1

   show(target)

Tags: in图像sourcetargetforrangewidthheight