Skip to content

Commit

Permalink
Merge pull request #44 from radarhere/fix-im-long-name
Browse files Browse the repository at this point in the history
Trim end of name, keeping ext
  • Loading branch information
hugovk committed Feb 26, 2020
2 parents a82ba5b + 9ba25f8 commit 853e2f2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
Binary file added Tests/images/hopper_long_name.im
Binary file not shown.
9 changes: 9 additions & 0 deletions Tests/test_file_im.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import filecmp

import pytest
from PIL import Image, ImImagePlugin

Expand All @@ -15,6 +17,13 @@ def test_sanity():
assert im.format == "IM"


def test_name_limit(tmp_path):
out = str(tmp_path / ("name_limit_test" * 7 + ".im"))
with Image.open(TEST_IM) as im:
im.save(out)
assert filecmp.cmp(out, "Tests/images/hopper_long_name.im")


@pytest.mark.skipif(is_pypy(), reason="Requires CPython")
def test_unclosed_file():
def open():
Expand Down
9 changes: 7 additions & 2 deletions src/PIL/ImImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,14 @@ def _save(im, fp, filename):

fp.write(("Image type: %s image\r\n" % image_type).encode("ascii"))
if filename:
# Each line must be under length 100, or: SyntaxError("not an IM file")
# Each line must be 100 characters or less,
# or: SyntaxError("not an IM file")
# 8 characters are used for "Name: " and "\r\n"
# Keep just the filename, ditch the potentially overlong path
fp.write(("Name: %s\r\n" % os.path.basename(filename)).encode("ascii"))
name, ext = os.path.splitext(os.path.basename(filename))
name = "".join([name[: 92 - len(ext)], ext])

fp.write(("Name: %s\r\n" % name).encode("ascii"))
fp.write(("Image size (x*y): %d*%d\r\n" % im.size).encode("ascii"))
fp.write(("File size (no of images): %d\r\n" % frames).encode("ascii"))
if im.mode in ["P", "PA"]:
Expand Down

0 comments on commit 853e2f2

Please sign in to comment.