@keestalkstech/

Background image parser

Nodejs

Cutout image from background.

fork
loading
Files
  • index.js
  • generated
  • package-lock.json
  • package.json
  • test.jpg
index.js
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const Jimp = require("jimp");
const path = require("path");

const MAX_NON_WHITE = 250;
const THUMB_SIZE = 250;
const PREVIEW_QUALITY = 60;

function isNonWhite(img, x, y) {
  const hex = img.getPixelColor(x, y);
  const color = Jimp.intToRGBA(hex);
  return color.r < MAX_NON_WHITE || color.g < MAX_NON_WHITE || color.b < MAX_NON_WHITE;
}

function getTopPixel(img, height, width) {
  for (let h = 0; h < height; h++)
    for (let w = 0; w < width; w++)
      if (isNonWhite(img, w, h))
        return h;

  return null;
}

function getBottomPixel(img, height, width) {
  for (let h = height - 1; h > -1; h--)
    for (let w = 0; w < width; w++)
      if (isNonWhite(img, w, h))
        return h;

  return null;
}

function getLeftPixel(img, width, top, bottom) {
  for (let w = 0; w < width; w++)
    for (let h = top; h <= bottom; h++)
      if (isNonWhite(img, w, h))
        return w;

  return null;
}

function getRightPixel(img, width, top, bottom){
  for(let w = width - 1; w > -1; w--){
    for (let h = top; h <= bottom; h++)
      if (isNonWhite(img, w, h))
        return w;
  }
}

async function getNonWhiteRect(url){

  const name = path.basename(url);

  console.log('Downloading for rect...');
  const img = await Jimp.read(url);

  let width = img.bitmap.width;
  let height = img.bitmap.height;

  if(width > THUMB_SIZE || height > THUMB_SIZE){
    let scale = Math.min(THUMB_SIZE / width,  THUMB_SIZE / height);
    console.log(`Scaling down to ${scale}...`);
    img.scale(scale);
    width = img.bitmap.width;
    height = img.bitmap.height;
  }

  console.log('Calculating rect...');
  const top = getTopPixel(img, height, width);
  const bottom = getBottomPixel(img, height, width);
  const left = getLeftPixel(img, width, top, bottom);
  const right = getRightPixel(img, width, top, bottom);

  const rect = {
    x: left / width,
    y: top / height,
    width: (right-left) / width,
    height: (bottom-top) / height
  };
  console.log(rect);
  console.log('Generating preview...')
  img.crop(
    Math.floor(rect.x * width), 
    Math.floor(rect.y * height), 
    Math.ceil(rect.width * width), 
    Math.ceil(rect.height * height)
  );
  const result = await img.quality(PREVIEW_QUALITY).write(`./generated/${name}-crop.jpg`);
  console.log('Done');
  return rect;
}

//let url = 'https://images.wehkamp.nl/i/wehkamp/16168786_pb_01/lenovo-yoga-book-windows-2-in-1-laptop-zwart-0190404838894.jpg?w=300';
let url = 'https://images.wehkamp.nl/i/wehkamp/16168786_eb_01/lenovo-yoga-book-windows-2-in-1-laptop-zwart-0190404838894.jpg?w=800';
// let url = 'https://images.wehkamp.nl/i/wehkamp/16168786_eb_02/lenovo-yoga-book-windows-2-in-1-laptop-zwart-0190404838894.jpg?w=300';
// let url = 'https://images.wehkamp.nl/i/wehkamp/16168786_eb_03/lenovo-yoga-book-windows-2-in-1-laptop-zwart-0190404838894.jpg?w=300';
// let url = 'https://images.wehkamp.nl/i/wehkamp/16168786_eb_04/lenovo-yoga-book-windows-2-in-1-laptop-zwart-0190404838894.jpg?w=300';
// let url = 'https://images.wehkamp.nl/i/wehkamp/16168786_eb_05/lenovo-yoga-book-windows-2-in-1-laptop-zwart-0190404838894.jpg?w=300';
// let url = 'https://images.wehkamp.nl/i/wehkamp/16168786_eb_06/lenovo-yoga-book-windows-2-in-1-laptop-zwart-0190404838894.jpg?w=300';

getNonWhiteRect(url);

node v10.16.0