Skip to content

Instantly share code, notes, and snippets.

@yukiarimo
Forked from aku/gen_images.mjs
Created March 29, 2024 06:03
Show Gist options
  • Save yukiarimo/ca49b43f889b34b2200edf129d58a046 to your computer and use it in GitHub Desktop.
Save yukiarimo/ca49b43f889b34b2200edf129d58a046 to your computer and use it in GitHub Desktop.
Generate images using Draw Things HTTP API
import { writeFile } from 'fs/promises'
const DRAW_THINGS_URL = 'http://127.0.0.1:7860/sdapi/v1/txt2img'
const BATCH_COUNT = 5
const IMG_SIZE = 512
const MAX_FILE_NAME_LEN = 30
const prompt = 'bunch of carrots'
const params = {
prompt,
negative_prompt:
'(worst quality, low quality, normal quality, (variations):1.4), blur:1.5',
seed: -1,
steps: 20,
guidance_scale: 4,
batch_count: BATCH_COUNT,
width: IMG_SIZE,
height: IMG_SIZE
}
const opts = {
method: 'POST',
mode: 'no-cors',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(params)
}
const now = Date.now()
const getFileName = (idx) =>
`${prompt.replace(/\s/g, '_').substring(0, MAX_FILE_NAME_LEN).toLowerCase()}_${now}_${idx}.png`
const saveImg = async (data, idx) => {
const fileName = getFileName(idx)
await writeFile(fileName, data, 'base64')
}
const res = await fetch(DRAW_THINGS_URL, opts)
const data = await res.json()
const images = data.images.map(saveImg)
await Promise.all(images)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment