Nodes/Comfyui-Gelbooru/Gelbooru (Random)
ComfyUI Node

Gelbooru (Random)

Pull real board images and ready-made prompts into img2img

By 1mckw·Created 2 years ago·Updated 2 months ago· 11
Gelbooru (Random)
    • imgtags
    • imgurl
    • imgid
    • width
    • height
    • source
    • url
    • IMAGE
    use_custom_promptfalse
    site
    OR_tags
    AND_tags
    exclude_taganimated,
    remove_tagscensored, mosaic_censoring, bar_censor
    add_good_tagstrue
    remove_bad_tagstrue
    convert_underscorefalse
    change_backgroundDon't Change
    change_colorDon't Change
    Safetrue
    Questionabletrue
    Explicittrue
    score0
    api_credentials
    seed0
    count1
    use_last_promptDisabled
    return_picturefalse
    extra_text
    custom_prompt

    This is the node the pack is named for. Gelbooru (Random) fires a query at Gelbooru or Rule34 with your tag filters, grabs a random post, and hands you two things at once: the image and its full tag list as a prompt. One run of the workflow and you've got a real board image under a positive prompt that actually describes it - no typing tags by hand, no guessing what a reference image contains. That's the workflow: fetch, img2img, iterate.

    The tag half is the sleeper feature. Anime models like Illustrious and NoobAI were trained on exactly this Danbooru-lineage tag vocabulary, so a real post's tags are precisely what those models want to see. And because it's the board's own tags, you get consistent, on-topic prompts batch after batch - the same reason people mine booru data instead of writing prompts freehand.

    How it works

    It builds a Gelbooru (or Rule34) API query: sort:random for the shuffle, your tags joined correctly per site, rating exclusions, and a score floor, then fires it with your credentials and count for the number of posts. One nice debugging touch: the node exposes the full query URL as an output, so when results look wrong you can paste it in a browser and see exactly what the API returned.

    The tag output goes through a processing pipeline before it reaches you: it strips your remove_tags, filters a built-in bad_tags.txt list (watermark, text, censored, ai_generated - 57 of them), optionally rewrites background/color tags, converts underscores if you ask, then prepends quality tags from good_tags.txt. Default good tags are masterpiece, best quality, amazing quality, very aesthetic - which is exactly the quality-tag stack those Danbooru-trained models expect. You can edit both text files in the node's folder if the defaults don't fit.

    Inputs that matter

    It has a lot of inputs, but you'll actually touch these:

    • api_credentials - required, even for Rule34. Format api_key=YOUR_KEY&user_id=YOUR_ID, grabbed free from the bottom of your Gelbooru account options page. The node hard-stops with an error if it's empty.
    • AND_tags - every tag must be present. 1girl, solo narrows hard.
    • OR_tags - at least one must be present. blonde, red_hair, brown_hair gives you variety within one batch. Pairs beautifully with AND for "any hair color, but always one girl."
    • seed - 0 is truly random; set it to make the random selection deterministic.
    • count - up to 100 posts per request; imgtags and imgurl get one line per post.
    • return_picture - downloads only the first post's image as the IMAGE tensor.
    • Safe / Questionable / Explicit - rating toggles, all on by default; flip one off to exclude that rating.

    The use_custom_prompt / custom_prompt pair lets you replace the fetched tags entirely with your own text if you'd rather just use the node as a random image source.

    The gotcha that will confuse you first

    Every workflow run fetches new random results. That's the point, but it also means the node reports itself as changed on every execution - so hitting Queue brings you a fresh image each time, and your img2img chain reruns from a new starting point. To iterate on one batch, set use_last_prompt to "Last Prompt" (or "Previous Prompt" to hop back one). It caches responses to gelbooru_prompt_cache.json so nothing is re-downloaded. If return_picture is on and the download fails - Gelbooru CDN URLs expire - you'll get a gray "gelbooru not available" placeholder instead of an image.

    Install

    ComfyUI Manager (search "Comfyui-Gelbooru"), or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/1mckw/Comfyui-Gelbooru.git
    python_embeded\python.exe -m pip install -r custom_nodes\Comfyui-Gelbooru\requirements.txt
    

    restart, done. Requirements are just requests and boto3 - no model downloads, no heavy deps. Get your API key before your first run or the node will refuse to start a search.

    The usual failure modes

    Credentials error: paste the api_key=...&user_id=... string exactly, no extra spaces. Blank prompt lines: a tag that returned zero posts yields empty output, so loosen OR_tags or drop an AND tag. And remember the Rule34 site option is real - same node, different board - but the credential check runs for both, so fill it in either way.

    CategoryGelbooru

    Inputs (22)

    NameTypeDefaultDescription
    use_custom_promptBOOLEANfalseEnable custom prompt override - replaces imgtags entirely with custom_prompt input
    siteCOMBOImage board to search: Gelbooru or Rule34
    OR_tagsSTRINGAt least one of these tags must be present (broadens results). Example: blonde, red_hair finds images with EITHER blonde OR red_hair. Use when you want variants (hair colors, poses, outfits) in one search
    AND_tagsSTRINGAll of these tags must be present on the image (narrows results). Example: 1girl, smile finds images with BOTH 1girl AND smile
    exclude_tagSTRINGanimated,Tags to exclude from results (comma-separated)
    remove_tagsSTRINGcensored, mosaic_censoring, bar_censorTags to remove from the output imgtags (comma-separated). Useful for filtering out unwanted tags from the result
    add_good_tagsBOOLEANtruePrepend quality tags (masterpiece, best quality, etc.) from good_tags.txt
    remove_bad_tagsBOOLEANtrueRemove known bad tags (watermark, text, censored, etc.) loaded from bad_tags.txt
    convert_underscoreBOOLEANfalseConvert underscores to spaces in tags
    change_backgroundCOMBODon't ChangeAdd or remove background-related tags from the result
    change_colorCOMBODon't ChangeAdd or remove color-related tags from the result
    SafeBOOLEANtrueInclude Safe/General rated posts
    QuestionableBOOLEANtrueInclude Questionable/Sensitive rated posts
    ExplicitBOOLEANtrueInclude Explicit rated posts
    scoreINT00–18446744073709550000Minimum score threshold (0 = no filter)
    api_credentialsSTRINGAPI credentials in format: api_key=YOUR_KEY&user_id=YOUR_ID
    seedINT00–18446744073709550000
    countINT11–100Number of posts to fetch per request (max 100)
    use_last_promptCOMBODisabledDisabled: fetch new results. Last Prompt: reuse previous API response. Previous Prompt: reuse the response before last.
    return_pictureBOOLEANfalseDownload the first post's image and output as IMAGE tensor
    extra_textoptSTRINGGenerated text from another node, prepended before the final output (connect only, not editable)
    custom_promptoptSTRINGCompletely replaces imgtags output when use_custom_prompt is enabled (connect only)

    Outputs (8)

    NameTypeDescription
    imgtagsSTRINGAll tags of the fetched post(s), comma-separated
    imgurlSTRINGDirect file URL(s) of the image(s)
    imgidSTRINGPost ID(s) on the image board
    widthINTWidth of the image(s) in pixels
    heightINTHeight of the image(s) in pixels
    sourceSTRINGSource website URL(s) of the post(s)
    urlSTRINGThe full API query URL used for the request
    IMAGEIMAGEImage tensor (only populated when return_picture is enabled)