Advertisement
spukspital

img2html.py

Nov 15th, 2015
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.90 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # img to html v0.1
  4. # will convert a non-svg image to html
  5. # see http://spukspital.neocities.org/doomguy.html for an example
  6. #
  7. # Gauntlet O. Manatee
  8. # spukspital@openmailbox.org
  9. # 2015-11-15
  10. #
  11. # feel free to use this in any way according to the four freedoms:
  12. # http://www.gnu.org/philosophy/free-sw
  13. #
  14.  
  15. import os, sys
  16. import Image
  17.  
  18.  
  19. imgname = raw_input("Enter the path to the .png file you want to convert.\n(I recommend .png, though it is not mandatory.\n \
  20. Other image files work, too. However, my best results have been .png pics in the RGB color space.)\n")
  21.  
  22. image = Image.open(imgname)
  23.  
  24. pix = image.load()
  25. width, height = image.size
  26. wbody = width*10
  27.  
  28. # creating the output file
  29. outfile = open("i2h-out.html",'a+')
  30. outfile.write(str('<!DOCTYPE html>\n \
  31.                  <html>\n \
  32.                  <head>\n \
  33.                  <title>img2html</title>\n \
  34.                  <style>   .square {display: block; width: 10px; height: 10px; float: left;}\n \
  35.                            .clear {clear: both;}\n \
  36.                            span {margin-top: 0; margin-bottom: 0; padding-top: 0; padding-bottom: 0;}\n \
  37.                            div {width: ' + str(wbody) + 'px;} \n \
  38.                  </style>\n \
  39.                  </head>\n \
  40.                  <body>\n \
  41.                  <div><p>\n'))
  42.  
  43. # pixel coordinates
  44. x = 0
  45. y = 0
  46.  
  47.  
  48. while y < height:
  49.   while x < width:
  50.     r, g, b = pix[x,y]
  51.     outfile.write(str('<span class="square" style="background-color: rgb(' + str(r) + str(',') + str(g) + str(',') + str(b) + ')">&nbsp;</span>\n'))
  52.     x = x+1
  53.   outfile.write(str('</p>\n'))
  54.   outfile.write(str('<p class="clear">\n\n'))
  55.   y = y+1
  56.   x = 0
  57.  
  58. # writing the rest of the html document and closing the file
  59. outfile.write(str('</p></div>\n \
  60.                  </body>\n \
  61.                  </html>'))
  62. outfile.close()
  63. print("Done.")
  64. quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement