erictik / midjourney-api

MidJourney client. Unofficial Node.js client
Apache License 2.0
1.69k stars 283 forks source link

Is it possible to Upscale 4x with the current features? #268

Closed Slushbanq closed 5 months ago

Slushbanq commented 5 months ago

I know we can upscale the output images as of now but I need some high resolution images via the API. After upscaling some further features come in like Upscale 2x and 4x. image

I tried to use the current upscale function twice to get it done but it did not work.The code is like this `const Upscale = await client.Upscale({ index: 2, msgId: Imagine.id.toString(), hash: Imagine.hash.toString(), flags: Imagine.flags, loading: function (uri, progress) { console.log("loading", uri, "progress", progress); }, }); console.log(Upscale); // Step 2: Upscale Again const UpscaleAgain = await client.Upscale({ index: 2, msgId: Upscale.id.toString(), hash: Upscale.hash.toString(), flags: Upscale.flags, loading: function (uri, progress) { console.log("loading", uri, "progress", progress); }, }); console.log("Upscale Again Result:", UpscaleAgain); if (!UpscaleAgain) { console.log("No Upscale Again message received"); return; }

client.Close();

} catch (error) { console.error("Error upscaling image:", error); throw error; } } `

Any ideas?

orcawhisperer commented 1 month ago

You can upscale 2x, 4x by updating the customId, here I have chosen the upscale(subtle) option which you ca get from the first upscale result.

try {
      console.log(`Starting image generation for prompt: ${prompt}`)
      const result = await client.Imagine(
         prompt,
         (uri: string, progress: string) => {
            console.log(`Progress: ${progress}`)
         }
      )

      if (!result) {
         throw new Error("No result from Midjourney")
      }

      console.log(`Initial image generation completed. ID: ${result.id}`)

      // Find the U1 (first upscale) option
      const U1Option = result.options?.find((option) => option.label === "U1")
      if (!U1Option || !U1Option.custom) {
         throw new Error("U1 upscale option not found")
      }

      console.log(`Starting first upscale process for U1`)
      const firstUpscaledResult = await client.Custom({
         msgId: result.id!,
         customId: U1Option.custom,
         content: prompt, // Include original prompt for consistency
         flags: result.flags,
      })

      console.log(JSON.stringify(firstUpscaledResult))
      // res.json({ imageUrl: firstUpscaledResult?.proxy_url })

      if (!firstUpscaledResult || !firstUpscaledResult.uri) {
         throw new Error("First upscale process failed or didn't return a URI")
      }

      console.log(
         `First upscale completed. URL: ${firstUpscaledResult.proxy_url}`
      )

      // Perform second upscale
      console.log(`Starting second upscale process`)
      const secondUpscaledResult = await client.Custom({
         msgId: firstUpscaledResult.id!,
         customId: firstUpscaledResult.options![0].custom,
         content: prompt,
         flags: result.flags,
      })

      if (!secondUpscaledResult || !secondUpscaledResult.uri) {
         throw new Error("Second upscale process failed or didn't return a URI")
      }

      console.log(
         `Second upscale completed. URL: ${secondUpscaledResult.proxy_url}`
      )
      res.json({ imageUrl: secondUpscaledResult.proxy_url })
   } catch (error) {
      console.error("Error during image generation or upscaling:", error)
      res.status(500).json({
         error: error instanceof Error ? error.message : "Unknown error",
      })
   }