Skip to content

Commit 5462af6

Browse files
authored
Merge pull request #590 from alexrudall/feat/multi-image
Add multi-image upload
2 parents 0415dcd + 04bf07c commit 5462af6

File tree

4 files changed

+136
-3
lines changed

4 files changed

+136
-3
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,6 +1564,21 @@ puts response.dig("data", 0, "url")
15641564

15651565
![Ruby](https://i.ibb.co/sWVh3BX/dalle-ruby.png)
15661566

1567+
You can also upload arrays of images, eg.
1568+
1569+
```ruby
1570+
client = OpenAI::Client.new
1571+
response = client.images.edit(
1572+
parameters: {
1573+
model: "gpt-image-1",
1574+
image: [File.open(base_image_path, "rb"), "image.png"],
1575+
prompt: "Take the first image as base and apply the second image as a watermark on the bottom right corner",
1576+
size: "1024x1024"
1577+
# Removed response_format parameter as it's not supported with gpt-image-1
1578+
}
1579+
)
1580+
```
1581+
15671582
### Image Variations
15681583

15691584
Create n variations of an image.

lib/openai/images.rb

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,23 @@ def variations(parameters: {})
1919
private
2020

2121
def open_files(parameters)
22-
parameters = parameters.merge(image: File.open(parameters[:image]))
23-
parameters = parameters.merge(mask: File.open(parameters[:mask])) if parameters[:mask]
24-
parameters
22+
params = parameters.dup
23+
24+
if params[:image].is_a?(Array)
25+
process_image_array(params)
26+
else
27+
params[:image] = File.open(params[:image])
28+
end
29+
30+
params[:mask] = File.open(params[:mask]) if params[:mask]
31+
params
32+
end
33+
34+
def process_image_array(params)
35+
params[:image].each_with_index do |img_path, index|
36+
params[:"image[#{index}]"] = File.open(img_path)
37+
end
38+
params.delete(:image)
2539
end
2640
end
2741
end

spec/fixtures/cassettes/images_edit_multiple.yml

Lines changed: 83 additions & 0 deletions
Large diffs are not rendered by default.

spec/openai/client/images_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,5 +123,26 @@
123123
end
124124
end
125125
end
126+
127+
describe "with multiple images", :vcr do
128+
let(:response) do
129+
OpenAI::Client.new.images.edit(
130+
parameters: {
131+
image: [
132+
File.join(RSPEC_ROOT, "fixtures/files", "image.png"),
133+
File.join(RSPEC_ROOT, "fixtures/files", "mask.png")
134+
],
135+
prompt: "Create a collage from these images",
136+
model: "gpt-image-1" # Required for multiple images
137+
}
138+
)
139+
end
140+
141+
it "converts array of images to image[] parameter" do
142+
VCR.use_cassette("images edit multiple") do
143+
expect(response.dig("data", 0, "b64_json")).to be_a(String)
144+
end
145+
end
146+
end
126147
end
127148
end

0 commit comments

Comments
 (0)