博客图片
我们为博客生成封面图片,这本身完全没什么意思...但生成这些图片的脚本值得一看。
博客文章封面的生成分为3个阶段:
- 将博客的Markdown内容转换为图像提示
- 根据图像提示生成一张图片
- 根据图像提示生成替代文本描述
- 调整大小、复制图片并修补博客文章的前置元数据
script({ parameters: { force: false } })const file = env.files.find(({ filename }) => /\.mdx?$/.test(filename))const target = path.changeext(file.filename, ".png")if (!env.vars.force && (await workspace.stat(target))) cancel("blog image already exists")
// phase 1: generate image promptconst style = "iconic, 2D, 8-bit, corporate, 5-color, simple, geometric, no people, no text"const { text: imagePrompt } = await runPrompt( (_) => { _.def("BLOG_POST", MD.content(file.content)) _.$`Generate an image prompt for DALLE-3 that illustrates the contents of <BLOG_POST>.Include specific description related to the content of <BLOG_POST>. ${style}` }, { responseType: "text", systemSafety: false })
// phase 2: generate imageconst image = await generateImage( `${imagePrompt} STYLE: ${style}`, { mime: "image/png", size: "1792x1024", scale: 768 / 1792, maxHeight: 762, style: "vivid", })
// phase 3: generate alt textconst { text: alt } = await prompt`Generate an alt text description from <IMAGE_PROMPT>.Rephrase the prompt in a way that would be useful for someone who cannot see the image.Do not start with "Alt Text:".
IMAGE_PROMPT:${imagePrompt}`.options({ responseType: "text", systemSafety: false })
// phase 4: patch fronmatterconst fm = MD.frontmatter(file.content)fm.cover = { alt, image: "./" + path.basename(target),}file.content = MD.updateFrontmatter(file.content, fm)
// phase 5: save filesawait workspace.copyFile(image.image.filename, target)await workspace.writeFiles(file)当这个脚本处理了几篇文章后,使用convert命令为所有博客文章生成图片。
genaiscript convert blog-image blog/*.md*图片怎么处理?
这些图像有些抽象,但它们是根据博客文章内容生成的。 图像提示肯定还有改进空间,但这已经是一个良好的开端。