generateImagePngWithMask method
Selectively modify portions of an image using a mask Returns the bytes of the image PNG
Implementation
Future<Uint8List> generateImagePngWithMask({
required String apiKey,
required String engineId,
required ImageMaskingRequestParam params,
required Uint8List initImage,
String? organization,
String? clientId,
String? clientVersion,
}) async {
Map<String, String> headers = {
"Authorization": apiKey,
"Accept": "application/json",
'Content-Type': 'application/json',
"Access-Control-Allow-Origin": "*", // Required for CORS support to work
};
if (organization != null) {
headers.putIfAbsent("Organization", () => organization);
}
if (clientId != null) {
headers.putIfAbsent("Stability-Client-ID", () => clientId);
}
if (clientVersion != null) {
headers.putIfAbsent("Stability-Client-Version", () => clientVersion);
}
Uri endpoint;
if (secure) {
endpoint =
Uri.https(baseUrl, "/v1/generation/$engineId/image-to-image/masking");
} else {
endpoint =
Uri.http(baseUrl, "/v1/generation/$engineId/image-to-image/masking");
}
var request = http.MultipartRequest('POST', endpoint)
..headers.addAll(headers)
..fields.addAll(
params.toJson().map((key, value) => MapEntry(key, value.toString())))
..fields.remove("text_prompts")
..files.add(http.MultipartFile.fromBytes("init_image", initImage));
if(params.maskImage != null) {
request.files.add(http.MultipartFile.fromBytes("mask_image", params.maskImage!));
}
List<TextPrompt> prompts = params.textPrompts;
int index = 0;
for (var element in prompts) {
request.fields['text_prompts[$index][text]'] = element.text;
request.fields['text_prompts[$index][weight]'] =
element.weight.toString();
index++;
}
var response = await request.send();
if (response.statusCode == 200 || response.statusCode == 201) {
return await response.stream.toBytes();
} else {
var error = ServerError.fromJson(jsonDecode(
const Utf8Decoder().convert(await response.stream.toBytes())));
throw GenerationException(
statusCode: response.statusCode, message: error.message);
}
}