Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewheiss committed Apr 9, 2015
0 parents commit 8b16ecc
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 0 deletions.
Binary file added SourceSansPro-Light.otf
Binary file not shown.
35 changes: 35 additions & 0 deletions timer_gif.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Libraries
library(animation)
library(dplyr)
library(lubridate)
library(ggplot2)

# Blank ggplot theme
blank_theme <- theme_bw() +
theme(line = element_blank(), rect = element_blank(),
strip.text = element_blank(), axis.text = element_blank(),
plot.title = element_blank(), axis.title = element_blank(),
plot.margin = structure(c(0, 0, -1, -1), unit = "lines",
valid.unit = 3L, class = "unit"))

# Create data frame of all the times to display
df <- data_frame(timer = rev(format(seq(ymd_hms("2015-04-08 00:00:00"),
ymd_hms("2015-04-08 00:00:00") + minutes(1),
by="sec"), "%M:%S")))

# Start recording
ani.record(reset=TRUE)

# Plot each time as text
for (i in 1:nrow(df)) {
p <- ggplot(slice(df, i), aes(x=1, y=1, label=timer)) +
geom_text(size=50, family="Source Sans Pro Light") +
blank_theme
print(p)
ani.record()
}

# Save final animation
# TODO: Looping doesn't work (maybe an ImageMagick/GIF problem though)
saveGIF(ani.replay(), interval=1, movie.name="timer_r.gif",
ani.width=600, ani.height=400, loop=1)
53 changes: 53 additions & 0 deletions timer_gif.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env python3
# `brew install imagemagick --with-x11` to support `display` and `animate` commands
# Verify x11 support with `convert --version`
from wand.image import Image
from wand.drawing import Drawing
from wand.color import Color

# Helpful links and documentation
# http://docs.wand-py.org/en/0.4.0/guide/sequence.html
# http://stackoverflow.com/questions/16544399/wand-python-multi-size-icon

# TODO: Transparency doesn't work---previous images are overlaid and not antialiased
# TODO: Make the gif not loop
# MAYBE: Make font red for last 10ish seconds?

# Timer settings
filename = "timer_python.gif"
bg_color = "white"
seconds = 1 * 60

# Generate time labels
def nice_time(seconds):
m, s = divmod(seconds, 60)
# m can be subdivided to hours if needed:
# h, m = divmod(m, 60)
return("{0:02d}:{1:02d}".format(m, s))

# from datetime import timedelta
# lambda x: str(timedelta(seconds=x)) works great, but it doesn't let you
# format the string however you want

labels = list(map(nice_time, reversed(range(seconds + 1))))

with Image() as gif:
for label in labels:
with Drawing() as draw:
draw.font = "SourceSansPro-Light.otf"
draw.font_size = 140
draw.text_alignment = "center"
draw.text_antialias = True

with Image(width=600, height=400, background=Color(bg_color)) as img:
x = int(img.width / 2)
y = int(img.height / 2)
draw.text(x, y, label)
draw.draw(img)
gif.sequence.append(img)

for frame in gif.sequence:
with frame:
frame.delay = 100 # Centiseconds

gif.save(filename=filename)
Binary file added timer_python.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added timer_r.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8b16ecc

Please sign in to comment.