__init__.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 ..download import download
  19. from ..flags import LOCAL_FONT_FILE_PATH
  20. def get_font_file_path(file_name: str) -> str:
  21. """
  22. Get the path of the font file.
  23. Returns:
  24. str: The path to the font file.
  25. """
  26. font_path = (Path(__file__).parent / file_name).resolve().as_posix()
  27. if not Path(font_path).is_file():
  28. download(
  29. url=f"https://paddle-model-ecology.bj.bcebos.com/paddlex/PaddleX3.0/fonts/{file_name}",
  30. save_path=font_path,
  31. )
  32. return font_path
  33. def create_font(txt: str, sz: tuple, font_path: str) -> ImageFont:
  34. """
  35. Create a font object with specified size and path, adjusted to fit within the given image region.
  36. Parameters:
  37. txt (str): The text to be rendered with the font.
  38. sz (tuple): A tuple containing the height and width of an image region, used for font size.
  39. font_path (str): The path to the font file.
  40. Returns:
  41. ImageFont: An ImageFont object adjusted to fit within the given image region.
  42. """
  43. font_size = int(sz[1] * 0.8)
  44. font = ImageFont.truetype(font_path, font_size, encoding="utf-8")
  45. if int(PIL.__version__.split(".")[0]) < 10:
  46. length = font.getsize(txt)[0]
  47. else:
  48. length = font.getlength(txt)
  49. if length > sz[0]:
  50. font_size = int(font_size * sz[0] / length)
  51. font = ImageFont.truetype(font_path, font_size, encoding="utf-8")
  52. return font
  53. def create_font_vertical(
  54. txt: str, sz: tuple, font_path: str, scale=1.2
  55. ) -> ImageFont.FreeTypeFont:
  56. n = len(txt) if len(txt) > 0 else 1
  57. base_font_size = int(sz[1] / n * 0.8 * scale)
  58. base_font_size = max(base_font_size, 10)
  59. font = ImageFont.truetype(font_path, base_font_size, encoding="utf-8")
  60. if int(PIL.__version__.split(".")[0]) < 10:
  61. max_char_width = max([font.getsize(c)[0] for c in txt])
  62. else:
  63. max_char_width = max([font.getlength(c) for c in txt])
  64. if max_char_width > sz[0]:
  65. new_size = int(base_font_size * sz[0] / max_char_width)
  66. new_size = max(new_size, 10)
  67. font = ImageFont.truetype(font_path, new_size, encoding="utf-8")
  68. return font
  69. if Path(str(LOCAL_FONT_FILE_PATH)).is_file():
  70. logging.warning(
  71. f"Using the local font file(`{LOCAL_FONT_FILE_PATH}`) specified by `LOCAL_FONT_FILE_PATH`!"
  72. )
  73. PINGFANG_FONT_FILE_PATH = LOCAL_FONT_FILE_PATH
  74. SIMFANG_FONT_FILE_PATH = LOCAL_FONT_FILE_PATH
  75. else:
  76. PINGFANG_FONT_FILE_PATH = get_font_file_path("PingFang-SC-Regular.ttf")
  77. SIMFANG_FONT_FILE_PATH = get_font_file_path("simfang.ttf")