AUTOMATIC1111 / stable-diffusion-webui

Stable Diffusion web UI
GNU Affero General Public License v3.0
139.78k stars 26.49k forks source link

Prompt matrix broken on Linux #157

Closed JohannesGaessler closed 2 years ago

JohannesGaessler commented 2 years ago

When trying to create a prompt matrix on Linux:

Error completing request
Arguments: ('', '', 4, 0, False, False, 1, 1, 10, -1.0, 512, 512, 1, False, 0, '', 1, '') {}
Traceback (most recent call last):
  File "/home/johannesg/Projects/stable-diffusion-webui-voldemort/modules/ui.py", line 119, in f
    res = list(func(*args, **kwargs))
  File "/home/johannesg/Projects/waifu-diffusion/scripts/webui_voldemort.py", line 147, in f
    res = func(*args, **kwargs)
  File "/home/johannesg/Projects/stable-diffusion-webui-voldemort/modules/txt2img.py", line 28, in txt2img
    processed = modules.scripts.scripts_txt2img.run(p, *args)
  File "/home/johannesg/Projects/stable-diffusion-webui-voldemort/modules/scripts.py", line 138, in run
    processed = script.run(p, *script_args)
  File "/home/johannesg/Projects/stable-diffusion-webui-voldemort/scripts/prompt_matrix.py", line 81, in run
    grid = images.draw_prompt_matrix(grid, p.width, p.height, prompt_matrix_parts)
  File "/home/johannesg/Projects/stable-diffusion-webui-voldemort/modules/images.py", line 197, in draw_prompt_matrix
    return draw_grid_annotations(im, width, height, hor_texts, ver_texts)
  File "/home/johannesg/Projects/stable-diffusion-webui-voldemort/modules/images.py", line 135, in draw_grid_annotations
    fnt = ImageFont.truetype(opts.font, fontsize)
  File "/usr/lib/python3.10/site-packages/PIL/ImageFont.py", line 959, in truetype
    return freetype(font)
  File "/usr/lib/python3.10/site-packages/PIL/ImageFont.py", line 956, in freetype
    return FreeTypeFont(font, size, index, encoding, layout_engine)
  File "/usr/lib/python3.10/site-packages/PIL/ImageFont.py", line 247, in __init__
    self.font = core.getfont(
OSError: cannot open resource

Intuitively I would have assumed that the problem is that the font set as the default is not available on Linux but after setting the font to "Noto Sans" I still get this exception (I don't know if the string I typed in is the correct way to specify the font).

AUTOMATIC1111 commented 2 years ago

You must type the full path to font for this to work.

JohannesGaessler commented 2 years ago

I can confirm that setting the font to the full path of an installed font works. I think catching the exception and re-throwing with a more informative error message may be useful.

I also tried what someone on /g/ suggested and installed the MS core fonts but this did not seem to work on my machine (Manjaro, maybe I would have needed to reboot for it to work?).

AUTOMATIC1111 commented 2 years ago

I strongly doubt you can get it to work on linux without full path.

orionaskatu commented 2 years ago

I'm on Debian Linux and don't have to use full path, I just have DejaVuSans.ttf in the conf. Maybe you have to put NotoSans-Medium.ttf instead of Noto Sans.

JohannesGaessler commented 2 years ago

I think I figured the problem out. I queried where my package manager installed the fonts and the font that is the default for webui is installed under /usr/share/fonts/TTF/Arial.TTF. Since Linux file systems are case sensitive I tried setting the font to "Arial.TTF". I could then use the prompt matrix grid without issue.

JohannesGaessler commented 2 years ago

A Windows user on /g/ reported having issues with capitalization:

Changed it from Arial.TTF to arial.ttf and it works now, thanks. But how the fuck does this matter in the first place? Isn't Windows case-insensitive?

orionaskatu commented 2 years ago

One solution I see to get rid of fonts issue regardless of the OS is to provide one with the webui. There is a python package for that: https://pypi.org/project/fonts/ We can use for example the free Roboto font. Usage is simple:

from fonts.ttf import Roboto
font = ImageFont.truetype(Roboto)

If @AUTOMATIC1111 is ok with that solution, I think I can do a PR for that 😉

AUTOMATIC1111 commented 2 years ago

I kind of don't want to include a font.

Edit: never mind, go for it.