stable diffusion | | Cell 1 | Search

This code sets up paths to access images generated by a text-to-image AI, likely Stable Diffusion, specifically targeting a folder containing images of desserts in a manga anime style.

Run example

npm run import -- "rename txt2img directories"

rename txt2img directories

const fs = require('fs')
const path = require('path')

const HOMEPATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE
const TXT2IMG = path.join(HOMEPATH, 'stable-diffusion-webui/outputs/txt2img-images')

const IMAGEPATH = path.join(TXT2IMG, 'desserts manga anime style with floral backgrounds')

let replacements = `Cherry Cobbler
Vanilla Cake
Chewy Chocolate Chip Cookies
Lemon Meringue Pie
Classic Strawberry Shortcake
Creme Brulee
Coconut Layer Cake
Lemon Bars
Tiramisu
Cheesecake
Fudgy Brownies
Banana Pudding
Peanut Brittle
Sugar Cookies for Decorating
Apple Pie
Butter Cookies
Peach Cobbler
Peanut Butter Cookies
Chocolate Lava Cakes
No-Bake Cheesecake
Chocolate Cupcakes
Banana Cake
Oatmeal Raisin Cookies
Bread Pudding
Carrot Cake
M&M Cookies
Vanilla Cupcakes
Crepes
Angel Food Cake
Key Lime Pie
Crispy Chocolate Chip Cookies
New York-Style Cheesecake
Peanut Butter Blossoms
Chocolate Cake
Coconut Cream Pie
Apple Crisp
Pumpkin Pie
Pecan Pie
Gingerbread Cookies
Pumpkin Cheesecake
Fruitcake
The Desserts of Your Dreams
Layered Chocolate Mousse Cake
Croquembouche
Strawberry Pocky Stick Cake
Rose Cheesecake Tart
Chocolate-Vanilla Swirl Bundt Cake
Eight-Flavor Sheet Pan Brownie-Cookie Bars
Oreo Bombe
Chocolate Cinnamon Roll Wool Bread
Giant Chocolate PB Cookie Cake
Three-Flavor Diner Pie
Individual Meringue Swan Lakes
Unicorn Cake
Lemon-Blueberry Sunshine Tart
Geode Meringues
Giant Chocolate Lava Cake
Galaxy Cake
Pumpkin Charlotte Royale
Pineapple Upside-Down Doughnuts
Baked Alaska
Our Most-Comforting Dessert Ideas
Chocolate Croissant Bread Pudding with Bourbon Ice Cream Sauce
Peanut Butter Bars with Salted Chocolate Ganache
Baked Fudge
Chocolate Chip Cookies
Banana Pudding
Boston Cream Pie Cheesecake
Chocolate Fondue
Mini Peanut Butter & Jelly Pies
Red Velvet Bread Pudding
Alton's Chocolate Pudding
Homemade Glazed Doughnuts
Warm Chocolate Cakes with Berries
Peanut Butter Cupboard Cookie Bars
Banana Pudding Tiramisu
Amaretto Chocolate Brownies
Triple Chocolate Pudding
Best Bread Pudding
Lizzie's Old-Fashioned Cocoa Cake with Caramel Icing
Rocky Road Pudding
Red Velvet Sandwich Cookies
Vanilla-Banana Pudding
Ooey Gooey Butter Cake
Monster Cookies
Peanut Butter and Jelly Bundt Cake
Individual Caramel Cakes
S'more Brownie Bites
Tarte Tatin
Carrot Cheesecake
Brownie Batter Cookies
Red Velvet Icebox Cake
Cookies and Cream Pudding
Cookies and Cream Cake
Crispy Chocolate Chip Cookies
Bananas Foster
Peanut Butter and Jelly Crumb Cake
Snickerdoodles
Giada's Rice Pudding
Fried Apple Pies
Dark Chocolate Mousse
Dark Chocolate Mousse`.split('\n')

function renameImages() {

  let images = fs.readdirSync(IMAGEPATH).sort((a, b) => fs.statSync(path.join(IMAGEPATH, a)).mtime - fs.statSync(path.join(IMAGEPATH, b)).mtime)

  for(let i = 0; i < images.length; i++) {
    if (images[i][0] == '.') continue
    let seed = images[i].split(/-|\./gi)[1]
    fs.renameSync(path.join(IMAGEPATH, images[i]), 
      path.join( IMAGEPATH, (''+i).padStart((images.length+'').length, '0') + '-' + seed + '-' + replacements[i].replace(/[^a-z0-9 ]/gi, '_') + '.png' ))
  }

}

module.exports = renameImages

What the code could have been:

const fs = require('fs');
const path = require('path');

// Define constants
const HOMEPATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
const TXT2IMG_DIR = path.join(HOMEPATH,'stable-diffusion-webui/outputs/txt2img-images');
const IMAGE_DIR = path.join(TXT2IMG_DIR, 'desserts manga anime style with floral backgrounds');

// Define dessert list and split it into an array
const dessertList = `Cherry Cobbler
Vanilla Cake
Chewy Chocolate Chip Cookies
Lemon Meringue Pie
Classic Strawberry Shortcake
Creme Brulee
Coconut Layer Cake
Lemon Bars
Tiramisu
Cheesecake
Fudgy Brownies
Banana Pudding
Peanut Brittle
Sugar Cookies for Decorating
Apple Pie
Butter Cookies
Peach Cobbler
Peanut Butter Cookies
Chocolate Lava Cakes
No-Bake Cheesecake
Chocolate Cupcakes
Banana Cake
Oatmeal Raisin Cookies
Bread Pudding
Carrot Cake
M&M Cookies
Vanilla Cupcakes
Crepes
Angel Food Cake
Key Lime Pie
Crispy Chocolate Chip Cookies
New York-Style Cheesecake
Peanut Butter Blossoms
Chocolate Cake
Coconut Cream Pie
Apple Crisp
Pumpkin Pie
Pecan Pie
Gingerbread Cookies
Pumpkin Cheesecake
Fruitcake
The Desserts of Your Dreams
Layered Chocolate Mousse Cake
Croquembouche
Strawberry Pocky Stick Cake
Rose Cheesecake Tart
Chocolate-Vanilla Swirl Bundt Cake
Eight-Flavor Sheet Pan Brownie-Cookie Bars
Oreo Bombe
Chocolate Cinnamon Roll Wool Bread
Giant Chocolate PB Cookie Cake
Three-Flavor Diner Pie
Individual Meringue Swan Lakes
Unicorn Cake
Lemon-Blueberry Sunshine Tart
Geode Meringues
Giant Chocolate Lava Cake
Galaxy Cake
Pumpkin Charlotte Royale
Pineapple Upside-Down Doughnuts
Baked Alaska
Our Most-Comforting Dessert Ideas
Chocolate Croissant Bread Pudding with Bourbon Ice Cream Sauce
Peanut Butter Bars with Salted Chocolate Ganache
Baked Fudge
Chocolate Chip Cookies
Banana Pudding
Boston Cream Pie Cheesecake
Chocolate Fondue
Mini Peanut Butter & Jelly Pies
Red Velvet Bread Pudding
Alton's Chocolate Pudding
Homemade Glazed Doughnuts
Warm Chocolate Cakes with Berries
Peanut Butter Cupboard Cookie Bars
Banana Pudding Tiramisu
Amaretto Chocolate Brownies
Triple Chocolate Pudding
Best Bread Pudding
Lizzie's Old-Fashioned Cocoa Cake with Caramel Icing
Rocky Road Pudding
Red Velvet Sandwich Cookies
Vanilla-Banana Pudding
Ooey Gooey Butter Cake
Monster Cookies
Peanut Butter and Jelly Bundt Cake
Individual Caramel Cakes
S'more Brownie Bites
Tarte Tatin
Carrot Cheesecake
Brownie Batter Cookies
Red Velvet Icebox Cake
Cookies and Cream Pudding
Cookies and Cream Cake
Crispy Chocolate Chip Cookies
Bananas Foster
Peanut Butter and Jelly Crumb Cake
Snickerdoodles
Giada's Rice Pudding
Fried Apple Pies
Dark Chocolate Mousse
Dark Chocolate Mousse`.split('\n');

// Function to rename images
function renameImages() {
  // Get image files in the directory
  const images = fs.readdirSync(IMAGE_DIR).sort((a, b) => {
    const statA = fs.statSync(path.join(IMAGE_DIR, a));
    const statB = fs.statSync(path.join(IMAGE_DIR, b));
    return statA.mtime - statB.mtime;
  });

  // Iterate over images and rename them
  images.forEach((image, index) => {
    if (image.startsWith('.')) return; // Skip hidden files

    const seed = image.split(/-|\./gi)[1];
    const dessertName = dessertList[index].replace(/[^a-z0-9 ]/gi, '_');

    // Rename the image
    fs.renameSync(
      path.join(IMAGE_DIR, image),
      path.join(IMAGE_DIR, `${index.toString().padStart(images.length.toString().length, '0')}-${seed}-${dessertName}.png`)
    );
  });
}

// Exports
module.exports = renameImages;

This code snippet appears to be setting up paths for accessing image files generated by a text-to-image AI model, likely Stable Diffusion.

Here's a breakdown:

  1. Dependencies:

  2. Environment Variables:

  3. Image Directory:

  4. Specific Image Path:

  5. Replacements:

Let me know if you have any more questions!