annotate_cell.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. from PIL import Image, ImageDraw, ImageFont
  2. import os
  3. def annotate_page(page_num, img_path, annotations, output_path):
  4. img = Image.open(img_path)
  5. draw = ImageDraw.Draw(img)
  6. # Try to use a Chinese font
  7. font_paths = [
  8. 'C:/Windows/Fonts/simhei.ttf',
  9. 'C:/Windows/Fonts/simsun.ttc',
  10. ]
  11. font_path = None
  12. for fp in font_paths:
  13. if os.path.exists(fp):
  14. font_path = fp
  15. break
  16. if font_path:
  17. try:
  18. font = ImageFont.truetype(font_path, 16)
  19. font_small = ImageFont.truetype(font_path, 13)
  20. except:
  21. font = ImageFont.load_default()
  22. font_small = ImageFont.load_default()
  23. else:
  24. font = ImageFont.load_default()
  25. font_small = ImageFont.load_default()
  26. for rect, text in annotations:
  27. # Draw red rectangle
  28. draw.rectangle(rect, outline='red', width=4)
  29. # Draw annotation text above the rectangle
  30. if text:
  31. draw.text((rect[0], rect[1] - 22), text, fill='red', font=font_small)
  32. img.save(output_path)
  33. print(f'Saved: {output_path}')
  34. # Page 2: Summary section
  35. annotate_page(
  36. 2,
  37. 'D:/workspace/cfc/web/img/ref-screenshots/cell-inflammation-p2.png',
  38. [
  39. # SUMMARY box (around y=350-700)
  40. ((60, 340, 1640, 700),
  41. 'SUMMARY: 肥胖导致慢性全身性炎症,引起胰岛素抵抗(IR) -> \u03b2细胞功能障碍 -> 2型糖尿病(T2D)'),
  42. # Paragraph about macrophages being the ultimate driver
  43. ((60, 720, 1640, 940),
  44. '促炎巨噬细胞在代谢组织中的募集和活化,是慢性低度炎症的终极驱动力')
  45. ],
  46. 'D:/workspace/cfc/web/img/ref-screenshots/cell-inflammation-p2-annotated.png'
  47. )
  48. # Page 3: Immunometabolism
  49. annotate_page(
  50. 3,
  51. 'D:/workspace/cfc/web/img/ref-screenshots/cell-inflammation-p3.png',
  52. [
  53. # Immunometabolism term
  54. ((60, 110, 1640, 410),
  55. '"免疫代谢"(immunometabolism):炎症与代谢缺陷之间的相互作用'),
  56. # Drug limitation
  57. ((60, 440, 1640, 700),
  58. '所有降糖药中仅3类能预防并发症;除二甲双胍外,其他药物均不能延缓糖尿病进展')
  59. ],
  60. 'D:/workspace/cfc/web/img/ref-screenshots/cell-inflammation-p3-annotated.png'
  61. )
  62. # Page 4: Inflammatory signaling mechanism
  63. annotate_page(
  64. 4,
  65. 'D:/workspace/cfc/web/img/ref-screenshots/cell-inflammation-p4.png',
  66. [
  67. # TNF-alpha and IKK/NF-kB pathway
  68. ((60, 100, 1640, 420),
  69. 'TNF-\u03b1 激活 JNK 和 IKK\u03b2 -> NF-\u03baB 核转位 -> 促炎细胞因子增加'),
  70. # Inflammasome
  71. ((60, 440, 1640, 700),
  72. '炎症小体(inflammasome)被葡萄糖和游离脂肪酸激活 -> 产生活性 IL-1\u03b2')
  73. ],
  74. 'D:/workspace/cfc/web/img/ref-screenshots/cell-inflammation-p4-annotated.png'
  75. )
  76. # Page 5: Macrophage polarization in obese vs lean
  77. annotate_page(
  78. 5,
  79. 'D:/workspace/cfc/web/img/ref-screenshots/cell-inflammation-p5.png',
  80. [
  81. # M1-like macrophages in obesity
  82. ((60, 100, 1640, 380),
  83. '肥胖时巨噬细胞向促炎M1样极化 -> 释放促炎细胞因子 -> 降低胰岛素信号'),
  84. # Omega-3 inhibits inflammation
  85. ((60, 400, 1640, 660),
  86. 'Omega-3脂肪酸(GPR120受体)抑制M1样极化;饱和脂肪酸(TLR4)促进M1样极化'),
  87. # Crown-like structures
  88. ((60, 680, 1640, 900),
  89. '肥胖巨噬细胞聚集在萎缩脂肪细胞周围形成"冠状结构"')
  90. ],
  91. 'D:/workspace/cfc/web/img/ref-screenshots/cell-inflammation-p5-annotated.png'
  92. )
  93. print('All annotations done.')