imager.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. from PIL import Image
  2. import os
  3. import subprocess
  4. ON = "X" # Dark pixel in an image
  5. OFF = "." # Light pixel in an image
  6. GREEN_DIFF = 10
  7. # How much brighter the green channel must be (compared to the others),
  8. # to be called green.
  9. def img_point(pix, x, y):
  10. """
  11. img_point, returns a pixel of an image,
  12. given the x and y on the image.
  13. """
  14. return pix[x, y]
  15. def img_avg(pix, x, y):
  16. """
  17. img_avg, returns the average brightness 0-255,
  18. given pixel, and the x and y on the image calls img_point,
  19. to get the individual rgb values to calculate,
  20. brightness. (Grey scale)
  21. """
  22. rgb = img_point(pix, x, y)
  23. # if(im.mode == 'P'):
  24. # rgb = pal[rgb*3:(rgb+1)*3]
  25. # if(im.mode == 'I'):
  26. # return rgb >> 8
  27. return int((rgb[0] + rgb[1] + rgb[2]) / 3)
  28. def is_set(pix, x, y):
  29. global INTENSITY
  30. """
  31. is_set, returns True or False of calculating,
  32. the brightness of the given point on a image,
  33. compared to given intensity.
  34. True means the brightness at the given x and y,
  35. is Less Than which means its dark.
  36. False means the brightness at the given x and y,
  37. is Greater Than which means its bright. (Grey Scale)
  38. """
  39. avg = img_avg(pix, x, y)
  40. return avg < INTENSITY
  41. def is_green(image, x, y):
  42. """
  43. Is this pixel Green?
  44. """
  45. (red, green, blue, _) = image.getpixel((x, y))
  46. # Find the difference between green and the other values.
  47. other = red
  48. if blue > other:
  49. other = blue
  50. diff = green - other
  51. return diff > GREEN_DIFF
  52. def scan_img(image):
  53. """
  54. Given an image, look at a image and find dark pixels,
  55. also find the bounding box size.
  56. (Where's the top,left,right,bottom-most pixels at?)
  57. returns start x, y and end x, y and total number of dark pixels.
  58. """
  59. total = 0
  60. size = image.size
  61. sx = size[0]
  62. ex = 0
  63. sy = size[1]
  64. ey = 0
  65. for y in range(0, size[1]):
  66. for x in range(0, size[0]):
  67. pnt_is = is_set(pix, x, y)
  68. if pnt_is:
  69. total += 1
  70. if x < sx:
  71. sx = x
  72. if x > ex:
  73. ex = x
  74. if y < sy:
  75. sy = y
  76. if y > ey:
  77. ey = y
  78. # print (sx,ex,sy,ey)
  79. # give us a little border to work with
  80. if sx > 0:
  81. sx -= 1
  82. if ex < size[0]:
  83. ex += 1
  84. if sy > 0:
  85. sy -= 1
  86. if ey < size[1]:
  87. ey += 1
  88. return (sx, sy, ex, ey, total)
  89. def output_image(image):
  90. """
  91. For the size of the area we have reduced down to where the majority of dark pixels,
  92. are located, store all that into a list and return the list.
  93. given pixel for function passing.
  94. returns multiple strings in a list that are edited to use characters to represent,
  95. the dark and light pixels of the image. (Grey Scale)
  96. """
  97. size = image.size
  98. for y in range(0, size[1]):
  99. s = ""
  100. for x in range(0, size[0]):
  101. # if is_set(pix, x, y):
  102. if not is_green(image, x, y):
  103. s += ON
  104. else:
  105. s += OFF
  106. print(s)
  107. def image_cleaner(source, destination, padfile=True):
  108. """ Given a source image, clean it and save to destination
  109. If padfile is set, we use imagemagik to add additional padding
  110. around the image, which helps with the conversion process.
  111. """
  112. image = Image.open(source)
  113. size = image.size
  114. for y in range(0, size[1]):
  115. for x in range(0, size[0]):
  116. (r, g, b, _) = image.getpixel( (x,y) )
  117. high = r
  118. if b > high:
  119. high = b
  120. diff = g - high
  121. is_green = diff > 0 # GREEN_DIFF
  122. if is_green:
  123. image.putpixel( (x,y), (255,255,255,255) )
  124. else:
  125. image.putpixel( (x,y), (0, 0, 0, 255) )
  126. if padfile:
  127. tmpfile = "temp.png"
  128. image.save(tmpfile)
  129. output = subprocess.run(
  130. ["convert", tmpfile, "-bordercolor", "White",
  131. "-border", "10x10", "-alpha", "off", destination],
  132. stderr=subprocess.DEVNULL,
  133. # capture_output=False,
  134. shell=False,
  135. )
  136. os.unlink(tmpfile)
  137. else:
  138. image.save(destination)