Question: how do I flatten this list:

```
irr_list = [[[u'Animal Collective'], u'Lion in a Coma'], [[u'Animal Collective'], u'Hocus Pocus'], [[u'Animal Collective'], u'Vertical'], [[u'Animal Collective'], u'Natural Selection'], [[u'Animal Collective'], u'Spilling Guts'], [[u'Animal Collective'], u'Recycling']]

```

for this desired output?

```
output = [[u'Animal Collective', u'Lion in a Coma'], [u'Animal Collective', u'Hocus Pocus'], [u'Animal Collective', u'Vertical'], [u'Animal Collective', u'Natural Selection'], [u'Animal Collective', u'Spilling Guts'], [u'Animal Collective', u'Recycling']]

```

I have tried :

```
flattened = [val for sublist in irr_list for val in sublist]

```

to no avail

Answer: 
If you do not mind using ImageMagick, then it is trivial using the -crop command. See <https://imagemagick.org/Usage/crop/#crop_tile>

You can call imagemagick using Python subprocess call.

Input:

[![enter image description here](https://i.stack.imgur.com/95BEv.png)](https://i.stack.imgur.com/95BEv.png)

Lets say you want 4 256x256 tiles for simplicity.

```
convert lena512.png -crop 256x256 lena512_%d.png

```

or by percent

```
convert lena512.png -crop 50x50% lena512_%d.png

```

[![enter image description here](https://i.stack.imgur.com/fs1MO.png)](https://i.stack.imgur.com/fs1MO.png)

[![enter image description here](https://i.stack.imgur.com/TAUxs.png)](https://i.stack.imgur.com/TAUxs.png)

[![enter image description here](https://i.stack.imgur.com/UKpmm.png)](https://i.stack.imgur.com/UKpmm.png)

[![enter image description here](https://i.stack.imgur.com/tRx24.png)](https://i.stack.imgur.com/tRx24.png)
I was not allowed to comment under the top answer from @ZdaR due to my lag of reputation. However, the code was so on point for my use case, I wanted to provide necessary changes for Python3 and Color-Channels from cv2. Thank you, @ZdaR.

This is his code adapted for Python3 with list(range()) instead of xrange() and cv2.COLOR\_BGR2RGB when reading and writing the picture. Somehow cv2 uses the channels the other way around.

```
img = cv2.cvtColor(cv2.imread("path/to/lena.png"),cv2.COLOR_BGR2RGB)
img_shape = img.shape
tile_size = (640, 640)
offset = (640, 640)

for i in list(range(int(math.ceil(img_shape[0]/(offset[1] * 1.0))))):
    for j in list(range(int(math.ceil(img_shape[1]/(offset[0] * 1.0))))):
        cropped_img = img[offset[1]*i:min(offset[1]*i+tile_size[1], img_shape[0]), offset[0]*j:min(offset[0]*j+tile_size[0], img_shape[1])]
        # Debugging the tiles
        cv2.imwrite("debug_" + str(i) + "_" + str(j) + ".png", cv2.cvtColor(cropped_img,cv2.COLOR_BGR2RGB))

```