Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def error_image(msg, width=420, height=600):
"""Creates an error frontpage"""
width, height = int(width), int(height)
img = WandImage(width=width, height=height)
with Drawing() as draw:
draw.text_alignment = 'center'
draw.text(img.width // 2, img.height // 3, msg)
draw(img)
background = WandImage(
width=img.width,
height=img.height,
background=Color('white'),
)
background.format = 'jpeg'
background.composite(img, 0, 0)
return background
def create_image_search(image_name, saved_image, text):
"""Create an image with the given string"""
with Image(filename=image_name) as img:
with img.clone() as cloned:
cloned.format = 'png'
with Drawing() as draw:
draw.font_family = 'Liberation Mono'
draw.font_size = 30
text = SpecialActions.simplifying(text)
draw.text(140, 785, text)
draw(cloned)
cloned.save(filename=saved_image)
subtract_top = int(difference/2)
subtract_bot = difference - subtract_top
self.img = self.img[subtract_top:-subtract_bot,:]
elif self.height() < self.width():
difference = self.width() - self.height()
subtract_left = int(difference/2)
subtract_right = difference - subtract_left
self.img = self.img[:,subtract_left:-subtract_right]
self.output_scale = float(output_size) / self.width()
self.canvas = Image(width = output_size,
height = output_size,
background = Color('white'))
self.canvas.format = 'png'
self.draw = Drawing()
self.recursive_draw(0, 0, self.width(), self.height())
self.draw(self.canvas)
def createKeyboardImage(physicalKeys, modifiers, source, imageDevices, biggestFontSize, displayGroups, runId, public):
config = Config(runId)
filePath = config.pathWithNameAndSuffix(source, '.jpg')
# See if it already exists or if we need to recreate it
if filePath.exists():
return True
with Image(filename='../res/' + source + '.jpg') as sourceImg:
with Drawing() as context:
# Defaults for the font
context.font = getFontPath('Regular', 'Normal')
context.text_antialias = True
context.font_style = 'normal'
context.stroke_width = 0
context.fill_color = Color('Black')
context.fill_opacity = 1
# Add the ID to the title
writeUrlToDrawing(config, context, public)
outputs = {}
for group in displayGroups:
outputs[group] = {}
def createHOTASImage(physicalKeys, modifiers, source, imageDevices, biggestFontSize, config, public, styling, deviceIndex, misconfigurationWarnings):
# Set up the path for our file
runId = config.name
if deviceIndex == 0:
name = source
else:
name = '%s-%s' % (source, deviceIndex)
filePath = config.pathWithNameAndSuffix(name, '.jpg')
# See if it already exists or if we need to recreate it
if filePath.exists():
return True
with Image(filename='../res/' + source + '.jpg') as sourceImg:
with Drawing() as context:
# Defaults for the font
context.font = getFontPath('Regular', 'Normal')
context.text_antialias = True
context.font_style = 'normal'
context.stroke_width = 0
context.fill_color = Color('Black')
context.fill_opacity = 1
# Add the ID to the title
writeUrlToDrawing(config, context, public)
for physicalKeySpec, physicalKey in physicalKeys.items():
itemDevice = physicalKey.get('Device')
itemDeviceIndex = int(physicalKey.get('DeviceIndex'))
itemKey = physicalKey.get('Key')
Displays each of the bounding boxes passed in 'boxes' on images of the pdf
pointed to by pdf_file
boxes is a list of 5-tuples (page, top, left, bottom, right)
"""
imgs = []
colors = [Color("blue"), Color("red")]
boxes_per_page: DefaultDict[int, int] = defaultdict(int)
boxes_by_page: DefaultDict[int, List[Tuple[int, int, int, int]]] = defaultdict(
list
)
for i, (page, top, left, bottom, right) in enumerate(boxes):
boxes_per_page[page] += 1
boxes_by_page[page].append((top, left, bottom, right))
for i, page_num in enumerate(boxes_per_page.keys()):
img = pdf_to_img(pdf_file, page_num)
draw = Drawing()
draw.fill_color = Color("rgba(0, 0, 0, 0.0)")
for j, (top, left, bottom, right) in enumerate(boxes_by_page[page_num]):
draw.stroke_color = colors[j % 2] if alternate_colors else colors[0]
draw.rectangle(left=left, top=top, right=right, bottom=bottom)
draw(img)
imgs.append(img)
return imgs
def get_text_width(self, text):
"""
:rtype: int
"""
with Drawing() as painter:
painter.font = self.font
painter.font_size = self.font_size
font_metrics = painter.get_font_metrics(self._canvas, text=text)
return font_metrics.text_width
market_cap = share['market_cap']
year_low = share['year_low']
year_high = share['year_high']
day_low = share['day_low']
day_high = share['day_high']
volume = share['volume']
pe_ratio = share['pe_ratio']
with Color('white') as bg:
with Image(width=WIDTH, height=HEIGHT, background=bg) as img:
with Image(filename=chart_file) as chart_img:
w = int(chart_img.size[0] * ZOOM_MULTIPLE)
h = int(chart_img.size[1] * ZOOM_MULTIPLE)
with Drawing() as draw:
draw.font = 'fonts/arial.ttf'
# Draw watermark
draw.fill_color = Color(BG_GREEN)
draw.rectangle(left=0, top=HEIGHT-WATERMARK_HEIGHT, right=WIDTH, bottom=HEIGHT)
draw(img)
draw.font_size = 20
draw.fill_color = Color(WHITE)
draw.text(WIDTH-150, HEIGHT-WATERMARK_HEIGHT+30, '@slashStock')
# Draw the chart
draw.composite(operator='add', left=0, top=0,
width=w, height=h, image=chart_img)
# Draw current price