未验证 提交 4a91a212 编写于 作者: S shiyutang 提交者: GitHub

compat_pillow (#10596)

上级 b3f9f681
...@@ -17,6 +17,7 @@ https://github.com/zcswdt/Color_OCR_image_generator ...@@ -17,6 +17,7 @@ https://github.com/zcswdt/Color_OCR_image_generator
""" """
import os import os
import random import random
import PIL
from PIL import Image, ImageDraw, ImageFont from PIL import Image, ImageDraw, ImageFont
import json import json
import argparse import argparse
...@@ -55,8 +56,11 @@ def get_horizontal_text_picture(image_file, chars, fonts_list, cf): ...@@ -55,8 +56,11 @@ def get_horizontal_text_picture(image_file, chars, fonts_list, cf):
ch_w = [] ch_w = []
ch_h = [] ch_h = []
for ch in chars: for ch in chars:
left, top, right, bottom = font.getbbox(ch) if int(PIL.__version__.split('.')[0]) < 10:
wt, ht = right - left, bottom - top wt, ht = font.getsize(ch)
else:
left, top, right, bottom = font.getbbox(ch)
wt, ht = right - left, bottom - top
ch_w.append(wt) ch_w.append(wt)
ch_h.append(ht) ch_h.append(ht)
f_w = sum(ch_w) f_w = sum(ch_w)
...@@ -102,8 +106,11 @@ def get_vertical_text_picture(image_file, chars, fonts_list, cf): ...@@ -102,8 +106,11 @@ def get_vertical_text_picture(image_file, chars, fonts_list, cf):
ch_w = [] ch_w = []
ch_h = [] ch_h = []
for ch in chars: for ch in chars:
left, top, right, bottom = font.getbbox(ch) if int(PIL.__version__.split('.')[0]) < 10:
wt, ht = right - left, bottom - top wt, ht = font.getsize(ch)
else:
left, top, right, bottom = font.getbbox(ch)
wt, ht = right - left, bottom - top
ch_w.append(wt) ch_w.append(wt)
ch_h.append(ht) ch_h.append(ht)
f_w = max(ch_w) f_w = max(ch_w)
......
...@@ -18,6 +18,7 @@ import numpy as np ...@@ -18,6 +18,7 @@ import numpy as np
import random import random
import copy import copy
from PIL import Image from PIL import Image
import PIL
from .text_image_aug import tia_perspective, tia_stretch, tia_distort from .text_image_aug import tia_perspective, tia_stretch, tia_distort
from .abinet_aug import CVGeometry, CVDeterioration, CVColorJitter, SVTRGeometry, SVTRDeterioration from .abinet_aug import CVGeometry, CVDeterioration, CVColorJitter, SVTRGeometry, SVTRDeterioration
from paddle.vision.transforms import Compose from paddle.vision.transforms import Compose
...@@ -406,7 +407,7 @@ class GrayRecResizeImg(object): ...@@ -406,7 +407,7 @@ class GrayRecResizeImg(object):
def __init__(self, def __init__(self,
image_shape, image_shape,
resize_type, resize_type,
inter_type='Image.LANCZOS', inter_type="Image.Resampling.LANCZOS",
scale=True, scale=True,
padding=False, padding=False,
**kwargs): **kwargs):
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
import cv2 import cv2
import os import os
import numpy as np import numpy as np
import PIL
from PIL import Image, ImageDraw, ImageFont from PIL import Image, ImageDraw, ImageFont
...@@ -62,8 +63,13 @@ def draw_box_txt(bbox, text, draw, font, font_size, color): ...@@ -62,8 +63,13 @@ def draw_box_txt(bbox, text, draw, font, font_size, color):
draw.rectangle(bbox, fill=color) draw.rectangle(bbox, fill=color)
# draw ocr results # draw ocr results
left, top, right, bottom = font.getbbox(text) if int(PIL.__version__.split('.')[0]) < 10:
tw, th = right - left, bottom - top tw = font.getsize(text)[0]
th = font.getsize(text)[1]
else:
left, top, right, bottom = font.getbbox(text)
tw, th = right - left, bottom - top
start_y = max(0, bbox[0][1] - th) start_y = max(0, bbox[0][1] - th)
draw.rectangle( draw.rectangle(
[(bbox[0][0] + 1, start_y), (bbox[0][0] + tw + 1, start_y + th)], [(bbox[0][0] + 1, start_y), (bbox[0][0] + tw + 1, start_y + th)],
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
# limitations under the License. # limitations under the License.
import random import random
import ast import ast
import PIL
from PIL import Image, ImageDraw, ImageFont from PIL import Image, ImageDraw, ImageFont
import numpy as np import numpy as np
<<<<<<< HEAD <<<<<<< HEAD
...@@ -137,8 +138,13 @@ def draw_structure_result(image, result, font_path): ...@@ -137,8 +138,13 @@ def draw_structure_result(image, result, font_path):
[(box_layout[0], box_layout[1]), (box_layout[2], box_layout[3])], [(box_layout[0], box_layout[1]), (box_layout[2], box_layout[3])],
outline=box_color, outline=box_color,
width=3) width=3)
left, top, right, bottom = font.getbbox(region['type'])
text_w, text_h = right - left, bottom - top if int(PIL.__version__.split('.')[0]) < 10:
text_w, text_h = font.getsize(region['type'])
else:
left, top, right, bottom = font.getbbox(region['type'])
text_w, text_h = right - left, bottom - top
draw_layout.rectangle( draw_layout.rectangle(
[(box_layout[0], box_layout[1]), [(box_layout[0], box_layout[1]),
(box_layout[0] + text_w, box_layout[1] + text_h)], (box_layout[0] + text_w, box_layout[1] + text_h)],
......
...@@ -15,4 +15,4 @@ premailer ...@@ -15,4 +15,4 @@ premailer
openpyxl openpyxl
attrdict attrdict
PyMuPDF<1.21.0 PyMuPDF<1.21.0
Pillow>=10.0.0 Pillow
...@@ -158,7 +158,7 @@ class TextRecognizer(object): ...@@ -158,7 +158,7 @@ class TextRecognizer(object):
if self.rec_algorithm == 'ViTSTR': if self.rec_algorithm == 'ViTSTR':
img = image_pil.resize([imgW, imgH], Image.BICUBIC) img = image_pil.resize([imgW, imgH], Image.BICUBIC)
else: else:
img = image_pil.resize([imgW, imgH], Image.LANCZOS) img = image_pil.resize([imgW, imgH], Image.Resampling.LANCZOS)
img = np.array(img) img = np.array(img)
norm_img = np.expand_dims(img, -1) norm_img = np.expand_dims(img, -1)
norm_img = norm_img.transpose((2, 0, 1)) norm_img = norm_img.transpose((2, 0, 1))
......
...@@ -19,6 +19,7 @@ import platform ...@@ -19,6 +19,7 @@ import platform
import cv2 import cv2
import numpy as np import numpy as np
import paddle import paddle
import PIL
from PIL import Image, ImageDraw, ImageFont from PIL import Image, ImageDraw, ImageFont
import math import math
from paddle import inference from paddle import inference
...@@ -475,7 +476,11 @@ def draw_box_txt_fine(img_size, box, txt, font_path="./doc/fonts/simfang.ttf"): ...@@ -475,7 +476,11 @@ def draw_box_txt_fine(img_size, box, txt, font_path="./doc/fonts/simfang.ttf"):
def create_font(txt, sz, font_path="./doc/fonts/simfang.ttf"): def create_font(txt, sz, font_path="./doc/fonts/simfang.ttf"):
font_size = int(sz[1] * 0.99) font_size = int(sz[1] * 0.99)
font = ImageFont.truetype(font_path, font_size, encoding="utf-8") font = ImageFont.truetype(font_path, font_size, encoding="utf-8")
length = font.getlength(txt) if int(PIL.__version__.split('.')[0]) < 10:
length = font.getsize(txt)[0]
else:
length = font.getlength(txt)
if length > sz[0]: if length > sz[0]:
font_size = int(font_size * sz[0] / length) font_size = int(font_size * sz[0] / length)
font = ImageFont.truetype(font_path, font_size, encoding="utf-8") font = ImageFont.truetype(font_path, font_size, encoding="utf-8")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册