Skip to content

Commit 0463de8

Browse files
Add global flag --transparent-bg to generate output images with transparent background
Signed-off-by: Jacob Stopak <jacob@initialcommit.io>
1 parent 478ff64 commit 0463de8

File tree

4 files changed

+38
-10
lines changed

4 files changed

+38
-10
lines changed

git_sim/__main__.py

+9
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ def main(
3939
"--light-mode",
4040
help="Enable light-mode with white background",
4141
),
42+
transparent_bg: bool = typer.Option(
43+
settings.transparent_bg,
44+
"--transparent-bg",
45+
help="Make background transparent",
46+
),
4247
logo: pathlib.Path = typer.Option(
4348
settings.logo,
4449
help="The path to a custom logo to use in the animation intro/outro",
@@ -138,6 +143,7 @@ def main(
138143
settings.auto_open = auto_open
139144
settings.img_format = img_format
140145
settings.light_mode = light_mode
146+
settings.transparent_bg = transparent_bg
141147
settings.logo = logo
142148
settings.low_quality = low_quality
143149
settings.max_branches_per_commit = max_branches_per_commit
@@ -183,6 +189,9 @@ def main(
183189
if settings.light_mode:
184190
config.background_color = WHITE
185191

192+
if settings.transparent_bg:
193+
settings.img_format = ImgFormat.png
194+
186195
t = datetime.datetime.fromtimestamp(time.time()).strftime("%m-%d-%y_%H-%M-%S")
187196
config.output_file = "git-sim-" + ctx.invoked_subcommand + "_" + t + ".mp4"
188197

git_sim/animations.py

+12
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ def handle_animations(scene: Scene) -> None:
4646
image_file_path = os.path.join(
4747
os.path.join(settings.media_dir, "images"), image_file_name
4848
)
49+
if settings.transparent_bg:
50+
unsharp_image = cv2.GaussianBlur(image, (0, 0), 3)
51+
image = cv2.addWeighted(image, 1.5, unsharp_image, -0.5, 0)
52+
53+
tmp = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
54+
if settings.light_mode:
55+
_, alpha = cv2.threshold(tmp, 225, 255, cv2.THRESH_BINARY_INV)
56+
else:
57+
_, alpha = cv2.threshold(tmp, 25, 255, cv2.THRESH_BINARY)
58+
b, g, r = cv2.split(image)
59+
rgba = [b, g, r, alpha]
60+
image = cv2.merge(rgba, 4)
4961
cv2.imwrite(image_file_path, image)
5062
if (
5163
not settings.stdout

git_sim/git_sim_base_command.py

+16-10
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ def __init__(self):
5353
self.logo = m.ImageMobject(settings.logo)
5454
self.logo.width = 3
5555

56+
self.fill_opacity = 0.25
57+
self.ref_fill_opacity = 0.25
58+
if settings.transparent_bg:
59+
self.fill_opacity = 0.5
60+
self.ref_fill_opacity = 1.0
61+
5662
def init_repo(self):
5763
try:
5864
self.repo = Repo(search_parent_directories=True)
@@ -224,14 +230,14 @@ def get_centers(self):
224230

225231
def draw_commit(self, commit, i, prevCircle, shift=numpy.array([0.0, 0.0, 0.0])):
226232
if commit == "dark":
227-
commitFill = m.WHITE if settings.light_mode else m.BLACK
233+
commit_fill = m.WHITE if settings.light_mode else m.BLACK
228234
elif len(commit.parents) <= 1:
229-
commitFill = m.RED
235+
commit_fill = m.RED
230236
else:
231-
commitFill = m.GRAY
232-
237+
commit_fill = m.GRAY
238+
233239
circle = m.Circle(
234-
stroke_color=commitFill, fill_color=commitFill, fill_opacity=0.25
240+
stroke_color=commit_fill, fill_color=commit_fill, fill_opacity=self.fill_opacity
235241
)
236242
circle.height = 1
237243

@@ -363,7 +369,7 @@ def build_commit_id_and_message(self, commit, i):
363369

364370
def draw_head(self, commit, i, commitId):
365371
if commit.hexsha == self.repo.head.commit.hexsha:
366-
headbox = m.Rectangle(color=m.BLUE, fill_color=m.BLUE, fill_opacity=0.25)
372+
headbox = m.Rectangle(color=m.BLUE, fill_color=m.BLUE, fill_opacity=self.ref_fill_opacity)
367373
headbox.width = 1
368374
headbox.height = 0.4
369375
if settings.highlight_commit_messages:
@@ -423,7 +429,7 @@ def draw_branch(self, commit, i, make_branches_remote=False):
423429
branchRec = m.Rectangle(
424430
color=m.GREEN,
425431
fill_color=m.GREEN,
426-
fill_opacity=0.25,
432+
fill_opacity=self.ref_fill_opacity,
427433
height=0.4,
428434
width=branchText.width + 0.25,
429435
)
@@ -468,7 +474,7 @@ def draw_tag(self, commit, i):
468474
tagRec = m.Rectangle(
469475
color=m.YELLOW,
470476
fill_color=m.YELLOW,
471-
fill_opacity=0.25,
477+
fill_opacity=self.ref_fill_opacity,
472478
height=0.4,
473479
width=tagText.width + 0.25,
474480
)
@@ -954,7 +960,7 @@ def setup_and_draw_parent(
954960
draw_arrow=True,
955961
color=m.RED,
956962
):
957-
circle = m.Circle(stroke_color=color, fill_color=color, fill_opacity=0.25)
963+
circle = m.Circle(stroke_color=color, fill_color=color, fill_opacity=self.ref_fill_opacity)
958964
circle.height = 1
959965
circle.next_to(
960966
self.drawnCommits[child.hexsha],
@@ -1033,7 +1039,7 @@ def draw_ref(self, commit, top, i=0, text="HEAD", color=m.BLUE):
10331039
refbox = m.Rectangle(
10341040
color=color,
10351041
fill_color=color,
1036-
fill_opacity=0.25,
1042+
fill_opacity=self.ref_fill_opacity,
10371043
height=0.4,
10381044
width=refText.width + 0.25,
10391045
)

git_sim/settings.py

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class Settings(BaseSettings):
2626
img_format: ImgFormat = ImgFormat.jpg
2727
INFO_STRING = "Simulating: git"
2828
light_mode = False
29+
transparent_bg = False
2930
logo = pathlib.Path(__file__).parent.resolve() / "logo.png"
3031
low_quality = False
3132
max_branches_per_commit = 1

0 commit comments

Comments
 (0)