小编pat*_*kku的帖子

在Python中将三角形区域从一张图片复制到另一张图片

我正在尝试编写一个python函数,它将从图片上的任何位置复制三角形区域到新的空白图片.我可以将图片中的矩形区域复制到新的空白图片,但我不知道如何复制三角形.这就是我所拥有的,但它只复制一个矩形区域.对不起,如果它看起来很乱或过于复杂,但我刚刚开始如何用python编写.

  def copyTriangle():
     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)
Run Code Online (Sandbox Code Playgroud)

python image-manipulation

3
推荐指数
1
解决办法
1724
查看次数

如何在Python中的列表中添加选定的数字

我有一个数字列表,我想要添加数字,但我不想添加列表中的所有数字,只是添加选定的数字,如前三个.

list = [2, 3, 7, 11, 15, 21]
for i in list:
   sum += i
Run Code Online (Sandbox Code Playgroud)

我的代码显然将列表中的所有数字相加.我已经尝试将for循环更改为范围(0,4),但这只是将数字0,1,2,3加在一起而不是我列表中的数字.那么如何修改我的代码以从我的列表中添加前三个数字.

python list

0
推荐指数
1
解决办法
4548
查看次数

C - 嵌套For循环永不停止

我写了一个简单的嵌套For循环,但由于某种原因它永远不会停止.

#include <stdio.h>

int main (void) {

    int x,y;

    for (x=10;x<100;x+=10) {
        for (y=10;y<100;x+=10)
            printf("x is %d \n",x);
            printf("y is %d \n",y);
    }
return 0;
}
Run Code Online (Sandbox Code Playgroud)

我是c的新手,但是从我看过的例子来看,它应该在x和y达到100时停止.但由于某种原因,它会一直持续下去.

c for-loop nested-loops

-1
推荐指数
1
解决办法
110
查看次数

标签 统计

python ×2

c ×1

for-loop ×1

image-manipulation ×1

list ×1

nested-loops ×1