spukspital

img2html.py

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