cli_tools.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. from optparse import OptionParser
  2. from .blind_watermark import WaterMark
  3. usage1 = 'blind_watermark --embed --pwd 1234 image.jpg "watermark text" embed.png'
  4. usage2 = 'blind_watermark --extract --pwd 1234 --wm_shape 111 embed.png'
  5. optParser = OptionParser(usage=usage1 + '\n' + usage2)
  6. optParser.add_option('--embed', dest='work_mode', action='store_const', const='embed'
  7. , help='Embed watermark into images')
  8. optParser.add_option('--extract', dest='work_mode', action='store_const', const='extract'
  9. , help='Extract watermark from images')
  10. optParser.add_option('-p', '--pwd', dest='password', help='password, like 1234')
  11. optParser.add_option('--wm_shape', dest='wm_shape', help='Watermark shape, like 120')
  12. (opts, args) = optParser.parse_args()
  13. def main():
  14. bwm1 = WaterMark(password_img=int(opts.password))
  15. if opts.work_mode == 'embed':
  16. if not len(args) == 3:
  17. print('Error! Usage: ')
  18. print(usage1)
  19. return
  20. else:
  21. bwm1.read_img(args[0])
  22. bwm1.read_wm(args[1], mode='str')
  23. bwm1.embed(args[2])
  24. print('Embed succeed! to file ', args[2])
  25. print('Put down watermark size:', len(bwm1.wm_bit))
  26. if opts.work_mode == 'extract':
  27. if not len(args) == 1:
  28. print('Error! Usage: ')
  29. print(usage2)
  30. return
  31. else:
  32. wm_str = bwm1.extract(filename=args[0], wm_shape=int(opts.wm_shape), mode='str')
  33. print('Extract succeed! watermark is:')
  34. print(wm_str)
  35. '''
  36. python -m blind_watermark.cli_tools --embed --pwd 1234 examples/pic/ori_img.jpeg "watermark text" examples/output/embedded.png
  37. python -m blind_watermark.cli_tools --extract --pwd 1234 --wm_shape 111 examples/output/embedded.png
  38. cd examples
  39. blind_watermark --embed --pwd 1234 examples/pic/ori_img.jpeg "watermark text" examples/output/embedded.png
  40. blind_watermark --extract --pwd 1234 --wm_shape 111 examples/output/embedded.png
  41. '''