图片转HTML(支持动画)
2014-12-19 Tech
也许是受到很久以前看到的这玩意儿的原因:The Shapes of CSS
现在开脑洞写了个自动转换,顺便支持了动画……嗯,纯 CSS (:з」∠)
主要步骤就是用 Python 读图片,然后把像素全转写成 CSS 的 box-shadow
,最后构建一个完整的 HTML 文件输出。
然后用浏览器打开生成的 HTML 文件,就直接看到图片了,如果输入是一个文件夹的话,就以文件夹里面的图片为帧生成一个带动画的 HTML。
之后再更新就丢这儿了: img2html
#!/usr/bin/env python3# -*- coding: utf-8 -*-## @package img2html# Usage : img2html.py file1|dir1 [file2|dir2 ...]# Description : generate html uses box-shadow to show picture# or a html to show your image sequence in a folder as css animation# Dependencies : Python Image Library, Python 3# Note : Take care of the Super-High-Energy output ( >﹏<。)# Date : 2014-12-19# Author : frantic1048import sysimport osfrom PIL import Imagefrom string import Templatepass## @var tHTML template for constructing entire html documenttHTML =## @var tCSSStatic template for constructing static image's css codetCSSStatic =## @var tCSSAnimation template for constructing image sequence's css animation codetCSSAnimation =## @var tCSSKeyframes template entire CSS keyframes ruletCSSKeyframes =## @var tCSSKeyframe template for a single CSS keyframetCSSKeyframe =## @var tCSSKeyframeRule template for a single CSS keyframe inner ruletCSSKeyframeRule =## ensure no trailiing slash in directory nameif ( == '):returnelse:return dirName## write str to a file,named as <exportFileName>.htmlwith as html:## construct HEX Color value for a pixel# @param pixel a RGB mode pixel object to be converted# @return CSS hex format color valuereturn '#{0:02x}{1:02x}{2:02x}'.## construct RGBA Color value for a pixel# @param pixel a RGBA mode pixle object to be comverted# @return CSS rgba format color valuereturn 'rgba({0},{1},{2},{3})'.if (mode == 'RGB'):returnelif (mode == 'RGBA'):returnelse:raise UnknownColorMode## construct single box-shadow param# @param color valid CSS colorreturn## process single image file to html# @param fileName input file's name# @param export output callback(doc, exportFileName):# doc : generated html string# exportFileName : output filenamewith as im:## what called magicboxshadow = '## file name as sysnameexportFileName = fileName+'.html'title =## image sizewidth, height = ,#ensure RGB(A) modeif (im.mode != 'RGBA' or im.mode != 'RGB'):firstPixel =for y in :for x in :color =#link magicboxshadow +=#add a spliter if not the endif (not (y == height-1 and x == width-1)):#keep a '\n' for text editor ˊ_>ˋboxshadow += ',' + '\n'doc =if (export=='):print(doc)else:## process a image folder# files in folder will processed to an animated html# process order is filename asend# @param dirName input file's name# @param export output callback, call with generated html as a string argumentdirName =title =exportFileName = title + '.html'files =FPS = 24mode = 'width, height = 0, 0frameCount = 0keyframeRules =keyframe = 'for f in files:try:with as im:if (export!='):print('processing file --> ' + f)frameCount+=1#ensure RGB(A) modeif (im.mode != 'RGBA' or im.mode != 'RGB'):;#collect animation infoif (width == 0) : width, height = ,if (mode == ') : mode = im.modefirstPixel =boxshadow = 'for y in :for x in :color =#link magicboxshadow +=#add a spliter if not the endif (not (y == height-1 and x == width-1)):#keep a '\n' for text editor ˊ_>ˋboxshadow += ',' + '\n'except:passpercentUnit= 100/frameCountfor i in :if (i == frameCount - 1):pc = '100'elif (i == 0):pc = '0'else:pc =keyframe +=if (export!='):print('generating document...')doc =#outputif (export=='):print(doc)else:print('Start exporting...')print('Finished exporting !\nenjoy with your magical ' + exportFileName + ' _(:з」∠)_')for path in :if :##export to stdout#mipaStatic(path)##export to autonamed fileelif :#mipaAnimation(path)
Comments...