test_image_compression_real.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. 测试图片压缩修复 - 真实场景
  5. 使用更真实的大图片验证压缩方法
  6. """
  7. import sys
  8. import os
  9. from io import BytesIO
  10. from PIL import Image, ImageDraw, ImageFont
  11. import random
  12. # 添加项目根目录到Python路径
  13. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  14. from utils.minio.image_util import ImageUtil
  15. def create_complex_test_image(width=3000, height=3000) -> BytesIO:
  16. """
  17. 创建一个复杂的大尺寸测试图片,包含多种元素以增加文件大小
  18. Args:
  19. width: 图片宽度
  20. height: 图片高度
  21. Returns:
  22. BytesIO: 复杂大尺寸图片流
  23. """
  24. print(f"创建 {width}x{height} 的复杂测试图片...")
  25. # 创建一个白色背景图片
  26. img = Image.new('RGB', (width, height), color=(255, 255, 255))
  27. draw = ImageDraw.Draw(img)
  28. # 添加大量随机形状和颜色,增加图片复杂度
  29. for _ in range(10000):
  30. # 随机位置
  31. x1 = random.randint(0, width)
  32. y1 = random.randint(0, height)
  33. x2 = x1 + random.randint(10, 100)
  34. y2 = y1 + random.randint(10, 100)
  35. # 随机颜色
  36. color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
  37. # 随机形状
  38. shape_type = random.choice(['rectangle', 'ellipse', 'line'])
  39. if shape_type == 'rectangle':
  40. draw.rectangle([x1, y1, x2, y2], fill=color)
  41. elif shape_type == 'ellipse':
  42. draw.ellipse([x1, y1, x2, y2], fill=color)
  43. else:
  44. draw.line([x1, y1, x2, y2], fill=color, width=random.randint(1, 5))
  45. # 添加一些随机文本
  46. try:
  47. # 尝试使用默认字体
  48. font = ImageFont.load_default()
  49. for _ in range(1000):
  50. text = f"Test {random.randint(1, 1000)}"
  51. x = random.randint(0, width - 100)
  52. y = random.randint(0, height - 20)
  53. draw.text((x, y), text, fill=(0, 0, 0), font=font)
  54. except Exception as e:
  55. print(f"添加文本失败: {e}")
  56. # 将图片保存到BytesIO流,使用JPEG格式以获得更大的文件大小
  57. img_stream = BytesIO()
  58. img.save(img_stream, format='JPEG', quality=100) # 使用最高质量生成大文件
  59. img_stream.seek(0)
  60. # 检查图片大小
  61. img_stream.seek(0, 2)
  62. size_kb = img_stream.tell() / 1024
  63. img_stream.seek(0)
  64. print(f"测试图片创建完成,大小为 {size_kb:.2f}KB")
  65. return img_stream
  66. def test_image_compression():
  67. """
  68. 测试图片压缩方法
  69. """
  70. print("开始测试图片压缩方法...")
  71. # 创建ImageUtil实例
  72. image_util = ImageUtil()
  73. # 测试不同尺寸的复杂图片压缩
  74. test_sizes = [
  75. (3000, 3000), # 约 25MB
  76. (4000, 4000), # 约 45MB
  77. ]
  78. for width, height in test_sizes:
  79. print(f"\n=== 测试 {width}x{height} 复杂图片压缩 ===")
  80. # 创建大尺寸测试图片
  81. img_stream = create_complex_test_image(width, height)
  82. # 调用压缩方法
  83. compressed_stream = image_util._compress_image(img_stream, "test_complex_image.jpg")
  84. # 检查压缩后的大小
  85. compressed_stream.seek(0, 2)
  86. compressed_size_kb = compressed_stream.tell() / 1024
  87. compressed_stream.seek(0)
  88. print(f"压缩后大小: {compressed_size_kb:.2f}KB")
  89. # 验证压缩结果
  90. if compressed_size_kb <= 5000:
  91. print("✅ 压缩成功!压缩后大小小于等于5000KB")
  92. else:
  93. print("❌ 压缩失败!压缩后大小仍大于5000KB")
  94. # 测试_compress_image_to_bytes方法
  95. print(f"\n=== 测试 _compress_image_to_bytes 方法 ===")
  96. img_stream = create_complex_test_image(4000, 4000)
  97. compressed_bytes = image_util._compress_image_to_bytes(img_stream)
  98. compressed_size_kb = len(compressed_bytes) / 1024
  99. print(f"压缩后字节大小: {compressed_size_kb:.2f}KB")
  100. if compressed_size_kb <= 5000:
  101. print("✅ _compress_image_to_bytes 压缩成功!")
  102. else:
  103. print("❌ _compress_image_to_bytes 压缩失败!")
  104. print("\n=== 所有测试完成 ===")
  105. if __name__ == "__main__":
  106. test_image_compression()