Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mass Composite Breaks Image #2710

Closed
nexensys opened this issue May 11, 2021 · 1 comment
Closed

Mass Composite Breaks Image #2710

nexensys opened this issue May 11, 2021 · 1 comment
Labels

Comments

@nexensys
Copy link

Are you using the latest version? Is the version currently in use as reported by npm ls sharp the same as the latest version as reported by npm view sharp dist-tags.latest?

No, but I think it is close enough, the version I am using is 0.28.1 and the latest is 0.28.2.

What are the steps to reproduce?

Take a large collection of images, each as a Buffer, and attempt to composite them all at once. (My total count was 1353 small images that I needed to composite onto a 4096 x 4096 png image)

What is the expected behaviour?

The images in the earlier parts of the array render properly, but after a certain amount (I got about 10% of my image) the images suddenly stop, and many are cut off.

Are you able to provide a minimal, standalone code sample, without other dependencies, that demonstrates this problem?

What I had was something like this:

//Async
let composite = [];
for (const image of images) { //Image names, not data
  let { data } = await new Promise((resolve, reject) => {
    sharp(image)
      .rotate(-90)
      .toBuffer({ resolveWithObject: true })
      .then(resolve)
      .catch(reject);
  });
  composite.push({
    input: data
    //Position as well
  });
};

sharp({
  create: {
    width: 4096,
    height: 4096,
    channels: 4,
    background: {
      r: 0,
      g: 0,
      b: 0,
      alpha: 0
    }
  }
})
.composite(composite)
.toFile("output.png")

Are you able to provide a sample image that helps explain the problem?

Broken Image:
image

Expected image:
image

(It's a texture pack splitter/merger for Geometry Dash)

What is the output of running npx envinfo --binaries --system?

  System:
    OS: Linux 5.4 Debian GNU/Linux 9 (stretch) 9 (stretch)
    CPU: (4) x64 Intel(R) Xeon(R) CPU @ 2.20GHz
    Memory: 973.71 MB / 25.46 GB
    Container: Yes
    Shell: 4.4.12 - /bin/bash
  Binaries:
    Node: 12.22.1 - /usr/local/bin/node
    Yarn: 1.22.5 - /usr/local/bin/yarn
    npm: 6.14.12 - /usr/local/bin/npm

(I am running on replit)

Current Fix

What I ended up doing was splitting the composite array into chunks of ten images, and cycling between two images.

Object.defineProperty(Array.prototype, 'chunk', {value: function(n) { //Chunk function
    return Array.from(Array(Math.ceil(this.length/n)), (_,i)=>this.slice(i*n,i*n+n));
}});

let even = true;
for (const group of composite.chunk(10)) {
  await new Promise(function (resolve, reject) {
    let temp = sharp(`./temp/temp${even ? "-a" : "-b"}.png`);
    temp
      .composite(group)
      .toFile(`./temp/temp${!even ? "-a" : "-b"}.png`)
      .then(function () {
        deleteFile(`./temp/temp${even ? "-a" : "-b"}.png`);
        even = !even;
        resolve();
      });
  });
}
@lovell
Copy link
Owner

lovell commented May 12, 2021

Please see #2286

@lovell lovell added question and removed triage labels May 12, 2021
@lovell lovell closed this as completed May 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants