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

fix issue for orientation

Browse files
Files changed (1) hide show
  1. app.py +37 -1
app.py CHANGED
@@ -4,7 +4,7 @@ import subprocess
4
  import streamlit as st
5
  import io
6
  import pypdfium2
7
- from PIL import Image
8
  import logging
9
 
10
  # 设置日志记录器
@@ -63,6 +63,41 @@ def resize_image_if_needed(pil_image, max_size_mb=1, max_edge_length=1024):
63
  return pil_image
64
 
65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  def clone_repo():
67
  # 从环境变量中获取 GitHub Token
68
  github_token = os.getenv('GH_TOKEN')
@@ -153,6 +188,7 @@ if clone_repo():
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("認識開始")
 
4
  import streamlit as st
5
  import io
6
  import pypdfium2
7
+ from PIL import Image, ExifTags
8
  import logging
9
 
10
  # 设置日志记录器
 
63
  return pil_image
64
 
65
 
66
+ def correct_image_orientation(pil_image):
67
+ """
68
+ 自动检测PIL Image对象是否包含EXIF信息,如果包含则根据EXIF信息重新修改图片的朝向。
69
+
70
+ :param pil_image: 输入的PIL Image对象
71
+ :return: 返回修正后的PIL Image对象
72
+ """
73
+ try:
74
+ # 获取EXIF信息
75
+ exif = pil_image._getexif()
76
+ if exif is not None:
77
+ # 查找Orientation的EXIF标签编号
78
+ for orientation in ExifTags.TAGS.keys():
79
+ if ExifTags.TAGS[orientation] == 'Orientation':
80
+ break
81
+
82
+ # 获取图片的朝向信息
83
+ orientation_value = exif.get(orientation)
84
+ print(f"Orientation value: {orientation_value}")
85
+
86
+ # 根据朝向信息调整图片方向
87
+ if orientation_value == 3:
88
+ pil_image = pil_image.rotate(180, expand=True)
89
+ elif orientation_value == 6:
90
+ pil_image = pil_image.rotate(270, expand=True)
91
+ elif orientation_value == 8:
92
+ pil_image = pil_image.rotate(90, expand=True)
93
+
94
+ except (AttributeError, KeyError, IndexError):
95
+ # 如果没有EXIF信息或者没有朝向信息,跳过处理
96
+ pass
97
+
98
+ return pil_image
99
+
100
+
101
  def clone_repo():
102
  # 从环境变量中获取 GitHub Token
103
  github_token = os.getenv('GH_TOKEN')
 
188
  pil_image = get_page_image(in_file, page_number)
189
  else:
190
  pil_image = Image.open(in_file).convert("RGB")
191
+ pil_image = correct_image_orientation(pil_image)
192
  pil_image = resize_image_if_needed(pil_image)
193
 
194
  text_rec = st.sidebar.button("認識開始")