Merge pull request #4424 from hugovk/fix-im-long-name

Fix saving IM images in dir with long path
This commit is contained in:
Andrew Murray 2020-02-27 20:27:44 +11:00 committed by GitHub
commit 1c1ad65a96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 202 additions and 162 deletions

Binary file not shown.

View File

@ -1,54 +1,59 @@
import io import io
import pytest
from PIL import BmpImagePlugin, Image from PIL import BmpImagePlugin, Image
from .helper import PillowTestCase, assert_image_equal, hopper from .helper import assert_image_equal, hopper
class TestFileBmp(PillowTestCase): def test_sanity(tmp_path):
def roundtrip(self, im): def roundtrip(im):
outfile = self.tempfile("temp.bmp") outfile = str(tmp_path / "temp.bmp")
im.save(outfile, "BMP") im.save(outfile, "BMP")
with Image.open(outfile) as reloaded: with Image.open(outfile) as reloaded:
reloaded.load() reloaded.load()
self.assertEqual(im.mode, reloaded.mode) assert im.mode == reloaded.mode
self.assertEqual(im.size, reloaded.size) assert im.size == reloaded.size
self.assertEqual(reloaded.format, "BMP") assert reloaded.format == "BMP"
self.assertEqual(reloaded.get_format_mimetype(), "image/bmp") assert reloaded.get_format_mimetype() == "image/bmp"
def test_sanity(self): roundtrip(hopper())
self.roundtrip(hopper())
self.roundtrip(hopper("1")) roundtrip(hopper("1"))
self.roundtrip(hopper("L")) roundtrip(hopper("L"))
self.roundtrip(hopper("P")) roundtrip(hopper("P"))
self.roundtrip(hopper("RGB")) roundtrip(hopper("RGB"))
def test_invalid_file(self):
def test_invalid_file():
with open("Tests/images/flower.jpg", "rb") as fp: with open("Tests/images/flower.jpg", "rb") as fp:
self.assertRaises(SyntaxError, BmpImagePlugin.BmpImageFile, fp) with pytest.raises(SyntaxError):
BmpImagePlugin.BmpImageFile(fp)
def test_save_to_bytes(self):
def test_save_to_bytes():
output = io.BytesIO() output = io.BytesIO()
im = hopper() im = hopper()
im.save(output, "BMP") im.save(output, "BMP")
output.seek(0) output.seek(0)
with Image.open(output) as reloaded: with Image.open(output) as reloaded:
self.assertEqual(im.mode, reloaded.mode) assert im.mode == reloaded.mode
self.assertEqual(im.size, reloaded.size) assert im.size == reloaded.size
self.assertEqual(reloaded.format, "BMP") assert reloaded.format == "BMP"
def test_save_too_large(self):
outfile = self.tempfile("temp.bmp") def test_save_too_large(tmp_path):
outfile = str(tmp_path / "temp.bmp")
with Image.new("RGB", (1, 1)) as im: with Image.new("RGB", (1, 1)) as im:
im._size = (37838, 37838) im._size = (37838, 37838)
with self.assertRaises(ValueError): with pytest.raises(ValueError):
im.save(outfile) im.save(outfile)
def test_dpi(self):
def test_dpi():
dpi = (72, 72) dpi = (72, 72)
output = io.BytesIO() output = io.BytesIO()
@ -57,12 +62,13 @@ class TestFileBmp(PillowTestCase):
output.seek(0) output.seek(0)
with Image.open(output) as reloaded: with Image.open(output) as reloaded:
self.assertEqual(reloaded.info["dpi"], dpi) assert reloaded.info["dpi"] == dpi
def test_save_bmp_with_dpi(self):
def test_save_bmp_with_dpi(tmp_path):
# Test for #1301 # Test for #1301
# Arrange # Arrange
outfile = self.tempfile("temp.jpg") outfile = str(tmp_path / "temp.jpg")
with Image.open("Tests/images/hopper.bmp") as im: with Image.open("Tests/images/hopper.bmp") as im:
# Act # Act
@ -71,51 +77,56 @@ class TestFileBmp(PillowTestCase):
# Assert # Assert
with Image.open(outfile) as reloaded: with Image.open(outfile) as reloaded:
reloaded.load() reloaded.load()
self.assertEqual(im.info["dpi"], reloaded.info["dpi"]) assert im.info["dpi"] == reloaded.info["dpi"]
self.assertEqual(im.size, reloaded.size) assert im.size == reloaded.size
self.assertEqual(reloaded.format, "JPEG") assert reloaded.format == "JPEG"
def test_load_dpi_rounding(self):
def test_load_dpi_rounding():
# Round up # Round up
with Image.open("Tests/images/hopper.bmp") as im: with Image.open("Tests/images/hopper.bmp") as im:
self.assertEqual(im.info["dpi"], (96, 96)) assert im.info["dpi"] == (96, 96)
# Round down # Round down
with Image.open("Tests/images/hopper_roundDown.bmp") as im: with Image.open("Tests/images/hopper_roundDown.bmp") as im:
self.assertEqual(im.info["dpi"], (72, 72)) assert im.info["dpi"] == (72, 72)
def test_save_dpi_rounding(self):
outfile = self.tempfile("temp.bmp") def test_save_dpi_rounding(tmp_path):
outfile = str(tmp_path / "temp.bmp")
with Image.open("Tests/images/hopper.bmp") as im: with Image.open("Tests/images/hopper.bmp") as im:
im.save(outfile, dpi=(72.2, 72.2)) im.save(outfile, dpi=(72.2, 72.2))
with Image.open(outfile) as reloaded: with Image.open(outfile) as reloaded:
self.assertEqual(reloaded.info["dpi"], (72, 72)) assert reloaded.info["dpi"] == (72, 72)
im.save(outfile, dpi=(72.8, 72.8)) im.save(outfile, dpi=(72.8, 72.8))
with Image.open(outfile) as reloaded: with Image.open(outfile) as reloaded:
self.assertEqual(reloaded.info["dpi"], (73, 73)) assert reloaded.info["dpi"] == (73, 73)
def test_load_dib(self):
def test_load_dib():
# test for #1293, Imagegrab returning Unsupported Bitfields Format # test for #1293, Imagegrab returning Unsupported Bitfields Format
with Image.open("Tests/images/clipboard.dib") as im: with Image.open("Tests/images/clipboard.dib") as im:
self.assertEqual(im.format, "DIB") assert im.format == "DIB"
self.assertEqual(im.get_format_mimetype(), "image/bmp") assert im.get_format_mimetype() == "image/bmp"
with Image.open("Tests/images/clipboard_target.png") as target: with Image.open("Tests/images/clipboard_target.png") as target:
assert_image_equal(im, target) assert_image_equal(im, target)
def test_save_dib(self):
outfile = self.tempfile("temp.dib") def test_save_dib(tmp_path):
outfile = str(tmp_path / "temp.dib")
with Image.open("Tests/images/clipboard.dib") as im: with Image.open("Tests/images/clipboard.dib") as im:
im.save(outfile) im.save(outfile)
with Image.open(outfile) as reloaded: with Image.open(outfile) as reloaded:
self.assertEqual(reloaded.format, "DIB") assert reloaded.format == "DIB"
self.assertEqual(reloaded.get_format_mimetype(), "image/bmp") assert reloaded.get_format_mimetype() == "image/bmp"
assert_image_equal(im, reloaded) assert_image_equal(im, reloaded)
def test_rgba_bitfields(self):
def test_rgba_bitfields():
# This test image has been manually hexedited # This test image has been manually hexedited
# to change the bitfield compression in the header from XBGR to RGBA # to change the bitfield compression in the header from XBGR to RGBA
with Image.open("Tests/images/rgb32bf-rgba.bmp") as im: with Image.open("Tests/images/rgb32bf-rgba.bmp") as im:

View File

@ -1,31 +1,39 @@
import unittest import filecmp
import pytest import pytest
from PIL import Image, ImImagePlugin from PIL import Image, ImImagePlugin
from .helper import PillowTestCase, assert_image_equal, hopper, is_pypy from .helper import assert_image_equal, hopper, is_pypy
# sample im # sample im
TEST_IM = "Tests/images/hopper.im" TEST_IM = "Tests/images/hopper.im"
class TestFileIm(PillowTestCase): def test_sanity():
def test_sanity(self):
with Image.open(TEST_IM) as im: with Image.open(TEST_IM) as im:
im.load() im.load()
self.assertEqual(im.mode, "RGB") assert im.mode == "RGB"
self.assertEqual(im.size, (128, 128)) assert im.size == (128, 128)
self.assertEqual(im.format, "IM") assert im.format == "IM"
@unittest.skipIf(is_pypy(), "Requires CPython")
def test_unclosed_file(self): 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(): def open():
im = Image.open(TEST_IM) im = Image.open(TEST_IM)
im.load() im.load()
pytest.warns(ResourceWarning, open) pytest.warns(ResourceWarning, open)
def test_closed_file(self):
def test_closed_file():
def open(): def open():
im = Image.open(TEST_IM) im = Image.open(TEST_IM)
im.load() im.load()
@ -33,14 +41,16 @@ class TestFileIm(PillowTestCase):
pytest.warns(None, open) pytest.warns(None, open)
def test_context_manager(self):
def test_context_manager():
def open(): def open():
with Image.open(TEST_IM) as im: with Image.open(TEST_IM) as im:
im.load() im.load()
pytest.warns(None, open) pytest.warns(None, open)
def test_tell(self):
def test_tell():
# Arrange # Arrange
with Image.open(TEST_IM) as im: with Image.open(TEST_IM) as im:
@ -48,42 +58,53 @@ class TestFileIm(PillowTestCase):
frame = im.tell() frame = im.tell()
# Assert # Assert
self.assertEqual(frame, 0) assert frame == 0
def test_n_frames(self):
def test_n_frames():
with Image.open(TEST_IM) as im: with Image.open(TEST_IM) as im:
self.assertEqual(im.n_frames, 1) assert im.n_frames == 1
self.assertFalse(im.is_animated) assert not im.is_animated
def test_eoferror(self):
def test_eoferror():
with Image.open(TEST_IM) as im: with Image.open(TEST_IM) as im:
n_frames = im.n_frames n_frames = im.n_frames
# Test seeking past the last frame # Test seeking past the last frame
self.assertRaises(EOFError, im.seek, n_frames) with pytest.raises(EOFError):
self.assertLess(im.tell(), n_frames) im.seek(n_frames)
assert im.tell() < n_frames
# Test that seeking to the last frame does not raise an error # Test that seeking to the last frame does not raise an error
im.seek(n_frames - 1) im.seek(n_frames - 1)
def test_roundtrip(self):
for mode in ["RGB", "P", "PA"]: def test_roundtrip(tmp_path):
out = self.tempfile("temp.im") def roundtrip(mode):
out = str(tmp_path / "temp.im")
im = hopper(mode) im = hopper(mode)
im.save(out) im.save(out)
with Image.open(out) as reread: with Image.open(out) as reread:
assert_image_equal(reread, im) assert_image_equal(reread, im)
def test_save_unsupported_mode(self): for mode in ["RGB", "P", "PA"]:
out = self.tempfile("temp.im") roundtrip(mode)
im = hopper("HSV")
self.assertRaises(ValueError, im.save, out)
def test_invalid_file(self):
def test_save_unsupported_mode(tmp_path):
out = str(tmp_path / "temp.im")
im = hopper("HSV")
with pytest.raises(ValueError):
im.save(out)
def test_invalid_file():
invalid_file = "Tests/images/flower.jpg" invalid_file = "Tests/images/flower.jpg"
self.assertRaises(SyntaxError, ImImagePlugin.ImImageFile, invalid_file) with pytest.raises(SyntaxError):
ImImagePlugin.ImImageFile(invalid_file)
def test_number(self):
self.assertEqual(1.2, ImImagePlugin.number("1.2")) def test_number():
assert ImImagePlugin.number("1.2") == 1.2

View File

@ -26,6 +26,7 @@
# #
import os
import re import re
from . import Image, ImageFile, ImagePalette from . import Image, ImageFile, ImagePalette
@ -347,7 +348,14 @@ 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:
fp.write(("Name: %s\r\n" % filename).encode("ascii")) # 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
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(("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"]: