Image generation
Generate custom images based on a prompt
Benefits
- Provides bespoke image generation, enabling quick creation of images as needed.
- Secures image generation using the Squid backend context to ensure exclusive access to resources.
How it works
To interact with the image generator, use the Squid Client SDK.
The Squid AI image generator requires admin access. Therefore, its methods must be run in the Squid backend or another admin environment that has access to the application's API key.
Create an image generator
To create an image generator, use Squid AI's .image() method:
- TypeScript
- Python
const imageGenerator = this.squid.ai().image();
image_generator = self.squid.ai().image()
You can then use this image generation instance using the generate() method. generate() takes two parameters: the string prompt for the image to generate, and an options object that contains options for generating the image. This function returns the URL for the generated image.
The following example shows how to generate an image using generate():
- TypeScript
- Python
const options: AiGenerateImageOptions = {
modelName: 'dall-e-3',
quality: 'standard',
size: '1024x1024',
numberOfImagesToGenerate: 1,
};
const imageUrl = await imageGenerator.generate('a pirate ship', options);
options = {
'modelName': 'dall-e-3',
'quality': 'standard',
'size': '1024x1024',
'numberOfImagesToGenerate': 1,
}
image_url = await image_generator.generate('a pirate ship', options)
More information on the AiGenerateImageOptions can be found in the reference documentation.
Securing AI image generation
Since the Squid AI image generator requires admin access, it should only be run in the Squid backend or another environment with admin privileges. If you are running the image generator from a client application connected to the Squid backend, you can use an Executable to trigger a backend function from the frontend. The following Executable function only generates an image if the user is authenticated:
- TypeScript
- Python
import { executable, SquidService } from '@squidcloud/backend';
import { AiGenerateImageOptions } from '@squidcloud/client';
@executable()
async generateImage(prompt: string): Promise<string | Error> {
if (!this.isAuthenticated()) {
return Error('Access denied');
}
const options: AiGenerateImageOptions = {
modelName: 'dall-e-3',
quality: 'standard',
size: '1024x1024',
numberOfImagesToGenerate: 1,
};
return await this.squid
.ai()
.image()
.generate(prompt, options);
}
from squidcloud_backend import SquidService, executable
@executable()
async def generate_image(self, prompt: str) -> str:
if not self.is_authenticated():
raise PermissionError('Access denied')
options = {
'modelName': 'dall-e-3',
'quality': 'standard',
'size': '1024x1024',
'numberOfImagesToGenerate': 1,
}
return await self.squid.ai().image().generate(prompt, options)
Remove the background from an image
In addition to image generation, Squid also provides a background removal feature. This feature allows you to remove the background from an image, making it easier to isolate the subject of the image.
To use this feature, you can call the removeBackground() method on the image generator instance, passing in the file you want to process.
- TypeScript
- Python
await this.squid.ai().image().removeBackground(IMAGE_FILE_TO_PROCESS);
await self.squid.ai().image().remove_background(
image_data, # image file content as bytes
'image.png',
'image/png',
)
Next steps
Squid AI offers many AI features, including custom AI agents and Query with AI functionality. Build powerful AI workflows and applications using Squid AI.