Repository: AUTOMATIC1111/stable-diffusion-webui

Issue:
[Bug]: Training embeddings stop after 1 epoch
### Is there an existing issue for this?

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

### What happened?

Training embeddings suddenly stopped working after the 1st epoch.

After some digging, I  narrowed it down to the "image_embeddings", and perhaps a font issue. I was able to solve the issue (train embeds through completion) by unchecking "Save images with embedding in PNG chunks" 

### Steps to reproduce the problem

Train embeddings as usual, but they stop after the first epoch (error logs below)

### What should have happened?

Training should've completed as per normal 

### Commit where the problem happens

20ae71f

### What Python version are you running on ?

Python 3.10.x

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

Linux, Other/Cloud

### What device are you running WebUI on?

AMD GPUs (RX 6000 above)

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

Google Chrome

### Command Line Arguments

```Shell
No
```


### List of extensions

Auto-Photoshop-StableDiffusion-Plugin
SadTalker
adetailer
clip-interrogator-ext deforum-for-automatic1111-webui
gif2gif openOutpaint-webUl-extension sd-dynamic-prompts
sd-webui-controlnet
sd-webui-infinite-image-browsing sd-webui-text2video
sd_civitai_extension sd_web_ui_preset_utils stable-diffusion-webui-state
ultimate-upscale-for-automatic1111
unprompted
LDSR
Lora
ScuNET
SwinIR
prompt-bracket-checker

### Console logs

```Shell
Traceback (most recent call last):
  File "/notebooks/sd/stable-diffusion-webui/modules/textual_inversion/textual_inversion.py", line 612, in train_embedding
    captioned_image = caption_image_overlay(image, title, footer_left, footer_mid, footer_right)
  File "/notebooks/sd/stable-diffusion-webui/modules/textual_inversion/image_embedding.py", line 150, in caption_image_overlay
    font = ImageFont.truetype(textfont, fontsize)
  File "/usr/local/lib/python3.9/dist-packages/PIL/ImageFont.py", line 976, in truetype
    return freetype(font)
  File "/usr/local/lib/python3.9/dist-packages/PIL/ImageFont.py", line 973, in freetype
    return FreeTypeFont(font, size, index, encoding, layout_engine)
  File "/usr/local/lib/python3.9/dist-packages/PIL/ImageFont.py", line 253, in __init__
    load_from_bytes(font)
  File "/usr/local/lib/python3.9/dist-packages/PIL/ImageFont.py", line 233, in load_from_bytes
    self.font_bytes = f.read()
AttributeError: 'FreeTypeFont' object has no attribute 'read'
```


### Additional information

_No response_


Discussion:
> > What Python version are you running on ?
>
> Python 3.10.x

and yet your traceback quite plainly says `/usr/local/lib/python3.9/`...



Anyway, yeah, I found the bug, it's related to my changes in df7070eca22278b25c921ef72d3f97a221d66242. Will fix.

Resolving patch:
diff --git a/modules/textual_inversion/image_embedding.py b/modules/textual_inversion/image_embedding.py
--- a/modules/textual_inversion/image_embedding.py
+++ b/modules/textual_inversion/image_embedding.py
@@ -1,8 +1,10 @@
 import base64
 import json
+import warnings
+
 import numpy as np
 import zlib
-from PIL import Image, ImageDraw, ImageFont
+from PIL import Image, ImageDraw
 import torch
 
 
@@ -129,14 +131,17 @@ def extract_image_data_embed(image):
 
 
 def caption_image_overlay(srcimage, title, footerLeft, footerMid, footerRight, textfont=None):
+    from modules.images import get_font
+    if textfont:
+        warnings.warn(
+            'passing in a textfont to caption_image_overlay is deprecated and does nothing',
+            DeprecationWarning,
+            stacklevel=2,
+        )
     from math import cos
 
     image = srcimage.copy()
     fontsize = 32
-    if textfont is None:
-        from modules.images import get_font
-        textfont = get_font(fontsize)
-
     factor = 1.5
     gradient = Image.new('RGBA', (1, image.size[1]), color=(0, 0, 0, 0))
     for y in range(image.size[1]):
@@ -147,12 +152,12 @@ def caption_image_overlay(srcimage, title, footerLeft, footerMid, footerRight, t
 
     draw = ImageDraw.Draw(image)
 
-    font = ImageFont.truetype(textfont, fontsize)
+    font = get_font(fontsize)
     padding = 10
 
     _, _, w, h = draw.textbbox((0, 0), title, font=font)
     fontsize = min(int(fontsize * (((image.size[0]*0.75)-(padding*4))/w)), 72)
-    font = ImageFont.truetype(textfont, fontsize)
+    font = get_font(fontsize)
     _, _, w, h = draw.textbbox((0, 0), title, font=font)
     draw.text((padding, padding), title, anchor='lt', font=font, fill=(255, 255, 255, 230))
 
@@ -163,7 +168,7 @@ def caption_image_overlay(srcimage, title, footerLeft, footerMid, footerRight, t
     _, _, w, h = draw.textbbox((0, 0), footerRight, font=font)
     fontsize_right = min(int(fontsize * (((image.size[0]/3)-(padding))/w)), 72)
 
-    font = ImageFont.truetype(textfont, min(fontsize_left, fontsize_mid, fontsize_right))
+    font = get_font(min(fontsize_left, fontsize_mid, fontsize_right))
 
     draw.text((padding, image.size[1]-padding),               footerLeft, anchor='ls', font=font, fill=(255, 255, 255, 230))
     draw.text((image.size[0]/2, image.size[1]-padding),       footerMid, anchor='ms', font=font, fill=(255, 255, 255, 230))


Test patch: