improve image tools

This commit is contained in:
Sha Zhou 2025-06-10 21:15:57 +08:00
parent 609687aaeb
commit 0ee295b83e
2 changed files with 8 additions and 8 deletions

View File

@ -51,13 +51,11 @@ const _loadImage = async (input: string | Buffer) => {
}
const img = await canvas.loadImage(buff).catch((err) => {
throw new Error('Loading image failed: ' + err.message);
console.error('Error loading image:', err);
return undefined;
});
Reflect.set(img, 'contentType', contentType);
return {
img,
};
return img;
}
export const loadImage = async (uri: string | Buffer) => {
@ -105,7 +103,7 @@ export const canvasToBuffer = (canvas: canvas.Canvas, mimeType?: 'image/png' | '
export const processImage = async (url: string, tracker: TokenTracker): Promise<ImageObject | undefined> => {
try {
const { img } = await loadImage(url);
const img = await loadImage(url);
if (!img) {
return;
}
@ -117,6 +115,7 @@ export const processImage = async (url: string, tracker: TokenTracker): Promise<
const canvas = fitImageToSquareBox(img, 256);
const base64Data = (await canvasToDataUrl(canvas)).split(',')[1];
img.src = ''; // Clear the image source to free memory
const {embeddings} = await getEmbeddings([{ image: base64Data }], tracker, {
dimensions: 512,

View File

@ -560,8 +560,9 @@ export async function processURLs(
// Process images
if (withImages && data.images) {
const imageEntries = Object.entries(data.images || {});
let imageObject: any;
imageEntries.forEach(async ([alt, url]) => {
const imageObject = await processImage(url, context.tokenTracker);
imageObject = await processImage(url, context.tokenTracker);
if (imageObject && !imageObjects.find(i => i.url === imageObject.url)) {
imageObjects.push(imageObject);
}