Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: most efficient way to mirror an image along its vertical axis

I have a lot of images that I will need to flip (on the fly) and so am looking for the fastest way possible to do this using Python.

What is the most efficient way to do this?

I have image files on disk and have tried to ways, shown in my own answer below, but these start with Numpy arrays and so may not be optimal. Are there better ways?

like image 527
n1k31t4 Avatar asked Dec 14 '22 20:12

n1k31t4


1 Answers

You can simply use slicing to flip the second last axis to get equivalent flipped view into the input array of images, as such won't be creating any new data in memory and hence an efficient one, like so -

images[...,::-1,:]

If you still need to make a copy, use .copy there, which would still be more efficient than np.fliplr and noticeable with small/decent sized arrays.

Runtime test -

It seems NumPy is the winner, so I will test it out against that one.

In [64]: images = np.random.randint(0,255,(3,200,400,3))

In [65]: out1 = np.array([np.fliplr(images[i]) for i in range(3)])

In [66]: out2 = images[...,::-1,:]

In [67]: np.allclose(out1, out2)
Out[67]: True

In [68]: %timeit np.array([np.fliplr(images[i]) for i in range(3)])
1000 loops, best of 3: 1.38 ms per loop

In [69]: %timeit images[...,::-1,:]
1000000 loops, best of 3: 259 ns per loop # virtually free

If you need copy -

In [76]: images = np.random.randint(0,255,(3,10,10,3))

In [77]: %timeit np.array([np.fliplr(images[i]) for i in range(3)])
100000 loops, best of 3: 5.76 µs per loop

In [78]: %timeit images[...,::-1,:].copy()
100000 loops, best of 3: 2.23 µs per loop

In [79]: images = np.random.randint(0,255,(3,100,100,3))

In [80]: %timeit np.array([np.fliplr(images[i]) for i in range(3)])
10000 loops, best of 3: 159 µs per loop

In [81]: %timeit images[...,::-1,:].copy()
10000 loops, best of 3: 152 µs per loop
like image 130
Divakar Avatar answered May 13 '23 05:05

Divakar