我目前正在使用python的re模块来搜索和捕获组.我列出了正则表达式,我必须编译并匹配导致性能问题的大型数据集.
Example:
REGEXES = [
'^New York(?P<grp1>\d+/\d+): (?P<grp2>.+)$',
'^Ohio (?P<grp1>\d+/\d+/\d+): (?P<grp2>.+)$',
'(?P<year>\d{4}-\d{1,2}-\d{1,2})$',
'^(?P<year>\d{1,2}/\d{1,2}/\d{2,4})$',
'^(?P<title>.+?)[- ]+E(?P<epi>\d+)$'
.
.
.
.
]
Run Code Online (Sandbox Code Playgroud)
注意:正则表达式不会相似
COMPILED_REGEXES = [re.compile(r, flags=re.I) for r in REGEXES]
def find_match(string):
for regex in COMPILED_REGEXES:
match = regex.search(string)
if not match:
continue
return match
Run Code Online (Sandbox Code Playgroud)
有没有解决的办法?我们的想法是避免通过编译的正则表达式进行迭代以获得匹配.
我有图像的宽度和高度。
img = Image.open(img_src)
width, height = img.size
font = ImageFont.truetype("MuseoSansCyrl_0.otf", 100)
text_w, text_h = draw.textsize(title, font)
Run Code Online (Sandbox Code Playgroud)
我试图找到一种通用的方法来在底部中间的图像中添加文本。
这是我写的函数:
def process_img(img_src, title, background):
img = Image.open(img_src, 'r')
draw = ImageDraw.Draw(img)
w, h = img.size
font = ImageFont.truetype("MuseoSansCyrl_0.otf", 100)
text_w, text_h = draw.textsize(title, font)
draw.text((REQ_WIDTH, REQ_HEIGHT), title, (255,255,255), font=font)
img.save(img_src)
return img_src
Run Code Online (Sandbox Code Playgroud)
有什么办法可以获得 REQ_WIDTH 和 REQ_HEIGHT 吗?
我正在尝试放大图像。
import numpy as np
from scipy.ndimage.interpolation import zoom
import Image
zoom_factor = 0.05 # 5% of the original image
img = Image.open(filename)
image_array = misc.fromimage(img)
zoomed_img = clipped_zoom(image_array, zoom_factor)
misc.imsave('output.png', zoomed_img)
Run Code Online (Sandbox Code Playgroud)
裁剪缩放参考:
Scipy旋转和缩放图像而无需更改其尺寸
这不起作用,并引发以下错误:
ValueError: could not broadcast input array from shape
关于此的任何帮助或建议是否有一种方法可以在给定缩放系数的情况下缩放图像。那是什么问题呢?
追溯:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/tornado/web.py", line 1443, in _execute
result = method(*self.path_args, **self.path_kwargs)
File "title_apis_proxy.py", line 798, in get
image, msg = resize_image(image_local_file, aspect_ratio, image_url, scheme, radius, sigma)
File "title_apis_proxy.py", line 722, in …Run Code Online (Sandbox Code Playgroud)