Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def on_site_startup(self, site):
if self.header_height:
for ext in ('jpg', 'png'):
fn = site.confdirs.find_config_file("top-right."+ext, "weasyprint")
if fn:
self.top_right_image = fn
w, h = imagesize.get(fn)
if self.top_right_width is None:
self.top_right_width = self.header_height * w / h
fn = site.confdirs.find_config_file("header."+ext, "weasyprint")
if fn:
self.header_image = fn
super(Plugin, self).on_site_startup(site)
def get_image_size(filename: str) -> Tuple[int, int]:
try:
size = imagesize.get(filename)
if size[0] == -1:
size = None
if size is None and Image: # fallback to Pillow
im = Image.open(filename)
size = im.size
try:
im.fp.close()
except Exception:
pass
return size
except Exception:
return None
def show_image_aspect_ratio_distr(self):
"""得到样本长宽比
"""
aspect_ratio_dict = {}
image_names = self.get_image_names()
for image_name in image_names:
sample_path = os.path.join(self.data_root, image_name)
width, height = imagesize.get(sample_path)
aspect_ratio = width/height
if aspect_ratio in aspect_ratio_dict:
aspect_ratio_dict[aspect_ratio] += 1
else:
aspect_ratio_dict[aspect_ratio] = 0
aspect_ratio_dict_filt = {}
for key, value in aspect_ratio_dict.items():
if value > 100:
aspect_ratio_dict_filt[key] = value
del aspect_ratio_dict
plt.bar(aspect_ratio_dict_filt.keys(), aspect_ratio_dict_filt.values())
plt.show()
def error_result(error_text):
return (None, TRResult(path = file_path, data = {}, text = '',
error = error_text, boxes = []))
if not readable(file_path):
return error_result('Unable to read file: {}'.format(file_path))
if __debug__: log('reading image file {} for {}', file_path, self.name())
with open(file_path, 'rb') as image_file:
image = image_file.read()
if len(image) == 0:
return error_result('Empty file: {}'.format(file_path))
if len(image) > self.max_size():
text = 'Exceeds {} byte limit for service: {}'.format(self.max_size(), file_path)
return error_result(text)
width, height = imagesize.get(file_path)
if __debug__: log('image size is width = {}, height = {}', width, height)
if self.max_dimensions():
max_width, max_height = self.max_dimensions()
if width > max_width or height > max_height:
text = 'Image dimensions {}x{} exceed {} limits: {}'.format(
width, height, self.name(), file_path)
return error_result(text)
return (image, None)
return TRResult(path = file_path, data = {}, boxes = [],
text = '', error = error)
if __debug__: log('setting up Amazon client function "{}"', variant)
creds = self._credentials
try:
session = boto3.session.Session()
client = session.client(variant, region_name = creds['region_name'],
aws_access_key_id = creds['aws_access_key_id'],
aws_secret_access_key = creds['aws_secret_access_key'])
if __debug__: log('calling Amazon API function')
response = getattr(client, api_method)( **{ image_keyword : {'Bytes': image} })
if __debug__: log('received {} blocks', len(response[response_key]))
full_text = ''
boxes = []
width, height = imagesize.get(file_path)
for block in response[response_key]:
if value_key in block and block[value_key] == "WORD":
text = block[block_key]
full_text += (text + ' ')
corners = corner_list(block['Geometry']['Polygon'], width, height)
if corners:
boxes.append(TextBox(boundingBox = corners, text = text))
else:
# Something's wrong with the vertex list. Skip & continue.
if __debug__: log('bad bb for {}: {}', text, bb)
return TRResult(path = file_path, data = response, boxes = boxes,
text = full_text, error = None)
except KeyboardInterrupt as ex:
raise
except Exception as ex: