From a935797cc8ecb95add1848ecaff9f7bb8ae22faa Mon Sep 17 00:00:00 2001 From: lobsterchan27 Date: Fri, 21 Jun 2024 20:56:53 -0400 Subject: [PATCH] thumbnail generation endpoint --- public/scripts/api.js | 28 +++++++++++++++++++++ src/thumbnail.js | 58 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 src/thumbnail.js diff --git a/public/scripts/api.js b/public/scripts/api.js index cbb5812..9cd13ed 100644 --- a/public/scripts/api.js +++ b/public/scripts/api.js @@ -328,6 +328,33 @@ async function live2d(contextName) { } } +/** + * Generates a thumbnail using the specified arguments. + * @param {string} contextName + */ +async function generateThumbnail(contextName) { + console.log('Generating thumbnail:', contextName); + try { + const response = await fetch('/thumbnail/generate', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ contextName }) + }); + + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + + const data = await response.json(); + console.log('Response:', data); + } catch (error) { + console.error('Error:', error); + } + +} + export { textGenerate, text2speech, @@ -338,4 +365,5 @@ export { combineAudio, generateSubs, live2d, + generateThumbnail, } \ No newline at end of file diff --git a/src/thumbnail.js b/src/thumbnail.js new file mode 100644 index 0000000..7da80ab --- /dev/null +++ b/src/thumbnail.js @@ -0,0 +1,58 @@ +const sharp = require('sharp'); + +const client = new vision.ImageAnnotatorClient({ + keyFilename: 'vision-426819-7efb56f51d64.json' +}); + +async function detectTextInImageGoogle(imagePath) { + try { + const [result] = await client.textDetection(imagePath); + const detections = result.textAnnotations; + if (detections.length > 0) { + console.log('Text detected in image:', detections[0].description); + return detections[0].description; + } else { + console.log('No text detected in image.'); + return null; + } + } catch (error) { + console.error('Error detecting text in image:', error); + return null; + } +} + +async function overlayTemplate(originalPath, templatePath, outputPath) { + try { + // Load the original image to get its dimensions + const originalImage = sharp(originalPath); + const originalMetadata = await originalImage.metadata(); + + // Load the template image to get its dimensions + const templateImage = sharp(templatePath); + const templateMetadata = await templateImage.metadata(); + + // Check if resizing is necessary + let resizedTemplateImage; + if (originalMetadata.width !== templateMetadata.width || originalMetadata.height !== templateMetadata.height) { + resizedTemplateImage = await templateImage + .resize(originalMetadata.width, originalMetadata.height, { + fit: 'cover', + kernel: sharp.kernel.lanczos3 // Specify the resizing algorithm + }) + .toBuffer(); + } else { + resizedTemplateImage = await templateImage.toBuffer(); + } + + // Composite the resized (or original) template image onto the original image + const compositeImage = await originalImage + .composite([{ input: resizedTemplateImage, blend: 'over' }]) // Overlay the template + .toFile(outputPath); + + console.log('Thumbnail created successfully:', outputPath); + } catch (error) { + console.error('Error creating thumbnail:', error); + } +} + +Router.post('/overlay', async (req, res) => { \ No newline at end of file