Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 创建坐标列表_Python_Coordinates - Fatal编程技术网

Python 创建坐标列表

Python 创建坐标列表,python,coordinates,Python,Coordinates,假设我的最大图像大小是 totalpixels = (45, 45) 我想将此图像拆分为 split_image = (10,20) #split the 45x45 pixels into 10x20 pixels 当我将45x45图像分割成10x20时,我可以得到的坐标列表是 [(0,0), (10,0), (20,0), (30,0), (0,20), (10,20), (20,20), (30,20)] 因此,我从(0,0)坐标开始,然后从这里添加 (0,0) (10,0)

假设我的最大图像大小是

totalpixels = (45, 45) 
我想将此图像拆分为

split_image = (10,20) #split the 45x45 pixels into 10x20 pixels
当我将45x45图像分割成10x20时,我可以得到的坐标列表是

[(0,0), (10,0), (20,0), (30,0), (0,20), (10,20), (20,20), (30,20)]
因此,我从(0,0)坐标开始,然后从这里添加

(0,0)
(10,0)    = (10,0)
(10+10,0) = (20,0)
(20+10,0) = (30,0)
(0,0+20)  = (0,20)    
(0+10,20) = (10,20)
(10+10,20)= (20,20)
(20+10,20)= (30,20)
我假设我要做两个for循环

for x in range(total[0]): #Is the range supposed to be this?
    for y in range(total[1]):
         #I'm not sure how to append values according to the split value

我认为您希望使用带有步骤参数的范围:

range(start, stop[, step])
所以你可以这样做:

>>> result = []
>>> totalpixels = (45, 45) 
>>> split_image = (10,20) #split the 45x45 pixels into 10x20 pixels
>>> for x in range(0, totalpixels[0], split_image[0]):
...     for y in range(0, totalpixels[1], split_image[1]):
...             result.append((x,y))
... 
>>> result
[(0, 0), (0, 20), (0, 40), (10, 0), (10, 20), (10, 40), (20, 0), (20, 20), (20, 40), (30, 0), (30, 20), (30, 40), (40, 0), (40, 20), (40, 40)]
>>> 
0 10 20 30 40 45 +-----+-----+-----+-----+--+ | | | | | | | | | | | | 20 +-----+-----+-----+-----+--+ | | | | | | | | | | | | 40 +-----+-----+-----+-----+--+ | | | | | | 45 +-----+-----+-----+-----+--+ 或者干脆

>>> [(x, y) for x in range(0, totalpixels[0], split_image[0]) for y in range(0, totalpixels[1], split_image[1])]
[(0, 0), (0, 20), (0, 40), (10, 0), (10, 20), (10, 40), (20, 0), (20, 20), (20, 40), (30, 0), (30, 20), (30, 40), (40, 0), (40, 20), (40, 40)]
这将生成分割图像的坐标,如下所示:

>>> result = []
>>> totalpixels = (45, 45) 
>>> split_image = (10,20) #split the 45x45 pixels into 10x20 pixels
>>> for x in range(0, totalpixels[0], split_image[0]):
...     for y in range(0, totalpixels[1], split_image[1]):
...             result.append((x,y))
... 
>>> result
[(0, 0), (0, 20), (0, 40), (10, 0), (10, 20), (10, 40), (20, 0), (20, 20), (20, 40), (30, 0), (30, 20), (30, 40), (40, 0), (40, 20), (40, 40)]
>>> 
0 10 20 30 40 45 +-----+-----+-----+-----+--+ | | | | | | | | | | | | 20 +-----+-----+-----+-----+--+ | | | | | | | | | | | | 40 +-----+-----+-----+-----+--+ | | | | | | 45 +-----+-----+-----+-----+--+ 0 10 20 30 40 45 +-----+-----+-----+-----+--+ | | | | | | | | | | | | 20 +-----+-----+-----+-----+--+ | | | | | | | | | | | | 40 +-----+-----+-----+-----+--+ | | | | | | 45 +-----+-----+-----+-----+--+
希望我能提供帮助。

我认为您希望使用带步骤的范围参数:

range(start, stop[, step])
所以你可以这样做:

>>> result = []
>>> totalpixels = (45, 45) 
>>> split_image = (10,20) #split the 45x45 pixels into 10x20 pixels
>>> for x in range(0, totalpixels[0], split_image[0]):
...     for y in range(0, totalpixels[1], split_image[1]):
...             result.append((x,y))
... 
>>> result
[(0, 0), (0, 20), (0, 40), (10, 0), (10, 20), (10, 40), (20, 0), (20, 20), (20, 40), (30, 0), (30, 20), (30, 40), (40, 0), (40, 20), (40, 40)]
>>> 
0 10 20 30 40 45 +-----+-----+-----+-----+--+ | | | | | | | | | | | | 20 +-----+-----+-----+-----+--+ | | | | | | | | | | | | 40 +-----+-----+-----+-----+--+ | | | | | | 45 +-----+-----+-----+-----+--+ 或者干脆

>>> [(x, y) for x in range(0, totalpixels[0], split_image[0]) for y in range(0, totalpixels[1], split_image[1])]
[(0, 0), (0, 20), (0, 40), (10, 0), (10, 20), (10, 40), (20, 0), (20, 20), (20, 40), (30, 0), (30, 20), (30, 40), (40, 0), (40, 20), (40, 40)]
这将生成分割图像的坐标,如下所示:

>>> result = []
>>> totalpixels = (45, 45) 
>>> split_image = (10,20) #split the 45x45 pixels into 10x20 pixels
>>> for x in range(0, totalpixels[0], split_image[0]):
...     for y in range(0, totalpixels[1], split_image[1]):
...             result.append((x,y))
... 
>>> result
[(0, 0), (0, 20), (0, 40), (10, 0), (10, 20), (10, 40), (20, 0), (20, 20), (20, 40), (30, 0), (30, 20), (30, 40), (40, 0), (40, 20), (40, 40)]
>>> 
0 10 20 30 40 45 +-----+-----+-----+-----+--+ | | | | | | | | | | | | 20 +-----+-----+-----+-----+--+ | | | | | | | | | | | | 40 +-----+-----+-----+-----+--+ | | | | | | 45 +-----+-----+-----+-----+--+ 0 10 20 30 40 45 +-----+-----+-----+-----+--+ | | | | | | | | | | | | 20 +-----+-----+-----+-----+--+ | | | | | | | | | | | | 40 +-----+-----+-----+-----+--+ | | | | | | 45 +-----+-----+-----+-----+--+
希望我能帮忙。

for
不需要循环:

>>> [(x, y) for x in range(0, 31, 10) for y in range(0, 21, 20)]
[(0, 0), (0, 20), (10, 0), (10, 20), (20, 0), (20, 20), (30, 0), (30, 20)]
此列表将生成您指定的结果

range的参数是start、stop和step。您为x指定的步长值为10,为y指定的步长值为20。请注意,
range
在“stop”值(其第二个参数)之前停止。因此,如果希望最高的“x”值为30,则需要使“stop”刚好大于该值

您似乎需要大小为split_image的所有像素块的边缘坐标,从(0,0)开始,这些像素块适合大小为totalpixels的图像,但不会超出该图像。计算该值的一般函数为:

def coords(split_image, totalpixels):
    xstop = totalpixels[0] - split_image[0]
    ystop = totalpixels[1] - split_image[1]
    return [(x, y) for x in range(0, xstop, split_image[0])
            for y in range(0, ystop, split_image[1])]
在参数值上运行时,它会生成:

>>> coords( (10, 20), (45, 45) )
[(0, 0), (0, 20), (10, 0), (10, 20), (20, 0), (20, 20), (30, 0), (30, 20)]

for
不需要循环:

>>> [(x, y) for x in range(0, 31, 10) for y in range(0, 21, 20)]
[(0, 0), (0, 20), (10, 0), (10, 20), (20, 0), (20, 20), (30, 0), (30, 20)]
此列表将生成您指定的结果

range的参数是start、stop和step。您为x指定的步长值为10,为y指定的步长值为20。请注意,
range
在“stop”值(其第二个参数)之前停止。因此,如果希望最高的“x”值为30,则需要使“stop”刚好大于该值

您似乎需要大小为split_image的所有像素块的边缘坐标,从(0,0)开始,这些像素块适合大小为totalpixels的图像,但不会超出该图像。计算该值的一般函数为:

def coords(split_image, totalpixels):
    xstop = totalpixels[0] - split_image[0]
    ystop = totalpixels[1] - split_image[1]
    return [(x, y) for x in range(0, xstop, split_image[0])
            for y in range(0, ystop, split_image[1])]
在参数值上运行时,它会生成:

>>> coords( (10, 20), (45, 45) )
[(0, 0), (0, 20), (10, 0), (10, 20), (20, 0), (20, 20), (30, 0), (30, 20)]

“分割值”是什么意思?我不明白这个问题。你能正确地解释这个问题吗?你说的分割值是什么意思?我不明白这个问题。你能正确地解释这个问题吗?John1024的解决方案对于循环来说更像是pythonic而不是显式的。另一种方法是
zip(范围(0,31,10),范围(0,21,20))
,它还返回坐标列表。如果不想同时在内存中存储大量磁贴坐标,请使用xrange和itertools.izip。John1024的解决方案比显式for循环更具pythonic。另一种方法是
zip(范围(0,31,10),范围(0,21,20))
,它还返回坐标列表。如果不希望同时在内存中存储大量磁贴坐标,请使用xrange和itertools.izip。