-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
* Update image handling for buffers and blobs and add jimp dependency for image processing * Add jimp dependency and mark it as external in compile scripts * Refactor image options and clean up imports for image processing functions * Refactor image processing and resolution functions for clarity and efficiency * Add detail, scaling, and auto-cropping options to images documentation
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { resolveFileDataUri } from "./file" | ||
import { TraceOptions } from "./trace" | ||
|
||
export async function imageEncodeForLLM( | ||
url: string | Buffer | Blob, | ||
options: DefImagesOptions & TraceOptions | ||
) { | ||
const { Jimp, HorizontalAlign, VerticalAlign } = await import("jimp") | ||
const { autoCrop, maxHeight, maxWidth } = options | ||
// If the image is already a string and we don't need to do any processing, return it | ||
if ( | ||
typeof url === "string" && | ||
!autoCrop && | ||
maxHeight === undefined && | ||
maxWidth === undefined | ||
) | ||
return url | ||
|
||
if (typeof url === "string") url = await resolveFileDataUri(url) | ||
|
||
if (url instanceof Blob) url = Buffer.from(await url.arrayBuffer()) | ||
const img = await Jimp.read(url) | ||
if (autoCrop) await img.autocrop() | ||
if (options.maxWidth ?? options.maxHeight) { | ||
await img.contain({ | ||
w: img.width > maxWidth ? maxWidth : img.width, | ||
h: img.height > maxHeight ? maxHeight : img.height, | ||
align: HorizontalAlign.CENTER | VerticalAlign.MIDDLE, | ||
}) | ||
} | ||
const outputMime = img.mime ?? ("image/jpeg" as any) | ||
const buf = await img.getBuffer(outputMime) | ||
const b64 = await buf.toString("base64") | ||
const imageDataUri = `data:${outputMime};base64,${b64}` | ||
return imageDataUri | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.