Skip to content

Commit

Permalink
thumbnail generation endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
lobsterchan27 committed Jun 22, 2024
1 parent c007219 commit a935797
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
28 changes: 28 additions & 0 deletions public/scripts/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -338,4 +365,5 @@ export {
combineAudio,
generateSubs,
live2d,
generateThumbnail,
}
58 changes: 58 additions & 0 deletions src/thumbnail.js
Original file line number Diff line number Diff line change
@@ -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) => {

0 comments on commit a935797

Please sign in to comment.