Advertisement
Guest User

Splitsxiv

a guest
Sep 26th, 2014
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.55 KB | None | 0 0
  1. #!/usr/bin/env python
  2. """ Serially invoke sxiv -i, splitting up a filelist (read from stdin) into chunks of N items.
  3.  
  4. Intended to simplify processing a large list of images, where you may wish to mark many and do operations on them,
  5. by limiting the number you need to deal with at once.
  6.  
  7. Secondarily, prevents a situation from developing in which sxiv eats up all the memory,
  8. presumably allocating it to thumbnails.
  9. This occurs at about 10000 images, on the author's machine with 4Gb memory.
  10.  
  11. Syntax:
  12.    splitsxiv [<chunksize> [sxiv arguments]]
  13.  
  14. chunksize defaults to 256. If you want to specify arguments to sxiv, you must specify chunksize.
  15.  
  16. """
  17.  
  18. import sys
  19. from subprocess import Popen, PIPE
  20.  
  21. chunksize = 256
  22. sxiv_args = []
  23.  
  24. try:
  25.     chunksize = int(sys.argv[1])
  26.     sxiv_args = sys.argv[2:]
  27. except IndexError:
  28.     pass
  29.  
  30. if chunksize < 1:
  31.     print('Chunksize %d invalid, must be greater than zero.' % chunksize)
  32.     sys.exit(1)
  33.  
  34. # Enforce -i
  35. sxiv_args.append('-i')
  36.  
  37. sxiv = Popen(['sxiv'] + sxiv_args, stdin=PIPE)
  38. count = 0
  39. total = 0
  40. nchunks = 1
  41.  
  42. for line in sys.stdin:
  43.     sxiv.stdin.write(line.encode('utf8'))
  44.     count += 1
  45.     total += 1
  46.     if count == chunksize:
  47.         sxiv.stdin.flush()
  48.         sxiv.stdin.close()
  49.         sxiv.wait()
  50.         sxiv = Popen(['sxiv'] + sxiv_args, stdin=PIPE)
  51.         nchunks += 1
  52.         count = 0
  53.  
  54. sxiv.stdin.flush()
  55. sxiv.stdin.close()
  56. sys.stdout.write('splitsxiv: %d chunks processed with chunksize=%d,'
  57.                  ' total %d files processed\n' % (nchunks, chunksize, total))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement