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

sharp - creating an image to overlayWith different buffers at specific offsets #1150

Closed
afullstopdot opened this issue Mar 6, 2018 · 1 comment
Labels

Comments

@afullstopdot
Copy link

afullstopdot commented Mar 6, 2018

Why does a sharp object not have persistence, I make a point to overlay each row of pixels and save the final image in the end but toFile() only creates an image with the last position I tried to overLay. Am I using overlayWith incorrectly or is that just the way it has been created. I cant find any information about this.

image.metadata().then((metadata) => {
        return {
            width: metadata.width,
            height: metadata.height,
            channels: metadata.channels
        }
    })
    .then((metadata) => {

        /*
        ** Tiles are 20x20 modulo for less computation
        */

        var xMax = metadata.width - (metadata.width % 20)
        var yMax = metadata.height - (metadata.height % 20)
        let final = sharp({
            create: {
                width: xMax,
                height: yMax,
                channels: metadata.channels,
                background: { r: 255, g: 255, b: 255 }
            }
        })
        .jpeg()

        /*
        ** Loop through x-axis
        */

        for (let xOffset = 0; xOffset <= xMax; xOffset += 20)
        {

            /*
            ** Loop through y-axis
            */

            for (let yOffset = 0; yOffset <= yMax; yOffset += 20)
            {

                /*
                ** To prevent a read biigger then possible
                */

                if ((yOffset + 20) > metadata.height || (xOffset + 20) > metadata.width)
                    break ;

                /*
                ** Extract the 20x20 tile from the input image
                */

                image.clone().extract({
                    left: xOffset,
                    top: yOffset,
                    width: 20,
                    height: 20
                })
                .toBuffer()
                .then((buffer) => {

                    /*
                    ** Get the color distance of the 20x20 tile
                    */

                    return  color.distance(buffer)
                    .then((space) => { return space.space })
                    .catch((err) => console.log(err))
                })
                .then((space) => {

                    /*
                    ** Find the image in the db with the smallest difference
                    */

                    return distance.smallest(db, space)
                })
                .then((path) => {

                    /*
                    ** Resize the image from the db with smallest difference
                    */

                    return sharp(path).resize(20, 20).toBuffer()
                })
                .then((buffer) => {

                    /*
                    ** Insert resized image into 20x20 tile
                    */

                    final.overlayWith(buffer, {
                        top: yOffset,
                        left: xOffset
                    })
                })
                .catch((err) => console.log(err))

            }
        }

        // final.toFile('./output/' + Date.now() + '.jpg')

    })
    .catch((err) => console.log(err))
@lovell
Copy link
Owner

lovell commented Mar 6, 2018

Hello, please see #728

@lovell lovell added the question label Mar 6, 2018
@lovell lovell closed this as completed Mar 9, 2018
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