Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

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? I am using

import numpy
(a,2,reflect') or wrap I am not getting this array
share|improve this question
    
When you say "replicate the border", what exactly does that mean? Your example input is nothing but corners, so it doesn't show us what should happen to edges or interior elements. – user2357112 Jun 24 '14 at 1:13

2 Answers 2

You can use a nested repeat method (example in 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 e.g:

Especially ImageMagick provides a lot of image manipulation tools.

share|improve this answer
1  
There's also a repeat method, which will likely be more readable. – user2357112 Jun 23 '14 at 20:12
    
@user2357112 Good point. Updated the answer. – Roland Smith Jun 23 '14 at 20:20

If you have numpy 1.7 or later, you can use np.pad, with 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]])
share|improve this answer
    
This does something completely different from the other answer, but with the current state of the question, it's difficult to tell whether either answer is right, and which is right, if any. – user2357112 Jun 24 '14 at 1:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.