Esoteric Wisdom Generator

Esoteric Wisdom Generator

Ich dachte mir, ich brauche so einen Weisheiten generator, man kennt ja schon "thispersondoesnotexists.com" oder auch andere. Aber die Idee den Text und den Prompt für DALL-E zu generieren fiel mir während meines eintägigen Wellnessurlaubs ein .. und so mußte ich dies auch gleich umsetzen..

Man kennt auch MEME-Generatoren, aber ich wollte einen kleinen Schritt weiter gehen und schrieb einen Weisheiten Generator.

Hier was man dazu benötigt:

yum install node
npm install fs
npm install openai
npm install jimp
const fs = require('fs');
const Jimp = require('jimp');
const { Configuration, OpenAIApi } = require("openai");
let wisdom = '';
let prompt_wisdom = '';

const configuration = new Configuration({
  apiKey: "DEIN OPEN AI KEY",
});
const openai = new OpenAIApi(configuration);

function splitText(text, width) {
    let words = text.split(" ");
    let lines = [];
    let line = "";
    for (let i = 0; i < words.length; i++) {
        if (line.length + words[i].length < width) {
            line += words[i] + " ";
        } else {
            lines.push(line);
            line = words[i] + " ";
        }
    }
    lines.push(line);
    return lines;
}

async function lightnessAt(image,startX,startY,width,height) {
    let totalLightness = 0;
    for (let x = startX; x < startX+width; x++) {
        for (let y = startY; y < startY+height; y++) {
            const rgba = Jimp.intToRGBA(image.getPixelColor(x, y));
            const lightness = 0.299 * rgba.r + 0.587 * rgba.g + 0.114 * rgba.b;
            totalLightness += lightness;
        }
    }
    return totalLightness / (width * height);
}

async function getWisdom() {
  const response = await openai.createCompletion({
    model: "text-davinci-003",
    prompt: "In German: Quote of the day for this topic: an esoteric wisdom of life",
    temperature: 0.8,
    max_tokens: 100,
    top_p: 1.0,
    frequency_penalty: 0.0,
    presence_penalty: 0.0,
  });
  wisdom = response.data.choices[0].text;
}

async function getPrompt() {
await getWisdom();
const l_prompt = 'Explain an image for this wisdom: ' + wisdom
  const response = await openai.createCompletion({
    model: "text-davinci-003",
    prompt: l_prompt,
    temperature: 0.8,
    max_tokens: 200,
    top_p: 1.0,
    frequency_penalty: 0.0,
    presence_penalty: 0.0,
  });
  prompt_wisdom = response.data.choices[0].text;
}
//getWisdom();

async function getImage() {
await getPrompt();
const l_prompt = prompt_wisdom

const respimg = await openai.createImage({
  prompt: l_prompt,
  n: 1,
  size: "512x512",
});

const url = respimg.data.data[0].url;
const image = await Jimp.read(url);
// Check the average lightness of the image
const lightness = lightnessAt(image,0, 0, image.bitmap.width, 100);

// Set font color based on the lightness
let font;
console.log(wisdom)
console.log(prompt_wisdom)
console.log(lightness)
    if (lightness < 190) {
      font = await Jimp.loadFont(Jimp.FONT_SANS_16_BLACK);
    } else {
      font = await Jimp.loadFont(Jimp.FONT_SANS_16_WHITE);
    }

const lines = splitText(wisdom, 30);
const y = 10;

for (let i = 0; i < lines.length; i++) {
let textWidth = Jimp.measureText(font, lines[i]);
let x = (image.bitmap.width - textWidth) / 2;
    image.print(font, x, y + i * 20, lines[i]);
}

await image.write("image.jpg");

Dieser Code verwendet die OpenAI API, die Jimp-Bibliothek und das Node.js fs-Modul, um ein Bild mit Text zu erzeugen. Der Text wird über die OpenAI-API generiert. Der Code holt zunächst ein Zitat des Tages für eine esoterische Lebensweisheit in deutscher Sprache. Dann erstellt er eine Aufforderung, ein Bild für diese Weisheit zu erklären. Dann generiert er ein Bild mit Hilfe der OpenAI API. Der Code verwendet dann die Jimp-Bibliothek, um das Bild zu lesen, die durchschnittliche Helligkeit des Bildes zu berechnen und die Schriftfarbe des Textes auf der Grundlage der Helligkeit einzustellen. Der Text wird dann in Zeilen aufgeteilt und in der Mitte des Bildes gedruckt. Schließlich wird das Bild im lokalen Dateisystem als "image.jpg" gespeichert.

Hier ein paar generierte Bilder: