Skip to content

Commit

Permalink
Clamp max image size, convert to jpg
Browse files Browse the repository at this point in the history
  • Loading branch information
szlend committed Oct 30, 2016
1 parent a38c7e1 commit a11fc6e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
25 changes: 11 additions & 14 deletions illuminati.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,7 @@ var express = require('express'),
* Return a list of all detected triangles from the image
*/
var findTriangles = function(img) {
var tmp;
if (img.channels() == 4) {
// node-opecv doesn't support BGRA2BGR conversion so just remove the A
var split = img.split();
var rgb = [split[0], split[1], split[2]];
tmp = new cv.Matrix(img.height(), img.width(), cv.Constants.CV_8UC3);
tmp.merge(rgb);
} else {
tmp = img.copy();
}

var tmp = img.copy();
tmp.convertGrayscale();
tmp.gaussianBlur([7, 7])
tmp.canny(10, 100);
Expand Down Expand Up @@ -108,8 +98,15 @@ var fetchCachedImage = function(path, options) {
var fetchImage = function(url, path, options) {
options = options || {}

var stream = new cv.ImageDataStream();
stream.on('load', function(img) {
var sharp = require('sharp');
var convert = sharp()
.resize(1080, 1080)
.max()
.withoutEnlargement()
.toFormat('jpeg')

var draw = new cv.ImageDataStream();
draw.on('load', function(img) {
var triangles = findTriangles(img);
if (options.random) {
drawRandomTriangle(img, triangles, [0, 0, 255], 2);
Expand All @@ -123,7 +120,7 @@ var fetchImage = function(url, path, options) {

request(url, function(err, r) {
if (err == null && r.statusCode == 200) {
request(url).pipe(stream);
request(url).pipe(convert).pipe(draw);
} else {
if (typeof(options.error) === 'function') options.error(err);
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"dependencies": {
"express": "^4.14.0",
"opencv": "^6.0.0",
"request": "^2.76.0"
"request": "^2.76.0",
"sharp": "^0.16.2"
},
"devDependencies": {},
"scripts": {
Expand Down

0 comments on commit a11fc6e

Please sign in to comment.