stevequinn / photoborder

Add a border to a photo
The Unlicense
1 stars 1 forks source link

TypeError: load_default() takes 0 positional arguments but 1 was given #3

Closed Rustymage closed 2 days ago

Rustymage commented 2 days ago

Fully appreciate and understand this is a personal script so you may not want or need to answer this. Hopefully, if I manage to resolve it, the answer may help others.

> python main.py -t p -e -p /Users/$USER/Downloads/Pixel\ Optics/IMG_2383.jpg
Adding border to /Users/$USER/Downloads/Pixel Optics/IMG_2383.jpg
Traceback (most recent call last):
  File "/Users/$USER/Downloads/photoborder-main/main.py", line 125, in <module>
    main()
  File "/Users/$USER/Downloads/photoborder-main/main.py", line 121, in main
    save_path = process_image(path, args.exif, args.palette, args.border_type)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/$USER/Downloads/photoborder-main/main.py", line 66, in process_image
    img_with_border = draw_exif(img_with_border, exif, border)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/$USER/Downloads/photoborder-main/border.py", line 129, in draw_exif
    font_size = tm.get_optimal_font_size("Test string", border.bottom * multiplier)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/$USER/Downloads/photoborder-main/text.py", line 104, in get_optimal_font_size
    if check_size(mid):
       ^^^^^^^^^^^^^^^
  File "/Users/$USER/Downloads/photoborder-main/text.py", line 96, in check_size
    font = ImageFont.load_default(font_size)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: load_default() takes 0 positional arguments but 1 was given

Running on:

image
> python --version
Python 3.11.5

Thanks.

Rustymage commented 2 days ago

Basic debugging:

-e causes me the problem. It runs fine if I exclude -e.

Changing font = ImageFont.load_default(font_size) to font = ImageFont.load_default() results in huge text but no errors.

Using guidance from here - https://github.com/python-pillow/Pillow/issues/2695#issuecomment-1047065437

I commented out font = ImageFont.load_default(font_size) from text.py and added

font_path = os.path.join(cv2.__path__[0],'qt','fonts','Andale Mono.ttf')
font = ImageFont.truetype(font_path, font_size)

This meant I also needed to import cv2

This limits me to the Andale Mono.ttf font but it works. I suspect there's a more elegant way to solve this but I ran out of time this AM - https://github.com/python-pillow/Pillow/issues/2695#issuecomment-1367094493

stevequinn commented 2 days ago

This is great, thanks for debugging and suggesting a fix! I ended up just using the font that ships with this app in the fonts dir.

Fix is up here: https://github.com/stevequinn/photoborder/commit/423f01dfb63fdfa651512f8740211acd3274a545.

If you have any other issues or ideas please feel free to keep posting :)

Rustymage commented 2 days ago

Thanks for fixing the elegant way 👍

Rustymage commented 2 days ago

I should note, for anyone who wants to change the font, find your .ttf folder on your device, locate a font that you like and change FONTNAME & BOLDFONTNAME in text.py - the font must have font type options, otherwise the text will be the same.