From 9e2814cb2c57ce255030c9edb99f80cf4f07f7b1 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sat, 11 Mar 2023 23:07:48 -0500 Subject: [PATCH] Init branch, WIP Needs new library to merge images --- commands/imagine.js | 2 +- controllers/openAiController.js | 4 +- package.json | 3 +- utilities/mergeImages.js | 70 +++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 utilities/mergeImages.js diff --git a/commands/imagine.js b/commands/imagine.js index a754765..278d508 100644 --- a/commands/imagine.js +++ b/commands/imagine.js @@ -57,7 +57,7 @@ module.exports = { log.DEBUG("Images in the result: ", imagesInResult); - if (imagesInResult == 1) dalleEmbed.setImage(imageResults.results.data[0].url); + if (imagesInResult != 0) dalleEmbed.setImage(imageResults.results.data[0].url); await interaction.editReply({ embeds: [dalleEmbed], ephemeral: false }); }); diff --git a/controllers/openAiController.js b/controllers/openAiController.js index 723cc57..3e70ccb 100644 --- a/controllers/openAiController.js +++ b/controllers/openAiController.js @@ -134,13 +134,13 @@ exports.submitTextPromptTransaction = async (prompt, temperature, max_tokens, di pricePerImage = 800; break; default: - log.DEBUG("256px defaulted"); + log.DEBUG("256px defaulted"); pricePerImage = 800; break; } if (!images_to_generate) images_to_generate = 1; - if (!image_size) images_to_generate = "256x256"; + if (!image_size) image_size = "256x256"; totalTokensToBeUsed = pricePerImage * images_to_generate; diff --git a/package.json b/package.json index 1d3b406..65190ac 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,8 @@ "node-html-markdown": "~1.3.0", "node-html-parser": "~6.1.5", "gpt-3-encoder": "~1.1.4", - "user-agents": "~1.0.1303" + "user-agents": "~1.0.1303", + "canvas": "~2.11.0" }, "scripts": { "test": "echo \"Error: no test specified\" && exit 1", diff --git a/utilities/mergeImages.js b/utilities/mergeImages.js new file mode 100644 index 0000000..d4b3e20 --- /dev/null +++ b/utilities/mergeImages.js @@ -0,0 +1,70 @@ +const { createCanvas, loadImage } = require("canvas"); +const fs = require("fs"); + +/** + * + * @param {Array} images The array of images to be merged together + * @param {function} callback The callback function to call with the merged images + * @returns {*} A PNG encoded image + */ +exports.mergeImages = async (images, callback) => { + if (!images) return callback(new Error("No images provided to merge"), undefined); + const numberOfImages = images.length; + const finalImageWidth = 1024; + const finalImageHeight = 1024; + + switch(numberOfImages) { + case 1: + break; + case 2: + break; + case 3: + break; + case 4: + break; + default: + break; + } + + + const imagePosition = { + w: 400, + h: 88, + x: 400, + // Calculate the Y of the image based on the number of + // lines in the title. + y: titleText.length === 2 ? 75 : 100, + }; + + +} + +// Do the same with the title's Y value. +const titleY = titleText.length === 2 ? 300 : 350; +const titleLineHeight = 100; +// And the author's Y value. +const authorY = titleText.length === 2 ? 525 : 500; + +const canvas = createCanvas(width, height); +const context = canvas.getContext("2d"); + +context.fillStyle = "#764abc"; +context.fillRect(0, 0, width, height); + +context.font = "bold 70pt 'PT Sans'"; +context.textAlign = "center"; +context.fillStyle = "#fff"; + +context.fillText(titleText[0], 600, titleY); +if (titleText[1]) context.fillText(titleText[1], 600, titleY + titleLineHeight); + +context.font = "40pt 'PT Sans'"; +context.fillText(`by ${post.author}`, 600, authorY); + +await loadImage("./assets/logo.png").then((image) => { + const { w, h, x, y } = imagePosition; + context.drawImage(image, x, y, w, h); +}); + +const buffer = canvas.toBuffer("image/png"); +fs.writeFileSync("./image.png", buffer); \ No newline at end of file