Create Stunning Images Effortlessly with Pencil XL V2

26 Apr 2025
Create Stunning Images Effortlessly with Pencil XL V2

The Pencil XL V2 is an advanced image generation service designed to empower developers with the ability to create high-quality images using sophisticated Text-to-Image, Image-to-Image, and Inpainting capabilities. This service offers enhanced speed and quality, making it an invaluable tool for those looking to integrate powerful visual content into their applications. Whether you're building a game, designing a marketing campaign, or creating artistic projects, Pencil XL V2 simplifies the image creation process, allowing you to focus on creativity rather than technical complexities.

Common use cases for Pencil XL V2 include generating unique artwork, creating assets for video games, enhancing existing images with inpainting, and producing visuals for social media or web content. By leveraging the capabilities of Pencil XL V2, developers can save time and resources while achieving professional-grade results.

Before diving into the specifics of the Cognitive Actions, ensure you have a Cognitive Actions API key and a basic understanding of making API calls.

Generate Image with Pencil XL v2

The "Generate Image with Pencil XL v2" action harnesses the power of the Pencil XL v2 model to generate images based on user-defined prompts and existing images. This action solves the problem of time-consuming image creation by automating the process, enabling users to generate high-quality visuals quickly and efficiently.

Input Requirements

To use this action, you must provide a structured input that includes parameters such as:

  • Prompt: A descriptive input that guides the content and style of the generated image.
  • Image: A URI for an existing image when using Image-to-Image or Inpainting modes.
  • Mask: A URI for an input mask in inpaint mode, determining which areas to preserve or alter.
  • Width and Height: Dimensions for the output image, with a default of 1024 pixels.
  • Strength: Controls the extent of alteration for Image-to-Image or Inpainting, ranging from 0 to 1.
  • Guidance Scale: A factor that influences the adherence to the prompt, with a range from 1 to 50.

Expected Output

The expected output will be a URI pointing to the generated image, allowing developers to easily integrate the visual content into their applications.

Use Cases for this Specific Action

  • Art Creation: Artists can generate unique artwork based on specific themes or styles without manual drawing.
  • Game Development: Developers can create game assets or characters rapidly, enhancing the visual appeal of their games.
  • Content Generation: Marketers can produce eye-catching visuals for campaigns or social media posts, increasing engagement.
  • Image Enhancement: Use inpainting to modify or enhance existing images, fixing imperfections or adding new elements seamlessly.
import requests
import json

# Replace with your actual Cognitive Actions API key and endpoint
# Ensure your environment securely handles the API key
COGNITIVE_ACTIONS_API_KEY = "YOUR_COGNITIVE_ACTIONS_API_KEY"
# This endpoint URL is hypothetical and should be documented for users
COGNITIVE_ACTIONS_EXECUTE_URL = "https://api.cognitiveactions.com/actions/execute"

action_id = "85b8926c-e5c9-468e-b8ac-3f66d67b0d7e" # Action ID for: Generate Image with Pencil XL v2

# Construct the exact input payload based on the action's requirements
# This example uses the predefined example_input for this action:
payload = {
  "width": 1024,
  "height": 1024,
  "prompt": "score_9, score_8_up, score_7_up, 1girl, solo, dynamic angle, indoors, ahoge, angel, backlighting, bare arms, bare hips, bead bracelet, beads, black wings, bracelet, brown hair, china dress, chinese clothes, closed mouth, cowboy shot, double bun, dress, expressionless, eyelashes, feathered wings, from above, gem, hair behind ear, hair bun, hair ornament, hands up, jewelry, long hair, looking at viewer, skirt, sleeveless, sleeveless dress, standing, thighs, very long hair, white skirt, wings, masterpiece, best quality",
  "strength": 0.8,
  "loraScale": 0.6,
  "scheduler": "K_EULER_ANCESTRAL",
  "numOutputs": 1,
  "guidanceScale": 3.5,
  "negativePrompt": "score_6, score_5, score_4, photo, 3d, realistic, disfigured, kitsch, ugly, oversaturated, greain, low-res, deformed, blurry, bad anatomy, poorly drawn face, mutation, mutated, extra limb, poorly drawn hands, missing limb, floating limbs, disconnected limbs, malformed hands, blur, out of focus, long neck, long body, disgusting, poorly drawn, childish, mutilated, mangled, old, surreal, calligraphy, sign, writing, watermark, text, body out of frame, extra legs, extra arms, extra feet, out of frame, poorly drawn feet, cross-eye",
  "numInferenceSteps": 40
}

headers = {
    "Authorization": f"Bearer {COGNITIVE_ACTIONS_API_KEY}",
    "Content-Type": "application/json",
    # Add any other required headers for the Cognitive Actions API
}

# Prepare the request body for the hypothetical execution endpoint
request_body = {
    "action_id": action_id,
    "inputs": payload
}

print(f"--- Calling Cognitive Action: {action.name or action_id} ---")
print(f"Endpoint: {COGNITIVE_ACTIONS_EXECUTE_URL}")
print(f"Action ID: {action_id}")
print("Payload being sent:")
print(json.dumps(request_body, indent=2))
print("------------------------------------------------")

try:
    response = requests.post(
        COGNITIVE_ACTIONS_EXECUTE_URL,
        headers=headers,
        json=request_body
    )
    response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)

    result = response.json()
    print("Action executed successfully. Result:")
    print(json.dumps(result, indent=2))

except requests.exceptions.RequestException as e:
    print(f"Error executing action {action_id}: {e}")
    if e.response is not None:
        print(f"Response status: {e.response.status_code}")
        try:
            print(f"Response body: {e.response.json()}")
        except json.JSONDecodeError:
            print(f"Response body (non-JSON): {e.response.text}")
    print("------------------------------------------------")

In conclusion, Pencil XL V2 offers a robust solution for developers looking to streamline the image generation process. By providing the ability to create stunning visuals quickly and easily, it opens up new possibilities for creativity and innovation across various industries. As you explore the capabilities of Pencil XL V2, consider how you can integrate it into your projects to enhance user experience and deliver high-quality content. Start experimenting with the action today and unlock the potential of automated image creation!