Many users have asked how to make the MidJourney Action output only one image instead of of four images.
We’ve answered that question elsewhere. I wanted to answer it here as well.
The Problem
By default, if you use the MidJourney Action within Pickaxe, it will output a grid with four images. This is good, but many users want only one single image to appear in the output.
The Solution
- Go to the Act tab
- Duplicate the MidJourney Action
- Edit the MidJourney Action code to be the following (underneath)
import os
import requests
import time
import shutil
def midjourney_images_userapi_copy(prompt: str):
Generate an upscaled single image with Midjourney
Args:
prompt (string): prompt for the image
Envs:
USERAPI_KEY (string): <a href='https://blog.userapi.ai/post/5' target='_blank'>UserAPI Documentation</a>
# Fetch the API key from environment variables
api_key = os.getenv('USERAPI_KEY')
if not api_key:
raise ValueError("USERAPI_KEY environment variable not set")
headers = {
"api-key": api_key,
"Content-Type": "application/json"
}
# Step 1: Send the imagine request to generate the initial image grid
imagine_url = "https://api.userapi.ai/midjourney/v2/imagine"
data = {
"prompt": prompt,
"is_disable_prefilter": False
}
response = requests.post(imagine_url, json=data, headers=headers)
# Raise an exception if the request was unsuccessful
if response.status_code != 200:
raise Exception(f"Imagine request failed with status {response.status_code}: {response.text}")
imagine_hash = response.json()["hash"]
print(f"<!--Hash: {imagine_hash} -->") # For tracking purposes
# Wait for the imagine task to complete
for _ in range(90):
status_response = requests.get(
f"https://api.userapi.ai/midjourney/v2/status?hash={imagine_hash}",
headers=headers
)
status_data = status_response.json()
if status_data.get("result") and status_data.get("status") == "done":
# Proceed to upscale the first image (choice 1)
upscale_url = "https://api.userapi.ai/midjourney/v2/upscale"
upscale_data = {
"hash": imagine_hash,
"choice": 1 # Upscale the first image
}
upscale_response = requests.post(upscale_url, json=upscale_data, headers=headers)
if upscale_response.status_code != 200:
raise Exception(f"Upscale request failed with status {upscale_response.status_code}: {upscale_response.text}")
upscale_hash = upscale_response.json()["hash"]
print(f"<!--Upscale Hash: {upscale_hash} -->") # For tracking purposes
# Wait for the upscale task to complete
for _ in range(90):
upscale_status_response = requests.get(
f"https://api.userapi.ai/midjourney/v2/status?hash={upscale_hash}",
headers=headers
)
upscale_status_data = upscale_status_response.json()
if upscale_status_data.get("result") and upscale_status_data.get("status") == "done":
# Download the upscaled image
upscaled_image_url = upscale_status_data["result"]["url"]
upscaled_image_url = upscaled_image_url.replace("\\u0026", "&")
upscaled_image_response = requests.get(upscaled_image_url, stream=True)
if upscaled_image_response.status_code == 200:
image_path = "generated_image.png"
with open(image_path, "wb") as f:
upscaled_image_response.raw.decode_content = True
shutil.copyfileobj(upscaled_image_response.raw, f)
print("Upscaled image downloaded successfully.")
return image_path # Return the path to the image
else:
print("Failed to download the upscaled image.")
return None
elif upscale_status_data.get("status") == "error":
print(f"Upscale failed: {upscale_status_data.get('status_reason')}")
return None
else:
time.sleep(1)
else:
print("Upscale timed out.")
return None
elif status_data.get("status") == "error":
print(f"Image generation failed: {status_data.get('status_reason')}")
return None
else:
time.sleep(1)
else:
print("Image generation timed out.")
return None