Repository: AUTOMATIC1111/stable-diffusion-webui

Issue:
[Bug]: "AssertionError: bad number of vertical texts: 2; must be 3" on hires and prompt matrix
### Is there an existing issue for this?

- [X] I have searched the existing issues and checked the recent builds/commits

### What happened?

Performing a render using both **hires fix** and **prompt matrix** results in an error and a failure to render the image grid (and accompanying legend). The individual images still render and can be accessed in the txt2img output folder. The image preview pane remains blank and the error text _AssertionError: bad number of vertical texts: 2; must be 3_* shows below it.

*These numbers might be different. Sometimes _vertical_ will instead say _horizontal_.

### Steps to reproduce the problem

1. In the txt2img tab, enter a base prompt with 2 or more variable prompts separated by the vertical line character. For example: **Painting of a man|Cowboy hat|beard**
2. Toggle **Hires. fix** to on. ✅
3. Under **Script**, select **Prompt matrix**.
4. Click generate.

### What should have happened?

On completion, the generation should have produced a grid showing the array of possible images with the variable prompts toggled on or off. Instead, no grid is produced and an error appears instead. The images still render (and can be viewed from the output folder), but the image grid does not. Since the image grid is the most important part (allowing you to compare images very easily), this is a problem.

Note that using prompt matrix or hires. fix individually still work just fine.

### Commit where the problem happens

3a0d6b77295162146d0a8d04278804334da6f1b4

### What platforms do you use to access UI ?

Windows

### What browsers do you use to access the UI ?

Google Chrome

### Command Line Arguments

_No response_

### Additional information, context and logs

The following text is an example of what shows in my command window when the error occurs:

```
Error completing request8:54,  1.83s/it]
Arguments: ('task(xe7gus9bhl4yqzh)', 'painting of a beautiful woman|cowboy hat|blindfold', '', 'None', 'None', 20, 1, False, False, 1, 1, 10, -1.0, -1.0, 0, 0, 0, False, 512, 512, True, 0.7, 2, 'Latent', 0, 0, 0, 1, False, False, False, False, '', 1, '', 0, '', True, False, False) {}
Traceback (most recent call last):
  File "C:\StableDiffusion\stable-diffusion-webui\modules\call_queue.py", line 56, in f
    res = list(func(*args, **kwargs))
  File "C:\StableDiffusion\stable-diffusion-webui\modules\call_queue.py", line 37, in f
    res = func(*args, **kwargs)
  File "C:\StableDiffusion\stable-diffusion-webui\modules\txt2img.py", line 49, in txt2img
    processed = modules.scripts.scripts_txt2img.run(p, *args)
  File "C:\StableDiffusion\stable-diffusion-webui\modules\scripts.py", line 337, in run
    processed = script.run(p, *script_args)
  File "C:\StableDiffusion\stable-diffusion-webui\scripts\prompt_matrix.py", line 82, in run
    grid = images.draw_prompt_matrix(grid, p.width, p.height, prompt_matrix_parts)
  File "C:\StableDiffusion\stable-diffusion-webui\modules\images.py", line 230, in draw_prompt_matrix
    return draw_grid_annotations(im, width, height, hor_texts, ver_texts)
  File "C:\StableDiffusion\stable-diffusion-webui\modules\images.py", line 175, in draw_grid_annotations
    assert cols == len(hor_texts), f'bad number of horizontal texts: {len(hor_texts)}; must be {cols}'
AssertionError: bad number of horizontal texts: 2; must be 4
```


Discussion:
I think the issue stems from here: https://github.com/AUTOMATIC1111/stable-diffusion-webui/blob/2c1bb46c7ad5b4536f6587d327a03f0ff7811c5d/modules/images.py#L174-L175

The call comes from here:
https://github.com/AUTOMATIC1111/stable-diffusion-webui/blob/2c1bb46c7ad5b4536f6587d327a03f0ff7811c5d/scripts/prompt_matrix.py#L82

Here,  `p.width` and `p.height` are the dimensions **before** upscaling, which defaults to 2x. Thus, the result is a factor of 2 increase in the computed `rows` and `cols`, which leads to the error.

I've fixed it locally, by modifying the above rows to:
```python
grid = images.draw_prompt_matrix(grid, max(p.width, p.hr_upscale_to_x), max(p.height, p.hr_upscale_to_y), prompt_matrix_parts)
```

I also need to round the cols/rows because sometimes the scaling is not exact:
```python
cols = round(im.width / width)
rows = round(im.height / height)
```

This seems to me more like an hack rather than a fix, and it may break something else along the way. So I'll check when I have more time and then maybe send a PR.

Resolving patch:
diff --git a/scripts/prompt_matrix.py b/scripts/prompt_matrix.py
--- a/scripts/prompt_matrix.py
+++ b/scripts/prompt_matrix.py
@@ -99,8 +99,8 @@ def run(self, p, put_at_start, different_seeds, prompt_type, variations_delimite
         p.prompt_for_display = positive_prompt
         processed = process_images(p)
 
-        grid = images.image_grid(processed.images, p.batch_size, rows=1 << ((len(prompt_matrix_parts) - 1) // 2))
-        grid = images.draw_prompt_matrix(grid, p.width, p.height, prompt_matrix_parts, margin_size)
+        grid = images.image_grid(processed.images, p.batch_size, rows=1 << ((len(prompt_matrix_parts) - 1) // 2)) 
+        grid = images.draw_prompt_matrix(grid, processed.images[0].width, processed.images[1].height, prompt_matrix_parts, margin_size)
         processed.images.insert(0, grid)
         processed.index_of_first_image = 1
         processed.infotexts.insert(0, processed.infotexts[0])


Test patch: