Save IM: use only filename as name, ditch potentially overlong path

This commit is contained in:
Hugo 2020-02-22 13:00:25 +02:00
parent 56fd7b3d41
commit a82ba5b2c2

View File

@ -26,6 +26,7 @@
# #
import os
import re import re
from . import Image, ImageFile, ImagePalette from . import Image, ImageFile, ImagePalette
@ -348,10 +349,8 @@ def _save(im, fp, filename):
fp.write(("Image type: %s image\r\n" % image_type).encode("ascii")) fp.write(("Image type: %s image\r\n" % image_type).encode("ascii"))
if filename: if filename:
# Each line must be under length 100, or: SyntaxError("not an IM file") # Each line must be under length 100, or: SyntaxError("not an IM file")
name_format = "Name: %s\r\n" # Keep just the filename, ditch the potentially overlong path
max = 100 - len(name_format % "") fp.write(("Name: %s\r\n" % os.path.basename(filename)).encode("ascii"))
# Keep the last part of the string, will hold the filename.ext
fp.write((name_format % filename[-max:]).encode("ascii"))
fp.write(("Image size (x*y): %d*%d\r\n" % im.size).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")) fp.write(("File size (no of images): %d\r\n" % frames).encode("ascii"))
if im.mode in ["P", "PA"]: if im.mode in ["P", "PA"]: