简体   繁体   English

使用numpy的Python数组操作

[英]Python array manipulation using numpy

I am trying to replicate the border of a array: 我正在尝试复制数组的边界:

A=[1,2],[3,4] 

and want the result as 并希望结果为

[1,1,1,2,2,2]
[1,1,1,2,2,2]
[1,1,1,2,2,2]
[3,3,3,4,4,4]
[3,3,3,4,4,4]
[3,3,3,4,4,4]

How do you do it in python? 你如何在python中做呢? I am using 我在用

import numpy
(a,2,reflect') or wrap I am not getting this array

You can use a nested repeat method (example in IPython ): 您可以使用嵌套的repeat方法(例如IPython中的示例):

In [1]: import numpy as np

In [2]: A = np.array([[1,2],[3,4]])

In [3]: A.repeat(3, 1).repeat(3, 0)
Out[3]: 
array([[1, 1, 1, 2, 2, 2],
       [1, 1, 1, 2, 2, 2],
       [1, 1, 1, 2, 2, 2],
       [3, 3, 3, 4, 4, 4],
       [3, 3, 3, 4, 4, 4],
       [3, 3, 3, 4, 4, 4]])

But for image manipulation you migth want to have a look at eg: 但是对于图像处理,您要迁移,例如:

Especially ImageMagick provides a lot of image manipulation tools. 特别是ImageMagick提供了许多图像处理工具。

If you have numpy 1.7 or later, you can use np.pad , with mode='edge' : 如果您使用的是numpy 1.7或更高版本,则可以使用np.pad ,其中mode='edge'

In [8]: a
Out[8]: 
array([[1, 2],
       [3, 4]])

In [9]: np.pad(a, pad_width=2, mode='edge')
Out[9]: 
array([[1, 1, 1, 2, 2, 2],
       [1, 1, 1, 2, 2, 2],
       [1, 1, 1, 2, 2, 2],
       [3, 3, 3, 4, 4, 4],
       [3, 3, 3, 4, 4, 4],
       [3, 3, 3, 4, 4, 4]])

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

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