From 0b62337b6fbdd04b2a744341bca50420baf222af Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sun, 17 Mar 2019 23:37:40 +1100 Subject: [PATCH] Ensure image is mutable before saving --- Tests/test_image.py | 12 ++++++++++++ src/PIL/Image.py | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Tests/test_image.py b/Tests/test_image.py index 330048057..fae514b2c 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -3,6 +3,8 @@ from .helper import unittest, PillowTestCase, hopper from PIL import Image from PIL._util import py3 import os +import sys +import shutil class TestImage(PillowTestCase): @@ -121,6 +123,16 @@ class TestImage(PillowTestCase): im.paste(0, (0, 0, 100, 100)) self.assertFalse(im.readonly) + @unittest.skipIf(sys.platform.startswith('win32'), + "Test requires opening tempfile twice") + def test_readonly_save(self): + temp_file = self.tempfile("temp.bmp") + shutil.copy("Tests/images/rgb32bf-rgba.bmp", temp_file) + + im = Image.open(temp_file) + self.assertTrue(im.readonly) + im.save(temp_file) + def test_dump(self): im = Image.new("L", (10, 10)) im._dump(self.tempfile("temp_L.ppm")) diff --git a/src/PIL/Image.py b/src/PIL/Image.py index c77a4cfe2..2eff2db29 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -1958,7 +1958,7 @@ class Image(object): filename = fp.name # may mutate self! - self.load() + self._ensure_mutable() save_all = params.pop('save_all', False) self.encoderinfo = params