Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- """ Serially invoke sxiv -i, splitting up a filelist (read from stdin) into chunks of N items.
- Intended to simplify processing a large list of images, where you may wish to mark many and do operations on them,
- by limiting the number you need to deal with at once.
- Secondarily, prevents a situation from developing in which sxiv eats up all the memory,
- presumably allocating it to thumbnails.
- This occurs at about 10000 images, on the author's machine with 4Gb memory.
- Syntax:
- splitsxiv [<chunksize> [sxiv arguments]]
- chunksize defaults to 256. If you want to specify arguments to sxiv, you must specify chunksize.
- """
- import sys
- from subprocess import Popen, PIPE
- chunksize = 256
- sxiv_args = []
- try:
- chunksize = int(sys.argv[1])
- sxiv_args = sys.argv[2:]
- except IndexError:
- pass
- if chunksize < 1:
- print('Chunksize %d invalid, must be greater than zero.' % chunksize)
- sys.exit(1)
- # Enforce -i
- sxiv_args.append('-i')
- sxiv = Popen(['sxiv'] + sxiv_args, stdin=PIPE)
- count = 0
- total = 0
- nchunks = 1
- for line in sys.stdin:
- sxiv.stdin.write(line.encode('utf8'))
- count += 1
- total += 1
- if count == chunksize:
- sxiv.stdin.flush()
- sxiv.stdin.close()
- sxiv.wait()
- sxiv = Popen(['sxiv'] + sxiv_args, stdin=PIPE)
- nchunks += 1
- count = 0
- sxiv.stdin.flush()
- sxiv.stdin.close()
- sys.stdout.write('splitsxiv: %d chunks processed with chunksize=%d,'
- ' total %d files processed\n' % (nchunks, chunksize, total))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement