This repository has been archived by the owner on Jun 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
img2Dataurls.tcl
44 lines (44 loc) · 2.09 KB
/
img2Dataurls.tcl
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
#img2Dataurls
#img2Dataurls.tcl
##===================================================================
# Copyright (c) 2017 Yuji SODE <yuji.sode@gmail.com>
#
# This software is released under the MIT License.
# See LICENSE or http://opensource.org/licenses/mit-license.php
##===================================================================
#It outputs file that has an array of dataurls, corresponding to image files with the given extension in the current directory, and returns the output filename.
#=== Synopsis ===
#**Tcl**
#::img2Dataurls::getUrls exName1;
#**Shell**
#tclsh img2Dataurls.tcl exName1 ??exName2? ?exName3? ... ?exNameN??;
#=== Parameters ===
# - exNameN: N-th name of image file extension
##===================================================================
set auto_noexec 1;
package require Tcl 8.6;
#=== namespace: img2Dataurls ===
namespace eval ::img2Dataurls {
#=== dataurl64/dataurl64.tcl (Yuji SODE,2017): the MIT License; https://gist.github.com/YujiSODE/009cdfb080e273ec506a1ef24acd050c ===
#it returns a data url with base64-encoded data.
proc dataurl64 {fileName {type text/plain}} {set c [open $fileName r];fconfigure $c -encoding binary -translation binary;set v [read $c];close $c;return "data:$type;base64,[binary encode base64 $v]";};
#it outputs file that has an array of dataurls, corresponding to image files with the given extension in the current directory, and returns the output filename
proc getUrls {exName} {
# - $exName: name of image filename extension
set T [clock seconds];
#$txt is text data to output
set txt "\\#timestamp:\\t[join [clock format $T -gmt 1] _]\\n\\#directory:\\t[pwd]\\n\\#fileExtension:\\t$exName";
#$txtName is filename to output
set txtName "dataurls_$exName$T.tcl";
set imgList [lsort -dictionary [glob "*.$exName"]];
lmap x $imgList {
lappend txt "\n\#===\tFile:\t$x\t===\n\#dataurl\nset\tdataurls\($x\)\t\{[dataurl64 $x "image/$exName"]\}\;";
};
set ch [open $txtName w];
puts $ch [join $txt {}];
close $ch;
#filename of output data is returned
return $txtName;
};
};
if {$argc>0} {lmap x $argv {::img2Dataurls::getUrls $x;}};