1

I am trying to use an img to ascii generator (python code) and I wanted to use a for loop to generate several outputs with different img resolution for a single input.

img_src = 'data/input/'
img_dest = 'data/output/'

for image in os.listdir(img_src):
    for size in range(100,1000,100):
        input_name = img_src + image
        output_name = img_dest + image.split('.')[0] + '_out_' + str(size) + '.jpg'
        %run img2img.py --input input_name --output output_name --num_cols size

I'm doing something like the code above in a jupyter notebook (maybe not best but I like it for experimenting). However I'm having doubts if I can really use %run in a for loop as it is made to call bash commands, but I also need to use the -- options.

Does anyone have a better solution? Thank you in advance :)

  • why not to `import img2img` and use the code there? – balderman Sep 12 '21 at 17:08
  • I don't know how to use -- options with an import – Muuraharkka Sep 12 '21 at 17:12
  • Inside `img2img.py` there is a function you want to call - right? The function accept arguments. See what is the function and what are the arguments – balderman Sep 12 '21 at 17:14
  • It's not that simple, look at it if you are curious https://github.com/uvipen/ASCII-generator/blob/master/img2img.py – Muuraharkka Sep 12 '21 at 17:15
  • so the function is `main()` - see https://github.com/uvipen/ASCII-generator/blob/master/img2img.py#L24 - agreed? Now we need to create the arguments – balderman Sep 12 '21 at 17:17
  • You suggest I modify the source code instead of using the options with the parser? – Muuraharkka Sep 12 '21 at 17:18
  • 2 options. 1) if you control the source - make it play as you like 2) if you dont - build the `opt` data struct and pass it to `main()` – balderman Sep 12 '21 at 17:20
  • 1
    You could use the `subprocess` module instead. The thing about `%run` is that it isn't python. This code won't work outside of jupyter. – tdelaney Sep 12 '21 at 17:21
  • Well, what happened when you did use ``%run`` in a ``for`` loop? – MisterMiyagi Sep 12 '21 at 17:32
  • The arguments like input_name, output_name and size aren't recognized as their value – Muuraharkka Sep 12 '21 at 17:54
  • Check [this question](https://stackoverflow.com/questions/4256107/running-bash-commands-in-python/51950538) for details on how to execute bash commands using `subprocess`. I'd say a `subprocess.run` would suffice, you can set the arguments as you want in a string, check they are correct, and then pass all of them at once, and even capture the output. – MatBBastos Sep 12 '21 at 17:57

1 Answers1

0

WIth a simple script that just prints sys.argv:

In [61]: cat echo.py
import sys
print(sys.argv)

a single call:

In [62]: %run echo.py foo bar
['echo.py', 'foo', 'bar']

In a loop. $ is used to put the current value of the variable in run string. I think this use of $ is a general jupyter magic feature, but I haven't used it much. %run also has a number of options that I haven't played with much.

In [63]: arg = ['--one 2'.split(), '--tow 3'.split()]
    ...: for a,b in arg:
    ...:    %run echo.py $a $b
    ...: 
['echo.py', '--one', '2']
['echo.py', '--tow', '3']

and working from https://stackoverflow.com/a/14411126/901925

In [75]: arg = ['--one 2'.split(), '--tow 3'.split()]
    ...: for a in arg:
    ...:    %run echo.py {a[0]} {a[1]} $a
    ...: 
    ...: 
['echo.py', '--one', '2', '[--one,', '2]']
['echo.py', '--tow', '3', '[--tow,', '3]']

https://ipython.readthedocs.io/en/stable/interactive/reference.html#system-shell-access

hpaulj
  • 221,503
  • 14
  • 230
  • 353