Jiang Xiaolan commited on
Commit
1cbf7e8
·
1 Parent(s): 3358ae6

reduce image size if too large

Browse files
Files changed (1) hide show
  1. app.py +52 -1
app.py CHANGED
@@ -13,6 +13,56 @@ logging.basicConfig(level=logging.ERROR)
13
  logger = logging.getLogger(__name__)
14
 
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  def clone_repo():
17
  # 从环境变量中获取 GitHub Token
18
  github_token = os.getenv('GH_TOKEN')
@@ -103,7 +153,8 @@ if clone_repo():
103
  pil_image = get_page_image(in_file, page_number)
104
  else:
105
  pil_image = Image.open(in_file).convert("RGB")
106
-
 
107
  text_rec = st.sidebar.button("認識開始")
108
 
109
  if pil_image is None:
 
13
  logger = logging.getLogger(__name__)
14
 
15
 
16
+ def resize_image_if_needed(pil_image, max_size_mb=1, max_edge_length=1024):
17
+ """
18
+ Detect the size of a PIL image, and if it exceeds 1MB or its long edge is larger than 1024 pixels,
19
+ reduce its size to a smaller size.
20
+
21
+ Args:
22
+ pil_image (PIL.Image.Image): The input PIL image.
23
+ max_size_mb (int): The maximum allowed size in megabytes.
24
+ max_edge_length (int): The maximum allowed length of the long edge in pixels.
25
+
26
+ Returns:
27
+ PIL.Image.Image: The resized PIL image.
28
+ """
29
+ # Convert image to bytes and check its size
30
+ img_byte_arr = io.BytesIO()
31
+ pil_image.save(img_byte_arr, format='JPEG')
32
+ img_size_mb = len(img_byte_arr.getvalue()) / (1024 * 1024)
33
+ print(f"Image size: {img_size_mb} MB")
34
+
35
+ # Check if the image size exceeds the maximum allowed size
36
+ if img_size_mb > max_size_mb or max(pil_image.size) > max_edge_length:
37
+ # Calculate the new size while maintaining the aspect ratio
38
+ aspect_ratio = pil_image.width / pil_image.height
39
+ if pil_image.width > pil_image.height:
40
+ new_width = min(max_edge_length, pil_image.width)
41
+ new_height = int(new_width / aspect_ratio)
42
+ else:
43
+ new_height = min(max_edge_length, pil_image.height)
44
+ new_width = int(new_height * aspect_ratio)
45
+
46
+ # Resize the image
47
+ pil_image = pil_image.resize((new_width, new_height), Image.LANCZOS)
48
+
49
+ # Convert the resized image to bytes and check its size again
50
+ img_byte_arr = io.BytesIO()
51
+ pil_image.save(img_byte_arr, format='JPEG')
52
+ img_size_mb = len(img_byte_arr.getvalue()) / (1024 * 1024)
53
+
54
+ # If the resized image still exceeds the maximum allowed size, reduce the quality
55
+ if img_size_mb > max_size_mb:
56
+ quality = 95
57
+ while img_size_mb > max_size_mb and quality > 10:
58
+ img_byte_arr = io.BytesIO()
59
+ pil_image.save(img_byte_arr, format='JPEG', quality=quality)
60
+ img_size_mb = len(img_byte_arr.getvalue()) / (1024 * 1024)
61
+ quality -= 5
62
+
63
+ return pil_image
64
+
65
+
66
  def clone_repo():
67
  # 从环境变量中获取 GitHub Token
68
  github_token = os.getenv('GH_TOKEN')
 
153
  pil_image = get_page_image(in_file, page_number)
154
  else:
155
  pil_image = Image.open(in_file).convert("RGB")
156
+ pil_image = resize_image_if_needed(pil_image)
157
+
158
  text_rec = st.sidebar.button("認識開始")
159
 
160
  if pil_image is None: