0
# Image Operations
1
2
Generate, edit, upscale, recontextualize, and segment images using Imagen models.
3
4
## Core Imports
5
6
```java
7
import com.google.genai.types.GenerateImagesResponse;
8
import com.google.genai.types.GenerateImagesConfig;
9
import com.google.genai.types.EditImageResponse;
10
import com.google.genai.types.EditImageConfig;
11
import com.google.genai.types.UpscaleImageResponse;
12
import com.google.genai.types.UpscaleImageConfig;
13
import com.google.genai.types.Image;
14
import com.google.genai.types.GeneratedImage;
15
import com.google.genai.types.ReferenceImage;
16
import com.google.genai.types.RawReferenceImage;
17
import com.google.genai.types.MaskReferenceImage;
18
```
19
20
## Generate Images
21
22
```java { .api }
23
public GenerateImagesResponse generateImages(
24
String model,
25
String prompt,
26
GenerateImagesConfig config);
27
```
28
29
```java
30
GenerateImagesConfig config = GenerateImagesConfig.builder()
31
.numberOfImages(4)
32
.aspectRatio("1:1")
33
.outputMimeType("image/jpeg")
34
.build();
35
36
GenerateImagesResponse response = client.models.generateImages(
37
"imagen-3.0-generate-002",
38
"A serene mountain landscape at sunset",
39
config
40
);
41
42
response.generatedImages().ifPresent(images -> {
43
System.out.println("Generated " + images.size() + " images");
44
for (GeneratedImage img : images) {
45
img.image().ifPresent(image -> {
46
// Save or process image
47
});
48
}
49
});
50
```
51
52
## Edit Images
53
54
```java { .api }
55
public EditImageResponse editImage(
56
String model,
57
String prompt,
58
List<ReferenceImage> referenceImages,
59
EditImageConfig config);
60
```
61
62
```java
63
Image baseImage = Image.fromFile("path/to/image.jpg");
64
Image maskImage = Image.fromFile("path/to/mask.png");
65
66
List<ReferenceImage> references = ImmutableList.of(
67
RawReferenceImage.builder()
68
.referenceImage(baseImage)
69
.referenceId(1)
70
.build(),
71
MaskReferenceImage.builder()
72
.referenceImage(maskImage)
73
.referenceId(2)
74
.config(MaskReferenceConfig.builder()
75
.maskMode(MaskReferenceMode.Known.MASK_MODE_FOREGROUND)
76
.build())
77
.build()
78
);
79
80
EditImageConfig config = EditImageConfig.builder()
81
.editMode(EditMode.Known.EDIT_MODE_INPAINT_INSERTION)
82
.numberOfImages(2)
83
.build();
84
85
EditImageResponse response = client.models.editImage(
86
"imagen-3.0-capability-001",
87
"Add a bright sun in the sky",
88
references,
89
config
90
);
91
```
92
93
## Upscale Images
94
95
```java { .api }
96
public UpscaleImageResponse upscaleImage(
97
String model,
98
Image image,
99
String upscaleFactor,
100
UpscaleImageConfig config);
101
```
102
103
```java
104
Image image = Image.fromFile("low-res-image.jpg");
105
106
UpscaleImageConfig config = UpscaleImageConfig.builder()
107
.outputMimeType("image/jpeg")
108
.enhanceInputImage(true)
109
.build();
110
111
UpscaleImageResponse response = client.models.upscaleImage(
112
"imagen-3.0-generate-002",
113
image,
114
"x2", // or "x4"
115
config
116
);
117
118
response.generatedImage().ifPresent(image -> {
119
// Process upscaled image
120
});
121
```
122
123
## Image Configuration
124
125
```java { .api }
126
public final class GenerateImagesConfig {
127
public static Builder builder();
128
129
public Optional<Integer> numberOfImages();
130
public Optional<String> aspectRatio();
131
public Optional<SafetySetting> safetySettings();
132
public Optional<String> personGeneration();
133
public Optional<String> outputMimeType();
134
public Optional<HttpOptions> httpOptions();
135
}
136
```
137
138
**Aspect Ratios**: "1:1", "3:4", "4:3", "9:16", "16:9"
139