Skip to content

Instantly share code, notes, and snippets.

@Dmitra
Last active August 12, 2016 06:32
Show Gist options
  • Save Dmitra/8c38fc7746a987da06ed to your computer and use it in GitHub Desktop.
Save Dmitra/8c38fc7746a987da06ed to your computer and use it in GitHub Desktop.
process very big images by splitting to squares
#!/usr/bin/env node
var inputFolder = './';
var ext = '.png';
var fs = require('fs')
, gm = require('gm')
, spawn = require('child_process').spawn;
//gm(inputFolder + '0-0.png')
//.append('ltr', inputFolder + '0-1.png', inputFolder + '0-2.png')
//.write('./resize.png', function (err) {
//console.log(err);
//if (!err) console.log('done');
//});
//for (var i=0; i<7; i++){
//var i = 0;
//for (var j=0; j<3; j++){
var merge = function(cb, file1, file2, output){
output = output || 'out.jpg';
var p = spawn('gm', ['convert', '+append', file1, file2, output])
p.stdout.on('data', function (data) {
});
p.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});
p.on('exit', function (code) {
cb();
});
}
var inject = function(i, j){
var out = 'out';
if (i>2) return;
if (j>6){
j = 0; i++;
}
var fileName1 = j==0 ? i + j.toString() : 'out';
var fileName2 = (j+1) + i.toString();
var file1 = inputFolder + fileName1 + ext;
var file2 = inputFolder + fileName2 + ext;
if (j==6) out = out + i;
var output = inputFolder + out + ext;
var cb = function(){
inject(i, j+1);
}
merge(cb, file1, file2, output)
}
inject(0, 0);
#!/usr/bin/env node
var fs = require('fs')
, wrench = require('wrench')
//works under linux
//, parser = require('xml2json')
, path = process.cwd()
_ = require('lodash')
var test = function(files){
var counter = 0;
files.forEach(function(file){
if (file.match(/test/gi)) {
var filename = path + '/module/' + file;
var f = new wrench.LineReader(filename);
console.log(counter + filename)
//fs.writeFileSync(filename, data);
}
})
}
var php2Json = function(files){
files.forEach(function(file){
if (file.match(/i18n.*\.php$/gi)) {
var filename = path + '/module/' + file;
var data = '{\n';
var f = new wrench.LineReader(filename);
while(f.hasNextLine()) {
var line = f.getNextLine();
if (line.match(/=>/)){
data += line.replace(/=>/, ':').replace(/\s*$/, '');
data += '\n';
}
}
data += '}'
fs.writeFileSync(filename.replace(/\.php$/, '.json'), data);
fs.unlink(filename);
}
})
}
var xml2json = function(files){
files.forEach(function(file){
if (file.match(/user.xml$/g)) {
var filename = path + '/config/' + file;
console.log(filename);
var xml = fs.readFileSync(filename);
var data = parser.toJson(xml)
//add formatting
var data = JSON.stringify(data, null, '');
fs.writeFileSync(filename.replace(/\.xml$/,'.json'), data)
}
})
}
var replace = function(files){
var exp = '/*';
var sub = '/**';
var counter = 0;
var process = [];
var regxp = new RegExp(exp);
files.forEach(function(file){
if (file.match(/\.js/gi)) {
//don't match this parser and .gitignore
if (file.indexOf('node_modules') >= 0 || file.indexOf('parse.js') >= 0) {
return;
}
process.push(path + '/' + file);
}
})
process.forEach(function(file){
var data = '';
var changed = false;
var f = new wrench.LineReader(file);
while(f.hasNextLine()) {
var line = f.getNextLine();
if (line.match(regxp)){
var index = line.indexOf(exp);
console.log('\x1b[36m'+exp+'\x1b[90m' + line.substr(index+exp.length, 110) + '\x1b[90m');
line = line.replace(regxp, sub);
changed = true;
counter++
}
data += (line +'\n');
}
if (changed){
console.log('\x1b[31m'+counter+'\x1b[32m' + ': '+ file)
//fs.writeFileSync(file, data);
}
})
}
function split(file) {
var ids = []
, data = []
, id = ''
, line = ''
while(f.hasNextLine()) {
line = f.getNextLine();
id = line.substr(0,11)
if (_.contains(ids, id)) {
data.push(line)
} else {
fs.writeFileSync(id + '.csv', data.join('\n'))
ids.push(id)
data = []
}
}
}
//var files = wrench.readdirSyncRecursive('.');
var file = new wrench.LineReader('test.csv');
split(file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment