-
Notifications
You must be signed in to change notification settings - Fork 0
/
imageSlicer.ts
65 lines (54 loc) · 2.08 KB
/
imageSlicer.ts
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import Jimp from 'jimp';
let MIN_HEIGHT = 1600;
// https://github.com/oliver-moran/jimp/tree/master/packages/jimp
enum PICTURE_SIZE {
FOUR_BY_SIX,
FIVE_BY_SEVEN
}
let newImagePromise = function(width:Number,height:Number) {
return new Promise( (resolve, reject) => {
new Jimp(width, height, (err:any, image:any) => {
if ( err ) {
reject(err);
}
resolve(image);
});
});
}
async function processImage(imagePath:string,
widthCount:number,
pictureSize:PICTURE_SIZE ) {
var theImage = await Jimp.read(imagePath);
console.log ("processing image ...");
let picDimentions = pictureSize == PICTURE_SIZE.FOUR_BY_SIX ? (4/6) : (5/7);
let pixelWidth = Math.round(theImage.getWidth() / widthCount);
let pixelHeight = Math.round( picDimentions * pixelWidth);
let heightCount = Math.round(theImage.getHeight() / pixelHeight);
console.log ("Number of total images: " + (widthCount * heightCount));
let destX = 0;
let destY = 0;
for ( var heightIter = 0; heightIter < heightCount; heightIter ++ ) {
for ( var widthIter = 0; widthIter < widthCount; widthIter ++ ) {
let srcX = widthIter * pixelWidth;
let srcY = heightIter * pixelHeight;
let srcWidth = pixelWidth;
let srcHeight = pixelHeight;
var tempImage = await newImagePromise(pixelWidth,pixelHeight) as Jimp;
let imageName = "./output/pic_row_" + heightIter +
"_col_" + widthIter + ".jpg";
let sizeMultiplier = Math.round(MIN_HEIGHT / srcHeight);
tempImage.blit( theImage,
destX,
destY,
srcX,
srcY,
srcWidth,
srcHeight );
tempImage.resize(tempImage.getWidth() * sizeMultiplier,
tempImage.getHeight() * sizeMultiplier)
tempImage.write (imageName);
}
}
}
export {PICTURE_SIZE}
export {processImage}