I want to make a nodejs application that takes a 9 digit numbers array which represent an rgb pixel so 123456789 represent r:123, g:456, b:789 and convert it to jpg image.
so example of array is :
let pixels = [
'000115166', '000115164', '000115164', '000115166', '000115166',
'000115166', '000115166', '000115164', '000115164', '000115166',
'000115166', '000115166', '000115166', '000115164', '000115166',
'000115166', '000115166', '000115166', '000115166', '000115166',
'000115164', '000115166', '000115166', '000115164', '000115164',
'000114168', '000115166', '000114168', '000115164', '000114168']
is there a way to do that in nodejs app?
Using sharp npm in node.js we can convert images using RGB color hope this code will help you
const sharp = require("sharp"); const fs = require("fs"); const path = require("path"); const { v4: uuidv4 } = require("uuid"); const convertImage = async () => { let pixels = [ "198115166", "000115164", "000115164", "000115166", "000115166", "000115166", "000115166", "000115164", "000115164", "000115166", "000115166", "000115166", "000115166", "000115164", "000115166", "000115166", "000115166", "000115166", "000115166", "000115166", "000115164", "000115166", "000115166", "000115164", "000115164", "000114168", "000115166", "000114168", "000115164", "000114168", ]; const rgbData = []; pixels.forEach((e) => { const obj = { r: e.substring(0, 3), g: e.substring(3, 6), b: e.substring(6, 9), }; rgbData.push(obj); }); const imagefolderpath = path.resolve(`${__dirname}`, "Images"); for (const color of rgbData) { const filename = `${uuidv4()}.jpeg`; await sharp({ create: { width: 48, height: 48, channels: 4, background: { ...color }, }, }).toFile(filename); } }; convertImage();