This answer quotes the full Python code from ChatGPT
to convert a broken image into a mask image, Be careful to replace damaged_image.jpg with your actual image file name:
from PIL import Image, ImageOps
# 打开破损图像
img = Image.open('damaged_image.jpg')
# 修复图像
fixed_img = ImageOps.expand(img, border=50, fill='black')
# 转换为mask图像
mask = fixed_img.convert('L').point(lambda x: 255 if x > 128 else 0, mode='1')
# 保存mask图像
mask.save('mask_image.jpg')
Executing the above code generates a file called mask_image.jpg, which is the mask image of the broken image.