hozer

Image to console.log code

Nov 23rd, 2015
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.95 KB | None | 0 0
  1. from PIL import Image
  2.  
  3. img = Image.open('DeY6q4Y.png')
  4.  
  5. width, height = img.size
  6.  
  7. blockWidth = 7
  8. blockHeight = 14
  9. colls = int(width/blockWidth)
  10. rows = int(height/blockHeight)
  11.  
  12. rgb_im = img.convert('RGB')
  13. block = "%c "
  14. coloriz = ",'background-color: rgb({0}, {1}, {2});'"
  15. imgStr = ""
  16. imgStl = ""
  17.  
  18. for i in range(rows):
  19.     topOffset = i * blockHeight
  20.     for j in range(colls):
  21.         leftOffset = j * blockWidth
  22.         avR = avG = avB = 0
  23.         for n in range(blockWidth):
  24.             for k in range(blockHeight):
  25.                 r, g, b = rgb_im.getpixel((leftOffset + n, topOffset + n))
  26.                 avR += r
  27.                 avG += g
  28.                 avB += b
  29.         avR /= (blockHeight*blockWidth)
  30.         avG /= (blockHeight*blockWidth)
  31.         avB /= (blockHeight*blockWidth)
  32.         imgStl += coloriz.format(int(avR), int(avG), int(avB))
  33.     imgStr += block * colls + '\\n'
  34.  
  35. print("console.log(\""+ imgStr + '\"' + imgStl +")")
Advertisement
Add Comment
Please, Sign In to add comment