NoiseArray.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/python3
  2. # Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  3. # All rights reserved.
  4. # Code licensed under the BSD License.
  5. # http://www.anki3d.org/LICENSE
  6. import argparse
  7. from PIL import Image, ImageDraw
  8. from math import *
  9. import os
  10. class Context:
  11. input = ""
  12. out_prefix = ""
  13. out_count = 0
  14. out_format = ""
  15. img = None
  16. color_delta = 0.0
  17. ctx = Context()
  18. def parse_commandline():
  19. """ Parse the command line arguments """
  20. parser = argparse.ArgumentParser(description = "This program takes a blue noise texture and creates an array of " \
  21. "noise textures",
  22. formatter_class = argparse.ArgumentDefaultsHelpFormatter)
  23. parser.add_argument("-i", "--input", required = True, help = "the input noise texture")
  24. parser.add_argument("-o", "--output-prefix", required = True, help = "prefix of the output images")
  25. parser.add_argument("-c", "--output-img-count", required = True, help = "number of output images")
  26. parser.add_argument("-f", "--output-format", help = "output format", default = "")
  27. args = parser.parse_args()
  28. ctx.input = args.input
  29. ctx.out_prefix = args.output_prefix
  30. ctx.out_count = int(args.output_img_count)
  31. ctx.out_format = args.output_format
  32. def init():
  33. # Open image
  34. ctx.img = Image.open(ctx.input)
  35. # Color fmt
  36. if ctx.img.mode != "RGB" and ctx.img.mode != "RGBA":
  37. raise Exception("Unknown mode %s" % ctx.img.mode)
  38. if ctx.out_format == "":
  39. ctx.out_format = ctx.img.mode
  40. # Color delta
  41. ctx.color_delta = int(0xFF / (ctx.out_count + 1.0))
  42. def create_image(idx):
  43. out_img = Image.new(ctx.img.mode, (ctx.img.width, ctx.img.height))
  44. delta = idx * ctx.color_delta
  45. for x in range(0, ctx.img.width):
  46. for y in range(0, ctx.img.height):
  47. pixel = ctx.img.getpixel((x, y))
  48. r = int(pixel[0] + delta) % 0xFF
  49. g = int(pixel[1] + delta) % 0xFF
  50. b = int(pixel[2] + delta) % 0xFF
  51. if ctx.img.mode == "RGBA":
  52. a = int(pixel[3] + delta) % 0xFF
  53. if ctx.out_format == "RGB":
  54. out_img.putpixel((x, y), (r, g, b))
  55. else:
  56. out_img.putpixel((x, y), (r, g, b, a))
  57. out_img.save("%s_%02d.png" % (ctx.out_prefix, idx))
  58. def main():
  59. """ The main """
  60. parse_commandline()
  61. init()
  62. for i in range(ctx.out_count):
  63. create_image(i + 1)
  64. if __name__ == "__main__":
  65. main()