Skip to content

Commit 50e4b46

Browse files
feat: add arguments width and height
1 parent 046a091 commit 50e4b46

File tree

7 files changed

+126
-16
lines changed

7 files changed

+126
-16
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ jobs:
6262
matrix:
6363
command:
6464
- diffused segmind/tiny-sd "an apple"
65+
- diffused segmind/tiny-sd "an apple" --output apple.jpg --width=512 --height=512
6566
steps:
6667
- name: Checkout repository
6768
uses: actions/checkout@v4
@@ -89,7 +90,7 @@ jobs:
8990
- name: Upload artifact
9091
uses: actions/upload-artifact@v4
9192
with:
92-
name: artifact
93+
name: artifact-${{ github.run_id }}-${{ github.job }}
9394
path: |
9495
*.jpg
9596
*.png

README.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,41 @@ diffused dreamlike-art/dreamlike-photoreal-2.0 "cinematic photo of Godzilla eati
5858
**Optional**: Generated image filename.
5959

6060
```sh
61-
diffused dreamlike-art/dreamlike-photoreal-2.0 "cat eating sushi" --output cat.jpg
61+
diffused dreamlike-art/dreamlike-photoreal-2.0 "cat eating sushi" --output=cat.jpg
6262
```
6363

6464
With short option:
6565

6666
```sh
67-
diffused dreamlike-art/dreamlike-photoreal-2.0 "cat eating sushi" -o cat.jpg
67+
diffused dreamlike-art/dreamlike-photoreal-2.0 "cat eating sushi" -o=cat.jpg
68+
```
69+
70+
### `--width`
71+
72+
**Optional**: Generated image width in pixels.
73+
74+
```sh
75+
diffused stabilityai/stable-diffusion-xl-base-1.0 "dog in space" --width=1024
76+
```
77+
78+
With short option:
79+
80+
```sh
81+
diffused stabilityai/stable-diffusion-xl-base-1.0 "dog in space" -W=1024
82+
```
83+
84+
### `--height`
85+
86+
**Optional**: Generated image height in pixels.
87+
88+
```sh
89+
diffused stabilityai/stable-diffusion-xl-base-1.0 "dog in space" --height=1024
90+
```
91+
92+
With short option:
93+
94+
```sh
95+
diffused stabilityai/stable-diffusion-xl-base-1.0 "dog in space" -H=1024
6896
```
6997

7098
### `--version`

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ authors = [
66
]
77
description = "Generate images with diffusion models"
88
readme = "README.md"
9-
requires-python = ">=3.9"
9+
requires-python = ">=3.10"
1010
classifiers = [
1111
"Programming Language :: Python :: 3",
1212
"Operating System :: OS Independent",

src/diffused/cli.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,26 @@ def main(argv: list[str] = None) -> None:
3030
help="output file",
3131
)
3232

33+
parser.add_argument(
34+
"--width",
35+
"-W",
36+
type=int,
37+
help="image width",
38+
)
39+
40+
parser.add_argument(
41+
"--height",
42+
"-H",
43+
type=int,
44+
help="image height",
45+
)
46+
3347
args = parser.parse_args(argv)
3448

3549
filename = args.output if args.output else f"{uuid1()}.png"
36-
image = generate(model=args.model, prompt=args.prompt)
50+
image = generate(
51+
model=args.model, prompt=args.prompt, width=args.width, height=args.height
52+
)
3753
image.save(filename)
3854
print(f"🤗 {filename}")
3955

src/diffused/generate.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@
22
from PIL import Image
33

44

5-
def generate(model: str, prompt: str) -> Image.Image:
5+
def generate(
6+
model: str, prompt: str, width: int | None = None, height: int | None = None
7+
) -> Image.Image:
68
"""
79
Generate image with diffusion model.
810
911
Args:
1012
model (str): Diffusion model.
1113
prompt (str): Text prompt.
14+
width (int): Image width.
15+
height (int): Image height.
1216
1317
Returns:
1418
image (PIL.Image.Image): Pillow image.
1519
"""
1620
pipeline = AutoPipelineForText2Image.from_pretrained(model)
17-
return pipeline(prompt).images[0]
21+
return pipeline(prompt=prompt, width=width, height=height).images[0]

tests/test_cli.py

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,36 @@ def test_version(capsys: pytest.LogCaptureFixture) -> None:
1414
assert captured.out == __version__ + "\n"
1515

1616

17+
def test_version_short(capsys: pytest.LogCaptureFixture) -> None:
18+
with pytest.raises(SystemExit):
19+
main(["-v"])
20+
captured = capsys.readouterr()
21+
assert captured.out == __version__ + "\n"
22+
23+
1724
def test_help(capsys: pytest.LogCaptureFixture) -> None:
1825
with pytest.raises(SystemExit):
1926
main(["--help"])
2027
captured = capsys.readouterr()
2128
assert "Generate image with diffusion model" in captured.out
2229

2330

24-
def test_required(capsys: pytest.LogCaptureFixture) -> None:
31+
def test_help_short(capsys: pytest.LogCaptureFixture) -> None:
32+
with pytest.raises(SystemExit):
33+
main(["-h"])
34+
captured = capsys.readouterr()
35+
assert "Generate image with diffusion model" in captured.out
36+
37+
38+
def test_no_arguments(capsys: pytest.LogCaptureFixture) -> None:
2539
with pytest.raises(SystemExit) as exception:
2640
main([])
2741
captured = capsys.readouterr()
2842
assert exception.type is SystemExit
2943
assert "error: the following arguments are required: model, prompt" in captured.err
3044

3145

32-
def test_invalid(capsys: pytest.LogCaptureFixture) -> None:
46+
def test_invalid_argument(capsys: pytest.LogCaptureFixture) -> None:
3347
with pytest.raises(SystemExit) as exception:
3448
main(["model", "prompt", "--invalid"])
3549
captured = capsys.readouterr()
@@ -39,7 +53,7 @@ def test_invalid(capsys: pytest.LogCaptureFixture) -> None:
3953

4054
@patch("diffusers.AutoPipelineForText2Image.from_pretrained")
4155
@patch("PIL.Image.Image.save")
42-
def test_generate(
56+
def test_generate_image(
4357
mock_from_pretrained: Mock, mock_save: Mock, capsys: pytest.LogCaptureFixture
4458
) -> None:
4559
main(["model", "prompt"])
@@ -56,11 +70,45 @@ def test_generate(
5670

5771
@patch("diffusers.AutoPipelineForText2Image.from_pretrained")
5872
@patch("PIL.Image.Image.save")
59-
def test_generate_output(
73+
def test_output(
6074
mock_from_pretrained: Mock, mock_save: Mock, capsys: pytest.LogCaptureFixture
6175
) -> None:
6276
filename = "image.png"
6377
main(["model", "prompt", "--output", filename])
6478
mock_save.assert_called_once()
6579
captured = capsys.readouterr()
6680
assert captured.out == f"🤗 {filename}\n"
81+
82+
83+
@patch("diffusers.AutoPipelineForText2Image.from_pretrained")
84+
@patch("PIL.Image.Image.save")
85+
def test_output_short(
86+
mock_from_pretrained: Mock, mock_save: Mock, capsys: pytest.LogCaptureFixture
87+
) -> None:
88+
filename = "image.png"
89+
main(["model", "prompt", "-o", filename])
90+
mock_save.assert_called_once()
91+
captured = capsys.readouterr()
92+
assert captured.out == f"🤗 {filename}\n"
93+
94+
95+
@patch("diffusers.AutoPipelineForText2Image.from_pretrained")
96+
@patch("PIL.Image.Image.save")
97+
def test_width_height(
98+
mock_from_pretrained: Mock, mock_save: Mock, capsys: pytest.LogCaptureFixture
99+
) -> None:
100+
main(["model", "prompt", "--width", "1024", "--height", "1024"])
101+
mock_save.assert_called_once()
102+
captured = capsys.readouterr()
103+
assert "🤗 " in captured.out
104+
105+
106+
@patch("diffusers.AutoPipelineForText2Image.from_pretrained")
107+
@patch("PIL.Image.Image.save")
108+
def test_width_height_short(
109+
mock_from_pretrained: Mock, mock_save: Mock, capsys: pytest.LogCaptureFixture
110+
) -> None:
111+
main(["model", "prompt", "-W", "1024", "-H", "1024"])
112+
mock_save.assert_called_once()
113+
captured = capsys.readouterr()
114+
assert "🤗 " in captured.out

tests/test_generate.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22

33
from diffused import generate
44

5-
mock_image = Mock()
65

7-
8-
def pipeline(prompt: str):
9-
return {"images": [mock_image]} # pragma: no cover
6+
def pipeline(prompt: str, width: int | None, height: int | None):
7+
pass # pragma: no cover
108

119

1210
mock_pipeline = create_autospec(pipeline)
@@ -20,4 +18,19 @@ def test_generate(mock_from_pretrained: Mock) -> None:
2018
image = generate(model=model, prompt=prompt)
2119
assert isinstance(image, Mock)
2220
mock_from_pretrained.assert_called_once_with(model)
23-
mock_pipeline.assert_called_once_with(prompt)
21+
mock_pipeline.assert_called_once_with(prompt=prompt, width=None, height=None)
22+
mock_pipeline.reset_mock()
23+
24+
25+
@patch("diffusers.AutoPipelineForText2Image.from_pretrained")
26+
def test_generate_width_height(mock_from_pretrained: Mock) -> None:
27+
mock_from_pretrained.return_value = mock_pipeline
28+
model = "model/test"
29+
prompt = "test prompt"
30+
width = 1024
31+
height = 1024
32+
image = generate(model=model, prompt=prompt, width=width, height=height)
33+
assert isinstance(image, Mock)
34+
mock_from_pretrained.assert_called_once_with(model)
35+
mock_pipeline.assert_called_once_with(prompt=prompt, width=width, height=height)
36+
mock_pipeline.reset_mock()

0 commit comments

Comments
 (0)