__init__.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from pathlib import Path
  15. import PIL
  16. from PIL import ImageFont
  17. from .. import logging
  18. from ..cache import CACHE_DIR
  19. from ..download import download
  20. from ..flags import LOCAL_FONT_FILE_PATH
  21. def get_font_file_path(file_name: str) -> str:
  22. """
  23. Get the path of the font file.
  24. Returns:
  25. str: The path to the font file.
  26. """
  27. font_path = (Path(CACHE_DIR) / "fonts" / file_name).resolve().as_posix()
  28. if not Path(font_path).is_file():
  29. download(
  30. url=f"https://paddle-model-ecology.bj.bcebos.com/paddlex/PaddleX3.0/fonts/{file_name}",
  31. save_path=font_path,
  32. )
  33. return font_path
  34. def create_font(txt: str, sz: tuple, font_path: str) -> ImageFont:
  35. """
  36. Create a font object with specified size and path, adjusted to fit within the given image region.
  37. Parameters:
  38. txt (str): The text to be rendered with the font.
  39. sz (tuple): A tuple containing the height and width of an image region, used for font size.
  40. font_path (str): The path to the font file.
  41. Returns:
  42. ImageFont: An ImageFont object adjusted to fit within the given image region.
  43. """
  44. font_size = int(sz[1] * 0.8)
  45. font = ImageFont.truetype(font_path, font_size, encoding="utf-8")
  46. if int(PIL.__version__.split(".")[0]) < 10:
  47. length = font.getsize(txt)[0]
  48. else:
  49. length = font.getlength(txt)
  50. if length > sz[0]:
  51. font_size = int(font_size * sz[0] / length)
  52. font = ImageFont.truetype(font_path, font_size, encoding="utf-8")
  53. return font
  54. def create_font_vertical(
  55. txt: str, sz: tuple, font_path: str, scale=1.2
  56. ) -> ImageFont.FreeTypeFont:
  57. n = len(txt) if len(txt) > 0 else 1
  58. base_font_size = int(sz[1] / n * 0.8 * scale)
  59. base_font_size = max(base_font_size, 10)
  60. font = ImageFont.truetype(font_path, base_font_size, encoding="utf-8")
  61. if int(PIL.__version__.split(".")[0]) < 10:
  62. max_char_width = max([font.getsize(c)[0] for c in txt])
  63. else:
  64. max_char_width = max([font.getlength(c) for c in txt])
  65. if max_char_width > sz[0]:
  66. new_size = int(base_font_size * sz[0] / max_char_width)
  67. new_size = max(new_size, 10)
  68. font = ImageFont.truetype(font_path, new_size, encoding="utf-8")
  69. return font
  70. if Path(str(LOCAL_FONT_FILE_PATH)).is_file():
  71. logging.warning(
  72. f"Using the local font file(`{LOCAL_FONT_FILE_PATH}`) specified by `LOCAL_FONT_FILE_PATH`!"
  73. )
  74. PINGFANG_FONT_FILE_PATH = LOCAL_FONT_FILE_PATH
  75. SIMFANG_FONT_FILE_PATH = LOCAL_FONT_FILE_PATH
  76. else:
  77. PINGFANG_FONT_FILE_PATH = get_font_file_path("PingFang-SC-Regular.ttf")
  78. SIMFANG_FONT_FILE_PATH = get_font_file_path("simfang.ttf")