Skip to content

Commit

Permalink
Upgrade Pillow to 8.2.0
Browse files Browse the repository at this point in the history
The test images need to change as Pillow 8 changed text handling in
two ways: first, introducing the anchor option to determine where the
text is placed compared to the offset; second, fixed a bug where the
text would incorrectly be offset by the same amount as the stroke
width.

This, unfortunately, also breaks the ability to rebuild any older
GIFs without them changing as I cannot find a setting where the captions
aren't altered in the rendering.
  • Loading branch information
norm committed Jul 1, 2021
1 parent 4d53556 commit e55706c
Show file tree
Hide file tree
Showing 18 changed files with 53 additions and 26 deletions.
3 changes: 3 additions & 0 deletions README.markdown
Expand Up @@ -191,8 +191,11 @@ array, supporting multiple captions in a GIF.
* `c,-60` — letters and numbers can be used in combination, and if
only letters the comma can be omitted
`caption` reports the x,y position actually used
* `anchor` is the [alignment of the text][an] relative to the `placement`;
default is `la`

[col]: https://pillow.readthedocs.io/en/stable/reference/ImageColor.html
[an]: https://pillow.readthedocs.io/en/stable/handbook/text-anchors.html


### Note on colours
Expand Down
5 changes: 5 additions & 0 deletions bin/caption
Expand Up @@ -47,6 +47,10 @@ def main():
'--text-align',
default='left',
)
parser.add_argument(
'--text-anchor',
default='la',
)
parser.add_argument(
'width',
type=int,
Expand Down Expand Up @@ -135,6 +139,7 @@ def main():
stroke_fill=args.stroke_colour,
stroke_width=args.stroke_width,
align=args.text_align,
anchor=args.text_anchor,
)
canvas.save(args.output)

Expand Down
2 changes: 2 additions & 0 deletions bin/make_gif
Expand Up @@ -80,6 +80,7 @@ function main {
caption_strokew="$(toml $toml caption.stroke_width $index)"
caption_strokecol="$(toml $toml caption.stroke_colour $index)"
caption_align="$(toml $toml caption.align $index)"
caption_anchor="$(toml $toml caption.anchor $index)"
caption \
${output_width:-$DEFAULT_WIDTH} \
$output_height \
Expand All @@ -91,6 +92,7 @@ function main {
--stroke-colour "${caption_strokecol:-#000000}" \
--text-align "${caption_align:-left}" \
--placement ${caption_placement:-bl} \
--text-anchor ${caption_anchor:-la} \
"${caption_text}" \
"${TEMP}/caption-${index}.png"

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
@@ -1,4 +1,4 @@
flourish>=0.10.2
Pillow==7.2.0
Pillow==8.2.0
toml
tweepy
7 changes: 7 additions & 0 deletions tests/captions.bats
Expand Up @@ -17,6 +17,7 @@
echo "$output"

[ "$status" -eq 0 ]
# cp $BATS_TMPDIR/caption.png tests/output/caption.png
diff tests/output/caption.png $BATS_TMPDIR/caption.png
}

Expand All @@ -31,6 +32,7 @@
echo "$output"

[ "$status" -eq 0 ]
# cp $BATS_TMPDIR/caption.png tests/output/caption_centred.png
diff tests/output/caption_centred.png $BATS_TMPDIR/caption.png
}

Expand All @@ -45,6 +47,7 @@
echo "$output"

[ "$status" -eq 0 ]
# cp $BATS_TMPDIR/caption.png tests/output/caption_placement.png
diff tests/output/caption_placement.png $BATS_TMPDIR/caption.png
}

Expand All @@ -61,6 +64,7 @@
echo "$output"

[ "$status" -eq 0 ]
# cp $BATS_TMPDIR/caption.png tests/output/caption_colours.png
diff tests/output/caption_colours.png $BATS_TMPDIR/caption.png
}

Expand All @@ -76,6 +80,7 @@
echo "$output"

[ "$status" -eq 0 ]
# cp $BATS_TMPDIR/caption.png tests/output/caption_sizes.png
diff tests/output/caption_sizes.png $BATS_TMPDIR/caption.png
}

Expand All @@ -91,6 +96,7 @@
echo "$output"

[ "$status" -eq 0 ]
# cp $BATS_TMPDIR/caption.png tests/output/caption_assistant_bold.png
diff tests/output/caption_assistant_bold.png $BATS_TMPDIR/caption.png
}

Expand All @@ -105,5 +111,6 @@
echo "$output"

[ "$status" -eq 0 ]
# cp $BATS_TMPDIR/caption.png tests/output/caption_align.png
diff tests/output/caption_align.png $BATS_TMPDIR/caption.png
}
Binary file modified tests/gifs/captions.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/gifs/captions_colours.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/gifs/captions_noscale.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/gifs/clips_captions.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/gifs/clips_captions_timing.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
60 changes: 35 additions & 25 deletions tests/make_gifs.bats
Expand Up @@ -9,21 +9,22 @@ reset=$'\e'[0m
# FIXME
[ $(uname) != 'Darwin' ] && skip "Not macOS"

run make_gif tests/config/original.toml $BATS_TMPDIR/gif
run make_gif tests/config/original.toml $BATS_TMPDIR/output.gif
echo "$output"

[ "$status" -eq 0 ]
[ "${lines[0]}" == " 640x480 dither=bayer_scale=4 fps=original" ]
[ "${lines[1]}" == " 64 colours (64 initially)" ]
[ "${lines[2]}" == " size 1.61mb (duration 1.01) $magenta[auto max 0.45mb]$reset" ]
[ "${lines[3]}" == " $cyan< optimised with loss 0, now 1.48mb, 92.3% of original$reset" ]
diff -u $BATS_TMPDIR/gif tests/gifs/original.gif
# cp $BATS_TMPDIR/output.gif tests/gifs/original.gif
diff -u $BATS_TMPDIR/output.gif tests/gifs/original.gif
}

@test generate_lossy {
[ $(uname) != 'Darwin' ] && skip "Not macOS"

run make_gif tests/config/lossy.toml $BATS_TMPDIR/gif
run make_gif tests/config/lossy.toml $BATS_TMPDIR/output.gif
echo "$output"

[ "${lines[2]}" == " size 1.61mb (duration 1.01), ${magenta}max_size 1.00mb$reset $magenta[auto max 0.45mb]$reset" ]
Expand All @@ -32,39 +33,43 @@ reset=$'\e'[0m
[ "${lines[5]}" == " $green< optimised with loss 40, now 1.00mb, 99.6% of max$reset" ]

[ "$status" -eq 0 ]
diff -u $BATS_TMPDIR/gif tests/gifs/lossy.gif
# cp $BATS_TMPDIR/output.gif tests/gifs/lossy.gif
diff -u $BATS_TMPDIR/output.gif tests/gifs/lossy.gif
}

@test generate_fps {
[ $(uname) != 'Darwin' ] && skip "Not macOS"

run make_gif tests/config/fps.toml $BATS_TMPDIR/gif
run make_gif tests/config/fps.toml $BATS_TMPDIR/output.gif
echo "$output"

[ "$status" -eq 0 ]
[ "${lines[0]}" == " 640x480 dither=bayer_scale=4 fps=10" ]
diff -u $BATS_TMPDIR/gif tests/gifs/fps.gif
# cp $BATS_TMPDIR/output.gif tests/gifs/fps.gif
diff -u $BATS_TMPDIR/output.gif tests/gifs/fps.gif
}

@test generate_scale {
[ $(uname) != 'Darwin' ] && skip "Not macOS"

run make_gif tests/config/scale.toml $BATS_TMPDIR/gif
run make_gif tests/config/scale.toml $BATS_TMPDIR/output.gif
echo "$output"

[ "$status" -eq 0 ]
[ "${lines[0]}" == " 480x360 dither=floyd_steinberg fps=10" ]
diff -u $BATS_TMPDIR/gif tests/gifs/scale.gif
# cp $BATS_TMPDIR/output.gif tests/gifs/scale.gif
diff -u $BATS_TMPDIR/output.gif tests/gifs/scale.gif
}

@test generate_crop {
[ $(uname) != 'Darwin' ] && skip "Not macOS"

run make_gif tests/config/crop.toml $BATS_TMPDIR/gif
run make_gif tests/config/crop.toml $BATS_TMPDIR/output.gif
echo "$output"

[ "$status" -eq 0 ]
diff -u $BATS_TMPDIR/gif tests/gifs/crop.gif
# cp $BATS_TMPDIR/output.gif tests/gifs/crop.gif
diff -u $BATS_TMPDIR/output.gif tests/gifs/crop.gif
}

@test generate_slowdown {
Expand All @@ -80,66 +85,71 @@ reset=$'\e'[0m
@test generate_clips {
[ $(uname) != 'Darwin' ] && skip "Not macOS"

run make_gif tests/config/clips.toml $BATS_TMPDIR/gif
run make_gif tests/config/clips.toml $BATS_TMPDIR/output.gif
echo "$output"

[ "$status" -eq 0 ]
diff -u $BATS_TMPDIR/gif tests/gifs/clips.gif
diff -u $BATS_TMPDIR/output.gif tests/gifs/clips.gif
}

@test generate_captions {
[ $(uname) != 'Darwin' ] && skip "Not macOS"

run make_gif tests/config/captions.toml $BATS_TMPDIR/gif
run make_gif tests/config/captions.toml $BATS_TMPDIR/output.gif
echo "$output"

[ "${lines[1]}" == ' " font_size=40 placement=[122, 305] And the climb' ]
[ "${lines[2]}" == ' " font_size=40 placement=[172, 305] is going' ]
[ "${lines[3]}" == " \" ${magenta}font_size=34${reset} placement=[15, 311] Where no man has gone before" ]
[ "${lines[1]}" == ' " font_size=40 placement=[121, 305] And the climb' ]
[ "${lines[2]}" == ' " font_size=40 placement=[173, 305] is going' ]
[ "${lines[3]}" == " \" ${magenta}font_size=34${reset} placement=[16, 311] Where no man has gone before" ]
[ "${lines[4]}" == " 102 colours (96 initially)" ]

[ "$status" -eq 0 ]
diff -u $BATS_TMPDIR/gif tests/gifs/captions.gif
# cp $BATS_TMPDIR/output.gif tests/gifs/captions.gif
diff -u $BATS_TMPDIR/output.gif tests/gifs/captions.gif
}

@test generate_captions_noscale {
[ $(uname) != 'Darwin' ] && skip "Not macOS"

run make_gif tests/config/captions_noscale.toml $BATS_TMPDIR/gif
run make_gif tests/config/captions_noscale.toml $BATS_TMPDIR/output.gif
echo "$output"

[ "$status" -eq 0 ]
diff -u $BATS_TMPDIR/gif tests/gifs/captions_noscale.gif
# cp $BATS_TMPDIR/output.gif tests/gifs/captions_noscale.gif
diff -u $BATS_TMPDIR/output.gif tests/gifs/captions_noscale.gif
}

@test generate_clips_captions {
[ $(uname) != 'Darwin' ] && skip "Not macOS"

run make_gif tests/config/clips_captions.toml $BATS_TMPDIR/gif
run make_gif tests/config/clips_captions.toml $BATS_TMPDIR/output.gif
echo "$output"

[ "$status" -eq 0 ]
diff -u $BATS_TMPDIR/gif tests/gifs/clips_captions.gif
# cp $BATS_TMPDIR/output.gif tests/gifs/clips_captions.gif
diff -u $BATS_TMPDIR/output.gif tests/gifs/clips_captions.gif
}

@test generate_clips_captions_timing {
[ $(uname) != 'Darwin' ] && skip "Not macOS"

run make_gif tests/config/clips_captions_timing.toml $BATS_TMPDIR/gif
run make_gif tests/config/clips_captions_timing.toml $BATS_TMPDIR/output.gif
echo "$output"

[ "$status" -eq 0 ]
diff -u $BATS_TMPDIR/gif tests/gifs/clips_captions_timing.gif
# cp $BATS_TMPDIR/output.gif tests/gifs/clips_captions_timing.gif
diff -u $BATS_TMPDIR/output.gif tests/gifs/clips_captions_timing.gif
}

@test generate_palette_additions {
[ $(uname) != 'Darwin' ] && skip "Not macOS"

run make_gif tests/config/captions_colours.toml $BATS_TMPDIR/gif
run make_gif tests/config/captions_colours.toml $BATS_TMPDIR/output.gif
echo "$output"

[ "${lines[4]}" == " 58 colours (48 initially)" ]

[ "$status" -eq 0 ]
diff -u $BATS_TMPDIR/gif tests/gifs/captions_colours.gif
# cp $BATS_TMPDIR/output.gif tests/gifs/captions_colours.gif
diff -u $BATS_TMPDIR/output.gif tests/gifs/captions_colours.gif
}
Binary file modified tests/output/caption.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/output/caption_align.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/output/caption_assistant_bold.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/output/caption_centred.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/output/caption_colours.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/output/caption_placement.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/output/caption_sizes.png
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 e55706c

Please sign in to comment.