Reputation: 310
I get buffer data to my program from external and I want to process buffer data and send it as a buffer also. So I don't want to convert buffer into an image. How can I do this?
I try this way but it not work.
const process = await sharp(incoming_buffer_data).grayscale();
fs.writeFileSync('test.jpg', process);
// I am using this for testing. Allways I am getting worng image format as an error
Upvotes: 6
Views: 12892
Reputation: 1917
Working snippet.
sharp(buffer)
.greyscale()
.toFile('file.png', (err, info) => {
// file is stored as file.png in current directory
})
You can even do other thing as well like resizing
sharp(buffer)
.greyscale()
.resize(512,512,{fit:'contain'})
.toFile('file.png', (err, info) => {
// file is stored as file.png in current directory
})
Upvotes: 0
Reputation: 5245
Assuming incoming_buffer_data
is indeed a buffer and has a supported image format.
You can either get the output as buffer, and send it to fs.writeFileSync()
like you tried to do
const buffer = await sharp(incoming_buffer_data).grayscale().toBuffer();
fs.writeFileSync('test.jpg', buffer);
Or you can write it to a file directly
await sharp(incoming_buffer_data).grayscale().toFile('test.jpg');
Upvotes: 9