Juan F. Meleiro

stretch

2021-10-01

This was an idea in algorithmic art. I had seen those “pixel sorting” pictures, and found them pretty nice. I thought maybe a stretching effect would also be interesting. So that project sat on my pile of sleeping projects until today, when I started it. And finished it. It's actually pretty simple. The core logic is this:

slit = lambda i : (0, i, width, i + 1) if args.vertical
       else (i, 0, i + 1, height)

strip = img.crop(slit(args.start))

indices = range(0, args.start) if args.invert
          else range(args.start, size)

for i in indices:
    img.paste(strip, slit(i))

I'm using Python because – well, what would I use? C? For manipulating images? I'm not that good.

What it does is take the strip of the image at some index (args.start) and repeats it for all subsequent strips. That means either columns rows, in the positive negative direction, depending on command-line arguments.

The whole code can be found here under the Fair License:

Here's a sample image I created with the program, using the following script (its my cat, Bunge).

#!/bin/sh
./stretch.py -v -i  in.jpg 2470 tmp.jpg
./stretch.py -v    tmp.jpg 3500 tmp.jpg
./stretch.py       tmp.jpg 3500 tmp.jpg
./stretch.py    -i tmp.jpg 1150 out.jpg
rm tmp.jpg

The interface is explained here, with the -h options:

usage: stretch.py [-h] [-v] [-i] INPUT START OUTPUT

Stretch INPUT by copying the column at index START over all subsequent ones.

positional arguments:
  INPUT           Path to the input image.
  START           Index at which to start stretching.
  OUTPUT          Path at which to store result.

optional arguments:
  -h, --help      show this help message and exit
  -v, --vertical  Stretch vertically instead of horizontally.
  -i, --invert    Overwrite all previous lines instead of the subsequent ones.