Image to CSS
2013-03-07 | Liu Yuyang | python
Image to CSS
Warning: 我警告过了,浏览器弱爆了的就不要点开了= =。内存太小了就慎重打开= =
眼残,竟然看到这种奇葩的演示
看了看CSS源码,想起来可以用Python Image Library做一个generator。
结果就真得做了一个……
Codes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
""" Script to turn image into css """
import Image import sys
__author__ = "Reverland ([email protected])"
def getcss(im): """docstring for get""" css = """position: absolute; top: 30px; left: 30px; width: 0; height: 0; box-shadow: """ string = '%dpx %dpx 0px 1px rgb%s,\n' for y in range(0, im.size[1], 1): for x in range(0, im.size[0], 1): if im.size[1] - y <= 1 and im.size[0] - x <= 1: string = '%dpx %dpx 0px 1px rgb%s;\n' color = im.getpixel((x, y)) css += string % (x, y, color) return css
def gethtml(css): """docstring for gethtml""" html = """ <div style=" %s"></div> """ % css return html
if __name__ == '__main__': filename = sys.argv[1] im = Image.open(filename) ratio = 0.5 size = (int(ratio * im.size[0]), int(ratio * im.size[1])) im.thumbnail(size) html = gethtml(getcss(im)) print html
|
Demo
点击显示图像