image_proc.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import os
  2. import pathlib
  3. import subprocess
  4. def render_svg(svg_in, png_out, renderer, size):
  5. """
  6. Export a single SVG to a PNG based on the user's renderer choice.
  7. """
  8. if renderer == 'inkscape':
  9. cmd = ['inkscape', os.path.abspath(svg_in),
  10. '--export-filename=' + os.path.abspath(png_out),
  11. '-h', str(size), '-w', str(size)]
  12. elif renderer == 'resvg':
  13. cmd = ['resvg', '-w', str(size), '-h', str(size),
  14. os.path.abspath(svg_in), os.path.abspath(png_out)]
  15. elif renderer == 'imagemagick':
  16. cmd = ['convert', '-background', 'none', '-density', str(size / 32 * 128),
  17. '-resize', str(size) + 'x' + str(size), os.path.abspath(svg_in), os.path.abspath(png_out)]
  18. else:
  19. raise AssertionError
  20. try:
  21. r = subprocess.run(cmd, stdout=subprocess.DEVNULL).returncode
  22. except Exception as e:
  23. raise Exception('Rasteriser invocation failed: ' + str(e))
  24. if r:
  25. raise Exception('Rasteriser returned error code: ' + str(r))
  26. def convert_webp(png_in, webp_out):
  27. """
  28. Converts a PNG at `png_in` to a Lossless WebP at `webp_out`.
  29. Will raise an exception if trying to invoke the converter failed.
  30. """
  31. cmd = ['cwebp', '-lossless', '-quiet', os.path.abspath(png_in), '-o', os.path.abspath(webp_out)]
  32. try:
  33. r = subprocess.run(cmd, stdout=subprocess.DEVNULL).returncode
  34. except Exception as e:
  35. raise Exception('Invoking the WebP converter (cwebp) failed: ' + str(e))
  36. if r:
  37. raise Exception('The WebP converter returned the following: ' + str(r))
  38. def convert_jxl(png_in, jxl_out):
  39. """
  40. Converts a single PNG at 'png_in' to a lossless JPEG XL at 'jxl_out'.
  41. Will raise an exception if trying to invoke the converter failed.
  42. """
  43. cmd = ['cjxl', os.path.abspath(png_in), os.path.abspath(jxl_out), '-q', '100', '-e', '9']
  44. # jxl is noisy. gotta put stderr into DEVNULL as well.
  45. try:
  46. r = subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).returncode
  47. except Exception as e:
  48. raise Exception('Invoking the JXL converter (cjxl) failed: ' + str(e))
  49. if r:
  50. raise Exception('The JXL converter returned the following: ' + str(r))
  51. def crush_png(png_in, pngc_out):
  52. """
  53. Crushes a single PNG at `png_in` to `png_out`.
  54. Will raise an exception if trying to invoke the optimiser failed.
  55. """
  56. cmd = ['oxipng', os.path.abspath(png_in), '--out', os.path.abspath(pngc_out), '--quiet']
  57. try:
  58. r = subprocess.run(cmd, stdout=subprocess.DEVNULL).returncode
  59. except Exception as e:
  60. raise Exception('Invoking the PNG crusher (oxipng) failed: ' + str(e))
  61. if r:
  62. raise Exception('The PNG crusher returned the following: ' + str(r))
  63. def batch_add_exif_metadata(paths, metadata, max_batch=1000):
  64. """
  65. Adds EXIF license metadata to an image file in batches.
  66. (This is done in batches because exiftool is far more performant
  67. this way than if it was done invidually)
  68. 'paths' is a list of paths of image files to have EXIF
  69. metadata applied to them.
  70. 'max_batch' is how many input images you can feed in one command
  71. because some operating systems have different restrictions.
  72. """
  73. cmd = ['exiftool']
  74. for tag, val in metadata.items():
  75. cmd.append('-{}={}'.format(tag, val))
  76. cmd.append('-overwrite_original')
  77. remaining = list(paths)
  78. while remaining:
  79. batch, remaining = remaining[:max_batch], remaining[max_batch:]
  80. try:
  81. r = subprocess.run(cmd + batch,
  82. stdout=subprocess.DEVNULL).returncode
  83. except Exception as e:
  84. raise Exception('Invoking the EXIF metadata embedding tool (exiftool) failed: ' + str(e))
  85. if r:
  86. raise Exception('exiftool returned error code: ' + str(r))