Skip to content

Instantly share code, notes, and snippets.

@chexov
Forked from mcsquaredjr/sysml_frame.py
Created September 9, 2013 17:07
Show Gist options
  • Save chexov/6498583 to your computer and use it in GitHub Desktop.
Save chexov/6498583 to your computer and use it in GitHub Desktop.
import Image, ImageDraw, ImageFont
import clipboard
# Simple script to create SysML frame around the image
# Define non-configurable options here
H_TEXT_OFFSET = 20
V_TEXT_OFFSET = 20
H_CUT_OUT = 20
V_CUT_OUT = 20
def draw_frame(img, txt, border=10, fontsize=20):
# Draw frame on img with border width=border (default width is 10 px)
# Get image size
try:
(width, height) = img.size
except AttributeError:
print 'No image in the clipboard.'
sys.exit()
draw = ImageDraw.Draw(img)
# Draw frame around image
draw.rectangle([(border, border), (width-border, height-border)],
outline='#000000')
# Configure font
font = ImageFont.truetype("Verdana", fontsize)
# Compute sizes
(tw, th) = draw.textsize(txt, font)
draw.text((border + H_TEXT_OFFSET, border + V_TEXT_OFFSET),
txt,
font=font, fill='#000000')
# Cutout width
fw = tw + 2*V_TEXT_OFFSET
fh = th + 2*H_TEXT_OFFSET
# Make outline arounf the text
draw.polygon([(border, border), (border, fh + border),
(fw - V_CUT_OUT + border, fh + border),
(fw + border, fh - H_CUT_OUT + border),
(fw + border, 0 + border)],
outline='#000000')
# Create a new image object for the output image
f_img = Image.new("RGBA", (width, height), (0,0,0,0))
del draw
# Paste the cropped image onto the new image
f_img.paste(img, (0, 0))
return f_img
def main():
# Run everything
img = clipboard.get_image()
txt = raw_input('Enter title: ')
f_img = draw_frame(img, txt, fontsize=20)
f_img.show()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment