Merge branch 'master' into box-in-thumbnail

# Conflicts:
#	Tests/test_image_thumbnail.py
This commit is contained in:
Alexander 2019-12-17 00:42:29 +03:00
commit a027897f74
101 changed files with 2109 additions and 2255 deletions

View File

@ -15,10 +15,6 @@ environment:
matrix:
- PYTHON: C:/Python38
- PYTHON: C:/Python38-x64
- PYTHON: C:/Python37
- PYTHON: C:/Python37-x64
- PYTHON: C:/Python36
- PYTHON: C:/Python36-x64
- PYTHON: C:/Python35
- PYTHON: C:/Python35-x64
- PYTHON: C:/msys64/mingw32
@ -56,6 +52,9 @@ install:
c:\pillow\winbuild\build_deps.cmd
$host.SetShouldExit(0)
}
- curl -fsSL -o gs950.exe https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs950/gs950w32.exe
- gs950.exe /S
- path %path%;C:\Program Files (x86)\gs\gs9.50\bin
build_script:
- ps: |
@ -77,7 +76,7 @@ test_script:
- cd c:\pillow
- '%PYTHON%\%PIP_DIR%\pip.exe install pytest pytest-cov'
- c:\"Program Files (x86)"\"Windows Kits"\10\Debuggers\x86\gflags.exe /p /enable %PYTHON%\%EXECUTABLE%
- '%PYTHON%\%EXECUTABLE% -m pytest -vx --cov PIL --cov-report term --cov-report xml Tests'
- '%PYTHON%\%EXECUTABLE% -m pytest -vx --cov PIL --cov Tests --cov-report term --cov-report xml Tests'
#- '%PYTHON%\%EXECUTABLE% test-installed.py -v -s %TEST_OPTIONS%' TODO TEST_OPTIONS with pytest?
after_test:

View File

@ -3,9 +3,16 @@
codecov:
# Avoid "Missing base report" due to committing CHANGES.rst with "[CI skip]"
# https://github.com/codecov/support/issues/363
# https://docs.codecov.io/v4.3.6/docs/comparing-commits
# https://docs.codecov.io/docs/comparing-commits
allow_coverage_offsets: true
token: 6dafc396-e7f5-4221-a38a-8b07a49fbdae
comment: off
# Matches 'omit:' in .coveragerc
ignore:
- "Tests/32bit_segfault_check.py"
- "Tests/bench_cffi_access.py"
- "Tests/check_*.py"
- "Tests/createfontdatachunk.py"

View File

@ -12,3 +12,10 @@ exclude_lines =
# Don't complain about debug code
if Image.DEBUG:
if DEBUG:
[run]
omit =
Tests/32bit_segfault_check.py
Tests/bench_cffi_access.py
Tests/check_*.py
Tests/createfontdatachunk.py

View File

@ -20,6 +20,9 @@ jobs:
with:
python-version: ${{ matrix.python-version }}
- name: Build system information
run: python .github/workflows/system-info.py
- name: Install dependencies
run: |
python -m pip install --upgrade pip

25
.github/workflows/system-info.py vendored Normal file
View File

@ -0,0 +1,25 @@
"""
Print out some handy system info like Travis CI does.
This sort of info is missing from GitHub Actions.
Requested here:
https://github.com/actions/virtual-environments/issues/79
"""
import os
import platform
import sys
print("Build system information")
print()
print("sys.version\t\t", sys.version.split("\n"))
print("os.name\t\t\t", os.name)
print("sys.platform\t\t", sys.platform)
print("platform.system()\t", platform.system())
print("platform.machine()\t", platform.machine())
print("platform.platform()\t", platform.platform())
print("platform.version()\t", platform.version())
print("platform.uname()\t", platform.uname())
if sys.platform == "darwin":
print("platform.mac_ver()\t", platform.mac_ver())

View File

@ -29,6 +29,9 @@ jobs:
steps:
- uses: actions/checkout@v1
- name: Build system information
run: python .github/workflows/system-info.py
- name: Docker pull
run: |
docker pull pythonpillow/${{ matrix.docker }}:${{ matrix.dockerTag }}

View File

@ -31,6 +31,21 @@ jobs:
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v1
with:
repository: python-pillow/pillow-depends
ref: master
- name: Cache
uses: actions/cache@v1
with:
path: ~\AppData\Local\pip\Cache
key:
${{ runner.os }}-${{ matrix.python-version }}-${{ matrix.architecture }}-${{ hashFiles('**/.github/workflows/test-windows.yml') }}
restore-keys: |
${{ runner.os }}-${{ matrix.python-version }}-${{ matrix.architecture }}-
${{ runner.os }}-${{ matrix.python-version }}-
- name: Install PyPy
if: "contains(matrix.python-version, 'pypy')"
run: |
@ -55,32 +70,28 @@ jobs:
python-version: ${{ matrix.python-version }}
architecture: ${{ matrix.architecture }}
- name: pip install wheel pytest pytest-cov codecov
- name: Build system information
run: python .github/workflows/system-info.py
- name: pip install wheel pytest pytest-cov
run: |
"%pythonLocation%\python.exe" -m pip install wheel pytest pytest-cov
pip install codecov
shell: cmd
- name: Fetch dependencies
run: |
curl -fsSL -o nasm.zip https://www.nasm.us/pub/nasm/releasebuilds/2.14.02/win64/nasm-2.14.02-win64.zip
7z x nasm.zip "-o$env:RUNNER_WORKSPACE\"
7z x ..\pillow-depends\nasm-2.14.02-win64.zip "-o$env:RUNNER_WORKSPACE\"
Write-Host "`#`#[add-path]$env:RUNNER_WORKSPACE\nasm-2.14.02"
Write-Host "::add-path::$env:RUNNER_WORKSPACE\nasm-2.14.02"
# 32-bit should work on both platforms
curl -fsSL -o gs950.exe https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs950/gs950w32.exe
./gs950.exe /S
..\pillow-depends\gs950w32.exe /S
Write-Host "`#`#[add-path]C:\Program Files (x86)\gs\gs9.50\bin"
Write-Host "::add-path::C:\Program Files (x86)\gs\gs9.50\bin"
$env:PYTHON=$env:pythonLocation
curl -fsSL -o pillow-depends.zip https://github.com/python-pillow/pillow-depends/archive/master.zip
7z x pillow-depends.zip -oc:\
mv c:\pillow-depends-master c:\pillow-depends
xcopy c:\pillow-depends\*.zip $env:GITHUB_WORKSPACE\winbuild\
xcopy c:\pillow-depends\*.tar.gz $env:GITHUB_WORKSPACE\winbuild\
xcopy /s c:\pillow-depends\test_images\* $env:GITHUB_WORKSPACE\tests\images\
xcopy ..\pillow-depends\*.zip $env:GITHUB_WORKSPACE\winbuild\
xcopy ..\pillow-depends\*.tar.gz $env:GITHUB_WORKSPACE\winbuild\
xcopy /s ..\pillow-depends\test_images\* $env:GITHUB_WORKSPACE\tests\images\
cd $env:GITHUB_WORKSPACE/winbuild/
python.exe $env:GITHUB_WORKSPACE\winbuild\build_dep.py
env:
@ -253,7 +264,6 @@ jobs:
# for Raqm
- name: Build dependencies / HarfBuzz
if: "!contains(matrix.python-version, 'pypy')"
run: |
set INCLUDE=C:\Program Files (x86)\Microsoft SDKs\Windows\V7.1A\Include
set INCLIB=%GITHUB_WORKSPACE%\winbuild\depends\msvcr10-x32
@ -274,7 +284,6 @@ jobs:
# for Raqm
- name: Build dependencies / FriBidi
if: "!contains(matrix.python-version, 'pypy')"
run: |
set INCLUDE=C:\Program Files (x86)\Microsoft SDKs\Windows\V7.1A\Include
set INCLIB=%GITHUB_WORKSPACE%\winbuild\depends\msvcr10-x32
@ -292,9 +301,7 @@ jobs:
copy /Y /B *.lib %INCLIB%
shell: cmd
# failing with PyPy3
- name: Build dependencies / Raqm
if: "!contains(matrix.python-version, 'pypy')"
run: |
set INCLUDE=C:\Program Files (x86)\Microsoft SDKs\Windows\V7.1A\Include
set INCLIB=%GITHUB_WORKSPACE%\winbuild\depends\msvcr10-x32
@ -347,7 +354,7 @@ jobs:
rem Add libraqm.dll (copied to INCLIB) to PATH.
path %INCLIB%;%PATH%
cd /D %GITHUB_WORKSPACE%
%PYTHON%\python.exe -m pytest -vx --cov PIL --cov-report term --cov-report xml Tests
%PYTHON%\python.exe -m pytest -vx --cov PIL --cov Tests --cov-report term --cov-report xml Tests
shell: cmd
- name: Upload errors
@ -358,8 +365,11 @@ jobs:
path: Tests/errors
- name: Upload coverage
run: 'codecov --file "%GITHUB_WORKSPACE%\coverage.xml" --name "%pythonLocation%"'
shell: cmd
if: success()
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
name: ${{ runner.os }} Python ${{ matrix.python-version }}
- name: Build wheel
id: wheel

View File

@ -30,11 +30,34 @@ jobs:
steps:
- uses: actions/checkout@v1
- name: Ubuntu cache
uses: actions/cache@v1
if: startsWith(matrix.os, 'ubuntu')
with:
path: ~/.cache/pip
key:
${{ matrix.os }}-${{ matrix.python-version }}-${{ hashFiles('**/.travis/*.sh') }}
restore-keys: |
${{ matrix.os }}-${{ matrix.python-version }}-
- name: macOS cache
uses: actions/cache@v1
if: startsWith(matrix.os, 'macOS')
with:
path: ~/Library/Caches/pip
key:
${{ matrix.os }}-${{ matrix.python-version }}-${{ hashFiles('**/.travis/*.sh') }}
restore-keys: |
${{ matrix.os }}-${{ matrix.python-version }}-
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v1
with:
python-version: ${{ matrix.python-version }}
- name: Build system information
run: python .github/workflows/system-info.py
- name: Install Linux dependencies
if: startsWith(matrix.os, 'ubuntu')
run: |
@ -71,5 +94,13 @@ jobs:
run: |
.travis/after_success.sh
env:
MATRIX_OS: ${{ matrix.os }}
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}
- name: Upload coverage
if: success()
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
name: ${{ matrix.os }} Python ${{ matrix.python-version }}

View File

@ -1,7 +1,12 @@
#!/bin/bash
# gather the coverage data
sudo apt-get -qq install lcov
if [[ "$MATRIX_OS" == "macOS-latest" ]]; then
brew install lcov
else
sudo apt-get -qq install lcov
fi
lcov --capture --directory . -b . --output-file coverage.info
# filter to remove system headers
lcov --remove coverage.info '/usr/*' -o coverage.filtered.info
@ -13,7 +18,9 @@ coverage report
pip install codecov
pip install coveralls-merge
coveralls-merge coverage.c.json
codecov
if [[ $TRAVIS ]]; then
codecov
fi
if [ "$TRAVIS_PYTHON_VERSION" == "3.7" ] && [ "$DOCKER" == "" ]; then
# Coverage and quality reports on just the latest diff.

View File

@ -2,7 +2,7 @@
set -e
python -m pytest -v -x --cov PIL --cov-report term Tests
python -m pytest -v -x --cov PIL --cov Tests --cov-report term Tests
# Docs
if [ "$TRAVIS_PYTHON_VERSION" == "3.7" ]; then make doccheck; fi

View File

@ -5,6 +5,21 @@ Changelog (Pillow)
7.0.0 (unreleased)
------------------
- Add La mode packing and unpacking #4248
[homm]
- Include tests in coverage reports #4173
[hugovk]
- Handle broken Photoshop data #4239
[radarhere]
- Raise a specific exception if no data is found for an MPO frame #4240
[radarhere]
- Fix Unicode support for PyPy #4145
[nulano]
- Drop support for EOL Python 2.7 #4109
[hugovk, radarhere, jdufresne]

View File

@ -27,6 +27,6 @@ Run all the tests from the root of the Pillow source distribution::
Or with coverage::
pytest --cov PIL --cov-report term
pytest --cov PIL --cov Tests --cov-report term
coverage html
open htmlcov/index.html

View File

@ -1,23 +0,0 @@
import sys
import timeit
from . import helper
sys.path.insert(0, ".")
def bench(mode):
im = helper.hopper(mode)
get = im.im.getpixel
xy = 50, 50 # position shouldn't really matter
t0 = timeit.default_timer()
for _ in range(1000000):
get(xy)
print(mode, timeit.default_timer() - t0, "us")
bench("L")
bench("I")
bench("I;16")
bench("F")
bench("RGB")

View File

@ -11,7 +11,7 @@ class TestFliOverflow(PillowTestCase):
def test_fli_overflow(self):
# this should not crash with a malloc error or access violation
im = Image.open(TEST_FILE)
with Image.open(TEST_FILE) as im:
im.load()

View File

@ -14,7 +14,7 @@ class TestLibtiffSegfault(PillowTestCase):
"""
with self.assertRaises(IOError):
im = Image.open(TEST_FILE)
with Image.open(TEST_FILE) as im:
im.load()

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 117 KiB

View File

@ -1,14 +0,0 @@
import glob
import os
import sys
import traceback
sys.path.insert(0, ".")
for file in glob.glob("src/PIL/*.py"):
module = os.path.basename(file)[:-3]
try:
exec("from PIL import " + module)
except (ImportError, SyntaxError):
print("===", "failed to import", module)
traceback.print_exc()

View File

@ -1,66 +0,0 @@
# brute-force search for access descriptor hash table
modes = [
"1",
"L",
"LA",
"La",
"I",
"I;16",
"I;16L",
"I;16B",
"I;32L",
"I;32B",
"F",
"P",
"PA",
"RGB",
"RGBA",
"RGBa",
"RGBX",
"CMYK",
"YCbCr",
"LAB",
"HSV",
]
def hash(s, i):
# djb2 hash: multiply by 33 and xor character
for c in s:
i = (((i << 5) + i) ^ ord(c)) & 0xFFFFFFFF
return i
def check(size, i0):
h = [None] * size
for m in modes:
i = hash(m, i0)
i = i % size
if h[i]:
return 0
h[i] = m
return h
min_start = 0
# 1) find the smallest table size with no collisions
for min_size in range(len(modes), 16384):
if check(min_size, 0):
print(len(modes), "modes fit in", min_size, "slots")
break
# 2) see if we can do better with a different initial value
for i0 in range(65556):
for size in range(1, min_size):
if check(size, i0):
if size < min_size:
print(len(modes), "modes fit in", size, "slots with start", i0)
min_size = size
min_start = i0
print()
print("#define ACCESS_TABLE_SIZE", min_size)
print("#define ACCESS_TABLE_HASH", min_start)

View File

@ -15,6 +15,6 @@ class TestFileBlp(PillowTestCase):
self.assert_image_equal(im, target)
def test_load_blp2_dxt1a(self):
im = Image.open("Tests/images/blp/blp2_dxt1a.blp")
target = Image.open("Tests/images/blp/blp2_dxt1a.png")
with Image.open("Tests/images/blp/blp2_dxt1a.blp") as im:
with Image.open("Tests/images/blp/blp2_dxt1a.png") as target:
self.assert_image_equal(im, target)

View File

@ -11,7 +11,7 @@ class TestFileBmp(PillowTestCase):
im.save(outfile, "BMP")
reloaded = Image.open(outfile)
with Image.open(outfile) as reloaded:
reloaded.load()
self.assertEqual(im.mode, reloaded.mode)
self.assertEqual(im.size, reloaded.size)
@ -36,8 +36,7 @@ class TestFileBmp(PillowTestCase):
im.save(output, "BMP")
output.seek(0)
reloaded = Image.open(output)
with Image.open(output) as reloaded:
self.assertEqual(im.mode, reloaded.mode)
self.assertEqual(im.size, reloaded.size)
self.assertEqual(reloaded.format, "BMP")
@ -57,13 +56,13 @@ class TestFileBmp(PillowTestCase):
# Test for #1301
# Arrange
outfile = self.tempfile("temp.jpg")
im = Image.open("Tests/images/hopper.bmp")
with Image.open("Tests/images/hopper.bmp") as im:
# Act
im.save(outfile, "JPEG", dpi=im.info["dpi"])
# Assert
reloaded = Image.open(outfile)
with Image.open(outfile) as reloaded:
reloaded.load()
self.assertEqual(im.info["dpi"], reloaded.info["dpi"])
self.assertEqual(im.size, reloaded.size)
@ -80,8 +79,7 @@ class TestFileBmp(PillowTestCase):
def test_save_dpi_rounding(self):
outfile = self.tempfile("temp.bmp")
im = Image.open("Tests/images/hopper.bmp")
with Image.open("Tests/images/hopper.bmp") as im:
im.save(outfile, dpi=(72.2, 72.2))
with Image.open(outfile) as reloaded:
self.assertEqual(reloaded.info["dpi"], (72, 72))
@ -92,20 +90,20 @@ class TestFileBmp(PillowTestCase):
def test_load_dib(self):
# test for #1293, Imagegrab returning Unsupported Bitfields Format
im = Image.open("Tests/images/clipboard.dib")
with Image.open("Tests/images/clipboard.dib") as im:
self.assertEqual(im.format, "DIB")
self.assertEqual(im.get_format_mimetype(), "image/bmp")
target = Image.open("Tests/images/clipboard_target.png")
with Image.open("Tests/images/clipboard_target.png") as target:
self.assert_image_equal(im, target)
def test_save_dib(self):
outfile = self.tempfile("temp.dib")
im = Image.open("Tests/images/clipboard.dib")
with Image.open("Tests/images/clipboard.dib") as im:
im.save(outfile)
reloaded = Image.open(outfile)
with Image.open(outfile) as reloaded:
self.assertEqual(reloaded.format, "DIB")
self.assertEqual(reloaded.get_format_mimetype(), "image/bmp")
self.assert_image_equal(im, reloaded)
@ -113,11 +111,11 @@ class TestFileBmp(PillowTestCase):
def test_rgba_bitfields(self):
# This test image has been manually hexedited
# to change the bitfield compression in the header from XBGR to RGBA
im = Image.open("Tests/images/rgb32bf-rgba.bmp")
with Image.open("Tests/images/rgb32bf-rgba.bmp") as im:
# So before the comparing the image, swap the channels
b, g, r = im.split()[1:]
im = Image.merge("RGB", (r, g, b))
target = Image.open("Tests/images/bmp/q/rgb32bf-xbgr.bmp")
with Image.open("Tests/images/bmp/q/rgb32bf-xbgr.bmp") as target:
self.assert_image_equal(im, target)

View File

@ -7,8 +7,7 @@ TEST_FILE = "Tests/images/deerstalker.cur"
class TestFileCur(PillowTestCase):
def test_sanity(self):
im = Image.open(TEST_FILE)
with Image.open(TEST_FILE) as im:
self.assertEqual(im.size, (32, 32))
self.assertIsInstance(im, CurImagePlugin.CurImageFile)
# Check some pixel colors to ensure image is loaded properly

View File

@ -17,37 +17,35 @@ class TestFileDds(PillowTestCase):
def test_sanity_dxt1(self):
"""Check DXT1 images can be opened"""
target = Image.open(TEST_FILE_DXT1.replace(".dds", ".png"))
im = Image.open(TEST_FILE_DXT1)
with Image.open(TEST_FILE_DXT1.replace(".dds", ".png")) as target:
target = target.convert("RGBA")
with Image.open(TEST_FILE_DXT1) as im:
im.load()
self.assertEqual(im.format, "DDS")
self.assertEqual(im.mode, "RGBA")
self.assertEqual(im.size, (256, 256))
self.assert_image_equal(target.convert("RGBA"), im)
self.assert_image_equal(im, target)
def test_sanity_dxt5(self):
"""Check DXT5 images can be opened"""
target = Image.open(TEST_FILE_DXT5.replace(".dds", ".png"))
im = Image.open(TEST_FILE_DXT5)
with Image.open(TEST_FILE_DXT5) as im:
im.load()
self.assertEqual(im.format, "DDS")
self.assertEqual(im.mode, "RGBA")
self.assertEqual(im.size, (256, 256))
with Image.open(TEST_FILE_DXT5.replace(".dds", ".png")) as target:
self.assert_image_equal(target, im)
def test_sanity_dxt3(self):
"""Check DXT3 images can be opened"""
target = Image.open(TEST_FILE_DXT3.replace(".dds", ".png"))
im = Image.open(TEST_FILE_DXT3)
with Image.open(TEST_FILE_DXT3.replace(".dds", ".png")) as target:
with Image.open(TEST_FILE_DXT3) as im:
im.load()
self.assertEqual(im.format, "DDS")
@ -59,23 +57,20 @@ class TestFileDds(PillowTestCase):
def test_dx10_bc7(self):
"""Check DX10 images can be opened"""
target = Image.open(TEST_FILE_DX10_BC7.replace(".dds", ".png"))
im = Image.open(TEST_FILE_DX10_BC7)
with Image.open(TEST_FILE_DX10_BC7) as im:
im.load()
self.assertEqual(im.format, "DDS")
self.assertEqual(im.mode, "RGBA")
self.assertEqual(im.size, (256, 256))
with Image.open(TEST_FILE_DX10_BC7.replace(".dds", ".png")) as target:
self.assert_image_equal(target, im)
def test_dx10_bc7_unorm_srgb(self):
"""Check DX10 unsigned normalized integer images can be opened"""
target = Image.open(TEST_FILE_DX10_BC7_UNORM_SRGB.replace(".dds", ".png"))
im = Image.open(TEST_FILE_DX10_BC7_UNORM_SRGB)
with Image.open(TEST_FILE_DX10_BC7_UNORM_SRGB) as im:
im.load()
self.assertEqual(im.format, "DDS")
@ -83,6 +78,9 @@ class TestFileDds(PillowTestCase):
self.assertEqual(im.size, (16, 16))
self.assertEqual(im.info["gamma"], 1 / 2.2)
with Image.open(
TEST_FILE_DX10_BC7_UNORM_SRGB.replace(".dds", ".png")
) as target:
self.assert_image_equal(target, im)
def test_unimplemented_dxgi_format(self):
@ -95,15 +93,16 @@ class TestFileDds(PillowTestCase):
def test_uncompressed_rgb(self):
"""Check uncompressed RGB images can be opened"""
target = Image.open(TEST_FILE_UNCOMPRESSED_RGB.replace(".dds", ".png"))
im = Image.open(TEST_FILE_UNCOMPRESSED_RGB)
with Image.open(TEST_FILE_UNCOMPRESSED_RGB) as im:
im.load()
self.assertEqual(im.format, "DDS")
self.assertEqual(im.mode, "RGBA")
self.assertEqual(im.size, (800, 600))
with Image.open(
TEST_FILE_UNCOMPRESSED_RGB.replace(".dds", ".png")
) as target:
self.assert_image_equal(target, im)
def test__validate_true(self):
@ -145,7 +144,7 @@ class TestFileDds(PillowTestCase):
img_file = f.read()
def short_file():
im = Image.open(BytesIO(img_file[:-100]))
with Image.open(BytesIO(img_file[:-100])) as im:
im.load()
self.assertRaises(IOError, short_file)

View File

@ -68,7 +68,7 @@ class TestFileEps(PillowTestCase):
self.assertEqual(cmyk_image.mode, "RGB")
if "jpeg_decoder" in dir(Image.core):
target = Image.open("Tests/images/pil_sample_rgb.jpg")
with Image.open("Tests/images/pil_sample_rgb.jpg") as target:
self.assert_image_similar(cmyk_image, target, 10)
@unittest.skipUnless(HAS_GHOSTSCRIPT, "Ghostscript not available")
@ -100,10 +100,11 @@ class TestFileEps(PillowTestCase):
with open(file1, "rb") as f:
img_bytes = io.BytesIO(f.read())
img = Image.open(img_bytes)
with Image.open(img_bytes) as img:
img.load()
image1_scale1_compare = Image.open(file1_compare).convert("RGB")
with Image.open(file1_compare) as image1_scale1_compare:
image1_scale1_compare = image1_scale1_compare.convert("RGB")
image1_scale1_compare.load()
self.assert_image_similar(img, image1_scale1_compare, 5)
@ -122,14 +123,16 @@ class TestFileEps(PillowTestCase):
# Zero bounding box
with Image.open(file1) as image1_scale1:
image1_scale1.load()
image1_scale1_compare = Image.open(file1_compare).convert("RGB")
with Image.open(file1_compare) as image1_scale1_compare:
image1_scale1_compare = image1_scale1_compare.convert("RGB")
image1_scale1_compare.load()
self.assert_image_similar(image1_scale1, image1_scale1_compare, 5)
# Non-Zero bounding box
with Image.open(file2) as image2_scale1:
image2_scale1.load()
image2_scale1_compare = Image.open(file2_compare).convert("RGB")
with Image.open(file2_compare) as image2_scale1_compare:
image2_scale1_compare = image2_scale1_compare.convert("RGB")
image2_scale1_compare.load()
self.assert_image_similar(image2_scale1, image2_scale1_compare, 10)
@ -143,14 +146,16 @@ class TestFileEps(PillowTestCase):
# Zero bounding box
with Image.open(file1) as image1_scale2:
image1_scale2.load(scale=2)
image1_scale2_compare = Image.open(file1_compare_scale2).convert("RGB")
with Image.open(file1_compare_scale2) as image1_scale2_compare:
image1_scale2_compare = image1_scale2_compare.convert("RGB")
image1_scale2_compare.load()
self.assert_image_similar(image1_scale2, image1_scale2_compare, 5)
# Non-Zero bounding box
with Image.open(file2) as image2_scale2:
image2_scale2.load(scale=2)
image2_scale2_compare = Image.open(file2_compare_scale2).convert("RGB")
with Image.open(file2_compare_scale2) as image2_scale2_compare:
image2_scale2_compare = image2_scale2_compare.convert("RGB")
image2_scale2_compare.load()
self.assert_image_similar(image2_scale2, image2_scale2_compare, 10)

View File

@ -5,12 +5,11 @@ from .helper import PillowTestCase
class TestFileFtex(PillowTestCase):
def test_load_raw(self):
im = Image.open("Tests/images/ftex_uncompressed.ftu")
target = Image.open("Tests/images/ftex_uncompressed.png")
with Image.open("Tests/images/ftex_uncompressed.ftu") as im:
with Image.open("Tests/images/ftex_uncompressed.png") as target:
self.assert_image_equal(im, target)
def test_load_dxt1(self):
im = Image.open("Tests/images/ftex_dxt1.ftc")
target = Image.open("Tests/images/ftex_dxt1.png")
with Image.open("Tests/images/ftex_dxt1.ftc") as im:
with Image.open("Tests/images/ftex_dxt1.png") as target:
self.assert_image_similar(im, target.convert("RGBA"), 15)

View File

@ -94,10 +94,11 @@ class TestFileGif(PillowTestCase):
outfile = BytesIO()
im.save(outfile, "GIF")
outfile.seek(0)
reloaded = Image.open(outfile)
with Image.open(outfile) as reloaded:
# check palette length
palette_length = max(i + 1 for i, v in enumerate(reloaded.histogram()) if v)
palette_length = max(
i + 1 for i, v in enumerate(reloaded.histogram()) if v
)
self.assertEqual(expected_palette_length, palette_length)
self.assert_image_equal(im.convert("RGB"), reloaded.convert("RGB"))
@ -554,7 +555,7 @@ class TestFileGif(PillowTestCase):
self.assertEqual(reread.info["background"], im.info["background"])
if HAVE_WEBP and _webp.HAVE_WEBPANIM:
im = Image.open("Tests/images/hopper.webp")
with Image.open("Tests/images/hopper.webp") as im:
self.assertIsInstance(im.info["background"], tuple)
im.save(out)

View File

@ -27,29 +27,28 @@ class TestFileIcns(PillowTestCase):
@unittest.skipIf(sys.platform != "darwin", "requires macOS")
def test_save(self):
im = Image.open(TEST_FILE)
temp_file = self.tempfile("temp.icns")
with Image.open(TEST_FILE) as im:
im.save(temp_file)
reread = Image.open(temp_file)
with Image.open(temp_file) as reread:
self.assertEqual(reread.mode, "RGBA")
self.assertEqual(reread.size, (1024, 1024))
self.assertEqual(reread.format, "ICNS")
@unittest.skipIf(sys.platform != "darwin", "requires macOS")
def test_save_append_images(self):
im = Image.open(TEST_FILE)
temp_file = self.tempfile("temp.icns")
provided_im = Image.new("RGBA", (32, 32), (255, 0, 0, 128))
with Image.open(TEST_FILE) as im:
im.save(temp_file, append_images=[provided_im])
reread = Image.open(temp_file)
with Image.open(temp_file) as reread:
self.assert_image_similar(reread, im, 1)
reread = Image.open(temp_file)
with Image.open(temp_file) as reread:
reread.size = (16, 16, 2)
reread.load()
self.assert_image_equal(reread, provided_im)

View File

@ -27,7 +27,7 @@ class TestFileIco(PillowTestCase):
# the default image
output.seek(0)
reloaded = Image.open(output)
with Image.open(output) as reloaded:
self.assertEqual(reloaded.info["sizes"], {(32, 32), (64, 64)})
self.assertEqual(im.mode, reloaded.mode)
@ -37,7 +37,7 @@ class TestFileIco(PillowTestCase):
# the other one
output.seek(0)
reloaded = Image.open(output)
with Image.open(output) as reloaded:
reloaded.size = (32, 32)
self.assertEqual(im.mode, reloaded.mode)

View File

@ -44,7 +44,7 @@ class TestFileJpeg(PillowTestCase):
# internal version number
self.assertRegex(Image.core.jpeglib_version, r"\d+\.\d+$")
im = Image.open(TEST_FILE)
with Image.open(TEST_FILE) as im:
im.load()
self.assertEqual(im.mode, "RGB")
self.assertEqual(im.size, (128, 128))
@ -66,7 +66,7 @@ class TestFileJpeg(PillowTestCase):
# Test CMYK handling. Thanks to Tim and Charlie for test data,
# Michael for getting me to look one more time.
f = "Tests/images/pil_sample_cmyk.jpg"
im = Image.open(f)
with Image.open(f) as im:
# the source image has red pixels in the upper left corner.
c, m, y, k = [x / 255.0 for x in im.getpixel((0, 0))]
self.assertEqual(c, 0.0)
@ -74,7 +74,9 @@ class TestFileJpeg(PillowTestCase):
self.assertGreater(y, 0.8)
self.assertEqual(k, 0.0)
# the opposite corner is black
c, m, y, k = [x / 255.0 for x in im.getpixel((im.size[0] - 1, im.size[1] - 1))]
c, m, y, k = [
x / 255.0 for x in im.getpixel((im.size[0] - 1, im.size[1] - 1))
]
self.assertGreater(k, 0.9)
# roundtrip, and check again
im = self.roundtrip(im)
@ -83,12 +85,14 @@ class TestFileJpeg(PillowTestCase):
self.assertGreater(m, 0.8)
self.assertGreater(y, 0.8)
self.assertEqual(k, 0.0)
c, m, y, k = [x / 255.0 for x in im.getpixel((im.size[0] - 1, im.size[1] - 1))]
c, m, y, k = [
x / 255.0 for x in im.getpixel((im.size[0] - 1, im.size[1] - 1))
]
self.assertGreater(k, 0.9)
def test_dpi(self):
def test(xdpi, ydpi=None):
im = Image.open(TEST_FILE)
with Image.open(TEST_FILE) as im:
im = self.roundtrip(im, dpi=(xdpi, ydpi or xdpi))
return im.info.get("dpi")
@ -140,7 +144,7 @@ class TestFileJpeg(PillowTestCase):
# https://github.com/python-pillow/Pillow/issues/148
# Sometimes the meta data on the icc_profile block is bigger than
# Image.MAXBLOCK or the image size.
im = Image.open("Tests/images/icc_profile_big.jpg")
with Image.open("Tests/images/icc_profile_big.jpg") as im:
f = self.tempfile("temp.jpg")
icc_profile = im.info["icc_profile"]
# Should not raise IOError for image with icc larger than image size.
@ -339,15 +343,15 @@ class TestFileJpeg(PillowTestCase):
def test_quality_keep(self):
# RGB
im = Image.open("Tests/images/hopper.jpg")
with Image.open("Tests/images/hopper.jpg") as im:
f = self.tempfile("temp.jpg")
im.save(f, quality="keep")
# Grayscale
im = Image.open("Tests/images/hopper_gray.jpg")
with Image.open("Tests/images/hopper_gray.jpg") as im:
f = self.tempfile("temp.jpg")
im.save(f, quality="keep")
# CMYK
im = Image.open("Tests/images/pil_sample_cmyk.jpg")
with Image.open("Tests/images/pil_sample_cmyk.jpg") as im:
f = self.tempfile("temp.jpg")
im.save(f, quality="keep")
@ -365,7 +369,7 @@ class TestFileJpeg(PillowTestCase):
def test_truncated_jpeg_should_read_all_the_data(self):
filename = "Tests/images/truncated_jpeg.jpg"
ImageFile.LOAD_TRUNCATED_IMAGES = True
im = Image.open(filename)
with Image.open(filename) as im:
im.load()
ImageFile.LOAD_TRUNCATED_IMAGES = False
self.assertIsNotNone(im.getbbox())
@ -381,16 +385,16 @@ class TestFileJpeg(PillowTestCase):
im.load()
def _n_qtables_helper(self, n, test_file):
im = Image.open(test_file)
with Image.open(test_file) as im:
f = self.tempfile("temp.jpg")
im.save(f, qtables=[[n] * 64] * n)
im = Image.open(f)
with Image.open(f) as im:
self.assertEqual(len(im.quantization), n)
reloaded = self.roundtrip(im, qtables="keep")
self.assertEqual(im.quantization, reloaded.quantization)
def test_qtables(self):
im = Image.open("Tests/images/hopper.jpg")
with Image.open("Tests/images/hopper.jpg") as im:
qtables = im.quantization
reloaded = self.roundtrip(im, qtables=qtables, subsampling=0)
self.assertEqual(im.quantization, reloaded.quantization)
@ -490,8 +494,7 @@ class TestFileJpeg(PillowTestCase):
@unittest.skipUnless(cjpeg_available(), "cjpeg not available")
def test_save_cjpeg(self):
img = Image.open(TEST_FILE)
with Image.open(TEST_FILE) as img:
tempfile = self.tempfile("temp.jpg")
JpegImagePlugin._save_cjpeg(img, 0, tempfile)
# Default save quality is 75%, so a tiny bit of difference is alright
@ -512,8 +515,7 @@ class TestFileJpeg(PillowTestCase):
f = self.tempfile("temp.jpeg")
im.save(f, quality=100, optimize=True)
reloaded = Image.open(f)
with Image.open(f) as reloaded:
# none of these should crash
reloaded.save(f, quality="keep")
reloaded.save(f, quality="keep", progressive=True)
@ -547,13 +549,13 @@ class TestFileJpeg(PillowTestCase):
def test_save_tiff_with_dpi(self):
# Arrange
outfile = self.tempfile("temp.tif")
im = Image.open("Tests/images/hopper.tif")
with Image.open("Tests/images/hopper.tif") as im:
# Act
im.save(outfile, "JPEG", dpi=im.info["dpi"])
# Assert
reloaded = Image.open(outfile)
with Image.open(outfile) as reloaded:
reloaded.load()
self.assertEqual(im.info["dpi"], reloaded.info["dpi"])
@ -568,13 +570,14 @@ class TestFileJpeg(PillowTestCase):
def test_save_dpi_rounding(self):
outfile = self.tempfile("temp.jpg")
im = Image.open("Tests/images/hopper.jpg")
with Image.open("Tests/images/hopper.jpg") as im:
im.save(outfile, dpi=(72.2, 72.2))
with Image.open(outfile) as reloaded:
self.assertEqual(reloaded.info["dpi"], (72, 72))
im.save(outfile, dpi=(72.8, 72.8))
with Image.open(outfile) as reloaded:
self.assertEqual(reloaded.info["dpi"], (73, 73))
@ -656,6 +659,11 @@ class TestFileJpeg(PillowTestCase):
},
)
# Test that the image can still load, even with broken Photoshop data
# This image had the APP13 length hexedited to be smaller
with Image.open("Tests/images/photoshop-200dpi-broken.jpg") as im_broken:
self.assert_image_equal(im_broken, im)
# This image does not contain a Photoshop header string
with Image.open("Tests/images/app13.jpg") as im:
self.assertNotIn("photoshop", im.info)

View File

@ -33,7 +33,7 @@ class TestFileJpeg2k(PillowTestCase):
# Internal version number
self.assertRegex(Image.core.jp2klib_version, r"\d+\.\d+\.\d+$")
im = Image.open("Tests/images/test-card-lossless.jp2")
with Image.open("Tests/images/test-card-lossless.jp2") as im:
px = im.load()
self.assertEqual(px[0, 0], (0, 0, 0))
self.assertEqual(im.mode, "RGB")
@ -54,7 +54,7 @@ class TestFileJpeg2k(PillowTestCase):
def test_bytesio(self):
with open("Tests/images/test-card-lossless.jp2", "rb") as f:
data = BytesIO(f.read())
im = Image.open(data)
with Image.open(data) as im:
im.load()
self.assert_image_similar(im, test_card, 1.0e-3)
@ -62,14 +62,14 @@ class TestFileJpeg2k(PillowTestCase):
# PIL (they were made using Adobe Photoshop)
def test_lossless(self):
im = Image.open("Tests/images/test-card-lossless.jp2")
with Image.open("Tests/images/test-card-lossless.jp2") as im:
im.load()
outfile = self.tempfile("temp_test-card.png")
im.save(outfile)
self.assert_image_similar(im, test_card, 1.0e-3)
def test_lossy_tiled(self):
im = Image.open("Tests/images/test-card-lossy-tiled.jp2")
with Image.open("Tests/images/test-card-lossy-tiled.jp2") as im:
im.load()
self.assert_image_similar(im, test_card, 2.0)
@ -110,7 +110,7 @@ class TestFileJpeg2k(PillowTestCase):
self.assert_image_equal(im, test_card)
def test_reduce(self):
im = Image.open("Tests/images/test-card-lossless.jp2")
with Image.open("Tests/images/test-card-lossless.jp2") as im:
im.reduce = 2
im.load()
self.assertEqual(im.size, (160, 120))
@ -132,21 +132,21 @@ class TestFileJpeg2k(PillowTestCase):
)
out.seek(0)
im = Image.open(out)
with Image.open(out) as im:
im.layers = 1
im.load()
self.assert_image_similar(im, test_card, 13)
out.seek(0)
im = Image.open(out)
with Image.open(out) as im:
im.layers = 3
im.load()
self.assert_image_similar(im, test_card, 0.4)
def test_rgba(self):
# Arrange
j2k = Image.open("Tests/images/rgb_trns_ycbc.j2k")
jp2 = Image.open("Tests/images/rgb_trns_ycbc.jp2")
with Image.open("Tests/images/rgb_trns_ycbc.j2k") as j2k:
with Image.open("Tests/images/rgb_trns_ycbc.jp2") as jp2:
# Act
j2k.load()
@ -157,37 +157,31 @@ class TestFileJpeg2k(PillowTestCase):
self.assertEqual(jp2.mode, "RGBA")
def test_16bit_monochrome_has_correct_mode(self):
j2k = Image.open("Tests/images/16bit.cropped.j2k")
jp2 = Image.open("Tests/images/16bit.cropped.jp2")
with Image.open("Tests/images/16bit.cropped.j2k") as j2k:
j2k.load()
jp2.load()
self.assertEqual(j2k.mode, "I;16")
with Image.open("Tests/images/16bit.cropped.jp2") as jp2:
jp2.load()
self.assertEqual(jp2.mode, "I;16")
def test_16bit_monochrome_jp2_like_tiff(self):
tiff_16bit = Image.open("Tests/images/16bit.cropped.tif")
jp2 = Image.open("Tests/images/16bit.cropped.jp2")
with Image.open("Tests/images/16bit.cropped.tif") as tiff_16bit:
with Image.open("Tests/images/16bit.cropped.jp2") as jp2:
self.assert_image_similar(jp2, tiff_16bit, 1e-3)
def test_16bit_monochrome_j2k_like_tiff(self):
tiff_16bit = Image.open("Tests/images/16bit.cropped.tif")
j2k = Image.open("Tests/images/16bit.cropped.j2k")
with Image.open("Tests/images/16bit.cropped.tif") as tiff_16bit:
with Image.open("Tests/images/16bit.cropped.j2k") as j2k:
self.assert_image_similar(j2k, tiff_16bit, 1e-3)
def test_16bit_j2k_roundtrips(self):
j2k = Image.open("Tests/images/16bit.cropped.j2k")
with Image.open("Tests/images/16bit.cropped.j2k") as j2k:
im = self.roundtrip(j2k)
self.assert_image_equal(im, j2k)
def test_16bit_jp2_roundtrips(self):
jp2 = Image.open("Tests/images/16bit.cropped.jp2")
with Image.open("Tests/images/16bit.cropped.jp2") as jp2:
im = self.roundtrip(jp2)
self.assert_image_equal(im, jp2)

View File

@ -47,14 +47,13 @@ class TestFileLibTiff(LibTiffTestCase):
"""Test the ordinary file path load path"""
test_file = "Tests/images/hopper_g4_500.tif"
im = Image.open(test_file)
with Image.open(test_file) as im:
self.assertEqual(im.size, (500, 500))
self._assert_noerr(im)
def test_g4_large(self):
test_file = "Tests/images/pport_g4.tif"
im = Image.open(test_file)
with Image.open(test_file) as im:
self._assert_noerr(im)
def test_g4_tiff_file(self):
@ -62,8 +61,7 @@ class TestFileLibTiff(LibTiffTestCase):
test_file = "Tests/images/hopper_g4_500.tif"
with open(test_file, "rb") as f:
im = Image.open(f)
with Image.open(f) as im:
self.assertEqual(im.size, (500, 500))
self._assert_noerr(im)
@ -74,8 +72,7 @@ class TestFileLibTiff(LibTiffTestCase):
with open(test_file, "rb") as f:
s.write(f.read())
s.seek(0)
im = Image.open(s)
with Image.open(s) as im:
self.assertEqual(im.size, (500, 500))
self._assert_noerr(im)
@ -87,37 +84,33 @@ class TestFileLibTiff(LibTiffTestCase):
s.write(f.read())
s.seek(0)
r = io.BufferedReader(s)
im = Image.open(r)
with Image.open(r) as im:
self.assertEqual(im.size, (500, 500))
self._assert_noerr(im)
def test_g4_eq_png(self):
""" Checking that we're actually getting the data that we expect"""
png = Image.open("Tests/images/hopper_bw_500.png")
g4 = Image.open("Tests/images/hopper_g4_500.tif")
with Image.open("Tests/images/hopper_bw_500.png") as png:
with Image.open("Tests/images/hopper_g4_500.tif") as g4:
self.assert_image_equal(g4, png)
# see https://github.com/python-pillow/Pillow/issues/279
def test_g4_fillorder_eq_png(self):
""" Checking that we're actually getting the data that we expect"""
png = Image.open("Tests/images/g4-fillorder-test.png")
g4 = Image.open("Tests/images/g4-fillorder-test.tif")
with Image.open("Tests/images/g4-fillorder-test.png") as png:
with Image.open("Tests/images/g4-fillorder-test.tif") as g4:
self.assert_image_equal(g4, png)
def test_g4_write(self):
"""Checking to see that the saved image is the same as what we wrote"""
test_file = "Tests/images/hopper_g4_500.tif"
orig = Image.open(test_file)
with Image.open(test_file) as orig:
out = self.tempfile("temp.tif")
rot = orig.transpose(Image.ROTATE_90)
self.assertEqual(rot.size, (500, 500))
rot.save(out)
reread = Image.open(out)
with Image.open(out) as reread:
self.assertEqual(reread.size, (500, 500))
self._assert_noerr(reread)
self.assert_image_equal(reread, rot)
@ -129,8 +122,7 @@ class TestFileLibTiff(LibTiffTestCase):
def test_adobe_deflate_tiff(self):
test_file = "Tests/images/tiff_adobe_deflate.tif"
im = Image.open(test_file)
with Image.open(test_file) as im:
self.assertEqual(im.mode, "RGB")
self.assertEqual(im.size, (278, 374))
self.assertEqual(im.tile[0][:3], ("libtiff", (0, 0, 278, 374), 0))
@ -204,7 +196,7 @@ class TestFileLibTiff(LibTiffTestCase):
# Exclude ones that have special meaning
# that we're already testing them
im = Image.open("Tests/images/hopper_g4.tif")
with Image.open("Tests/images/hopper_g4.tif") as im:
for tag in im.tag_v2:
try:
del core_items[tag]
@ -342,16 +334,16 @@ class TestFileLibTiff(LibTiffTestCase):
self.assertEqual(reloaded.info["dpi"], (72.0, 72.0))
def test_g3_compression(self):
i = Image.open("Tests/images/hopper_g4_500.tif")
with Image.open("Tests/images/hopper_g4_500.tif") as i:
out = self.tempfile("temp.tif")
i.save(out, compression="group3")
reread = Image.open(out)
with Image.open(out) as reread:
self.assertEqual(reread.info["compression"], "group3")
self.assert_image_equal(reread, i)
def test_little_endian(self):
im = Image.open("Tests/images/16bit.deflate.tif")
with Image.open("Tests/images/16bit.deflate.tif") as im:
self.assertEqual(im.getpixel((0, 0)), 480)
self.assertEqual(im.mode, "I;16")
@ -363,16 +355,14 @@ class TestFileLibTiff(LibTiffTestCase):
out = self.tempfile("temp.tif")
# out = "temp.le.tif"
im.save(out)
reread = Image.open(out)
with Image.open(out) as reread:
self.assertEqual(reread.info["compression"], im.info["compression"])
self.assertEqual(reread.getpixel((0, 0)), 480)
# UNDONE - libtiff defaults to writing in native endian, so
# on big endian, we'll get back mode = 'I;16B' here.
def test_big_endian(self):
im = Image.open("Tests/images/16bit.MM.deflate.tif")
with Image.open("Tests/images/16bit.MM.deflate.tif") as im:
self.assertEqual(im.getpixel((0, 0)), 480)
self.assertEqual(im.mode, "I;16B")
@ -384,16 +374,14 @@ class TestFileLibTiff(LibTiffTestCase):
out = self.tempfile("temp.tif")
im.save(out)
reread = Image.open(out)
with Image.open(out) as reread:
self.assertEqual(reread.info["compression"], im.info["compression"])
self.assertEqual(reread.getpixel((0, 0)), 480)
def test_g4_string_info(self):
"""Tests String data in info directory"""
test_file = "Tests/images/hopper_g4_500.tif"
orig = Image.open(test_file)
with Image.open(test_file) as orig:
out = self.tempfile("temp.tif")
orig.tag[269] = "temp.tif"
@ -407,7 +395,7 @@ class TestFileLibTiff(LibTiffTestCase):
""" Are we generating the same interpretation
of the image as Imagemagick is? """
TiffImagePlugin.READ_LIBTIFF = True
im = Image.open("Tests/images/12bit.cropped.tif")
with Image.open("Tests/images/12bit.cropped.tif") as im:
im.load()
TiffImagePlugin.READ_LIBTIFF = False
# to make the target --
@ -424,13 +412,13 @@ class TestFileLibTiff(LibTiffTestCase):
from PIL import ImageFilter
out = self.tempfile("temp.tif")
im = Image.open("Tests/images/pport_g4.tif")
with Image.open("Tests/images/pport_g4.tif") as im:
im = im.convert("L")
im = im.filter(ImageFilter.GaussianBlur(4))
im.save(out, compression="tiff_adobe_deflate")
im2 = Image.open(out)
with Image.open(out) as im2:
im2.load()
self.assert_image_equal(im, im2)
@ -446,17 +434,17 @@ class TestFileLibTiff(LibTiffTestCase):
for compression in ("packbits", "tiff_lzw"):
im.save(out, compression=compression)
size_compressed = os.path.getsize(out)
im2 = Image.open(out)
with Image.open(out) as im2:
self.assert_image_equal(im, im2)
im.save(out, compression="jpeg")
size_jpeg = os.path.getsize(out)
im2 = Image.open(out)
with Image.open(out) as im2:
self.assert_image_similar(im, im2, 30)
im.save(out, compression="jpeg", quality=30)
size_jpeg_30 = os.path.getsize(out)
im3 = Image.open(out)
with Image.open(out) as im3:
self.assert_image_similar(im2, im3, 30)
self.assertGreater(size_raw, size_compressed)
@ -479,7 +467,7 @@ class TestFileLibTiff(LibTiffTestCase):
out = self.tempfile("temp.tif")
im.save(out, compression="tiff_adobe_deflate")
im2 = Image.open(out)
with Image.open(out) as im2:
self.assert_image_equal(im, im2)
def xtest_bw_compression_w_rgb(self):
@ -543,7 +531,7 @@ class TestFileLibTiff(LibTiffTestCase):
def test__next(self):
TiffImagePlugin.READ_LIBTIFF = True
im = Image.open("Tests/images/hopper.tif")
with Image.open("Tests/images/hopper.tif") as im:
self.assertFalse(im.tag.next)
im.load()
self.assertFalse(im.tag.next)
@ -555,7 +543,7 @@ class TestFileLibTiff(LibTiffTestCase):
# Act
TiffImagePlugin.READ_LIBTIFF = True
im = Image.open(test_file)
with Image.open(test_file) as im:
TiffImagePlugin.READ_LIBTIFF = False
# Assert
@ -586,12 +574,12 @@ class TestFileLibTiff(LibTiffTestCase):
)
original = hopper("L")
for epsilon, group in test_files:
im = Image.open(group[0])
with Image.open(group[0]) as im:
self.assertEqual(im.size, (128, 128))
self.assertEqual(im.mode, "L")
self.assert_image_similar(im, original, epsilon)
for file in group[1:]:
im2 = Image.open(file)
with Image.open(file) as im2:
self.assertEqual(im2.size, (128, 128))
self.assertEqual(im2.mode, "L")
self.assert_image_equal(im, im2)
@ -612,7 +600,7 @@ class TestFileLibTiff(LibTiffTestCase):
pilim.save(buffer_io, format="tiff", compression=compression)
buffer_io.seek(0)
pilim_load = Image.open(buffer_io)
with Image.open(buffer_io) as pilim_load:
self.assert_image_similar(pilim, pilim_load, 0)
save_bytesio()
@ -625,7 +613,7 @@ class TestFileLibTiff(LibTiffTestCase):
def test_crashing_metadata(self):
# issue 1597
im = Image.open("Tests/images/rdf.tif")
with Image.open("Tests/images/rdf.tif") as im:
out = self.tempfile("temp.tif")
TiffImagePlugin.WRITE_LIBTIFF = True
@ -696,15 +684,13 @@ class TestFileLibTiff(LibTiffTestCase):
# Created with ImageMagick: convert hopper.jpg hopper_jpg.tif
# Contains JPEGTables (347) tag
infile = "Tests/images/hopper_jpg.tif"
im = Image.open(infile)
with Image.open(infile) as im:
# Act / Assert
# Should not raise UnicodeDecodeError or anything else
im.save(outfile)
def test_16bit_RGB_tiff(self):
im = Image.open("Tests/images/tiff_16bit_RGB.tiff")
with Image.open("Tests/images/tiff_16bit_RGB.tiff") as im:
self.assertEqual(im.mode, "RGB")
self.assertEqual(im.size, (100, 40))
self.assertEqual(
@ -723,17 +709,25 @@ class TestFileLibTiff(LibTiffTestCase):
self.assert_image_equal_tofile(im, "Tests/images/tiff_16bit_RGB_target.png")
def test_16bit_RGBa_tiff(self):
im = Image.open("Tests/images/tiff_16bit_RGBa.tiff")
with Image.open("Tests/images/tiff_16bit_RGBa.tiff") as im:
self.assertEqual(im.mode, "RGBA")
self.assertEqual(im.size, (100, 40))
self.assertEqual(
im.tile,
[("libtiff", (0, 0, 100, 40), 0, ("RGBa;16N", "tiff_lzw", False, 38236))],
[
(
"libtiff",
(0, 0, 100, 40),
0,
("RGBa;16N", "tiff_lzw", False, 38236),
)
],
)
im.load()
self.assert_image_equal_tofile(im, "Tests/images/tiff_16bit_RGBa_target.png")
self.assert_image_equal_tofile(
im, "Tests/images/tiff_16bit_RGBa_target.png"
)
def test_gimp_tiff(self):
# Read TIFF JPEG images from GIMP [@PIL168]
@ -743,12 +737,12 @@ class TestFileLibTiff(LibTiffTestCase):
self.skipTest("jpeg support not available")
filename = "Tests/images/pil168.tif"
im = Image.open(filename)
with Image.open(filename) as im:
self.assertEqual(im.mode, "RGB")
self.assertEqual(im.size, (256, 256))
self.assertEqual(
im.tile, [("libtiff", (0, 0, 256, 256), 0, ("RGB", "jpeg", False, 5122))]
im.tile,
[("libtiff", (0, 0, 256, 256), 0, ("RGB", "jpeg", False, 5122))],
)
im.load()
@ -756,14 +750,13 @@ class TestFileLibTiff(LibTiffTestCase):
def test_sampleformat(self):
# https://github.com/python-pillow/Pillow/issues/1466
im = Image.open("Tests/images/copyleft.tiff")
with Image.open("Tests/images/copyleft.tiff") as im:
self.assertEqual(im.mode, "RGB")
self.assert_image_equal_tofile(im, "Tests/images/copyleft.png", mode="RGB")
def test_lzw(self):
im = Image.open("Tests/images/hopper_lzw.tif")
with Image.open("Tests/images/hopper_lzw.tif") as im:
self.assertEqual(im.mode, "RGB")
self.assertEqual(im.size, (128, 128))
self.assertEqual(im.format, "TIFF")
@ -772,50 +765,48 @@ class TestFileLibTiff(LibTiffTestCase):
def test_strip_cmyk_jpeg(self):
infile = "Tests/images/tiff_strip_cmyk_jpeg.tif"
im = Image.open(infile)
self.assert_image_similar_tofile(im, "Tests/images/pil_sample_cmyk.jpg", 0.5)
with Image.open(infile) as im:
self.assert_image_similar_tofile(
im, "Tests/images/pil_sample_cmyk.jpg", 0.5
)
def test_strip_cmyk_16l_jpeg(self):
infile = "Tests/images/tiff_strip_cmyk_16l_jpeg.tif"
im = Image.open(infile)
self.assert_image_similar_tofile(im, "Tests/images/pil_sample_cmyk.jpg", 0.5)
with Image.open(infile) as im:
self.assert_image_similar_tofile(
im, "Tests/images/pil_sample_cmyk.jpg", 0.5
)
def test_strip_ycbcr_jpeg_2x2_sampling(self):
infile = "Tests/images/tiff_strip_ycbcr_jpeg_2x2_sampling.tif"
im = Image.open(infile)
with Image.open(infile) as im:
self.assert_image_similar_tofile(im, "Tests/images/flower.jpg", 0.5)
def test_strip_ycbcr_jpeg_1x1_sampling(self):
infile = "Tests/images/tiff_strip_ycbcr_jpeg_1x1_sampling.tif"
im = Image.open(infile)
with Image.open(infile) as im:
self.assert_image_equal_tofile(im, "Tests/images/flower2.jpg")
def test_tiled_cmyk_jpeg(self):
infile = "Tests/images/tiff_tiled_cmyk_jpeg.tif"
im = Image.open(infile)
self.assert_image_similar_tofile(im, "Tests/images/pil_sample_cmyk.jpg", 0.5)
with Image.open(infile) as im:
self.assert_image_similar_tofile(
im, "Tests/images/pil_sample_cmyk.jpg", 0.5
)
def test_tiled_ycbcr_jpeg_1x1_sampling(self):
infile = "Tests/images/tiff_tiled_ycbcr_jpeg_1x1_sampling.tif"
im = Image.open(infile)
with Image.open(infile) as im:
self.assert_image_equal_tofile(im, "Tests/images/flower2.jpg")
def test_tiled_ycbcr_jpeg_2x2_sampling(self):
infile = "Tests/images/tiff_tiled_ycbcr_jpeg_2x2_sampling.tif"
im = Image.open(infile)
with Image.open(infile) as im:
self.assert_image_similar_tofile(im, "Tests/images/flower.jpg", 0.5)
def test_old_style_jpeg(self):
infile = "Tests/images/old-style-jpeg-compression.tif"
im = Image.open(infile)
with Image.open(infile) as im:
self.assert_image_equal_tofile(
im, "Tests/images/old-style-jpeg-compression.png"
)
@ -828,10 +819,9 @@ class TestFileLibTiff(LibTiffTestCase):
self.assertEqual(im.size, (950, 975))
def test_orientation(self):
base_im = Image.open("Tests/images/g4_orientation_1.tif")
with Image.open("Tests/images/g4_orientation_1.tif") as base_im:
for i in range(2, 9):
im = Image.open("Tests/images/g4_orientation_" + str(i) + ".tif")
with Image.open("Tests/images/g4_orientation_" + str(i) + ".tif") as im:
im.load()
self.assert_image_similar(base_im, im, 0.7)

View File

@ -20,8 +20,7 @@ class TestFileLibTiffSmall(LibTiffTestCase):
test_file = "Tests/images/hopper_g4.tif"
with open(test_file, "rb") as f:
im = Image.open(f)
with Image.open(f) as im:
self.assertEqual(im.size, (128, 128))
self._assert_noerr(im)
@ -32,8 +31,7 @@ class TestFileLibTiffSmall(LibTiffTestCase):
with open(test_file, "rb") as f:
s.write(f.read())
s.seek(0)
im = Image.open(s)
with Image.open(s) as im:
self.assertEqual(im.size, (128, 128))
self._assert_noerr(im)
@ -41,7 +39,6 @@ class TestFileLibTiffSmall(LibTiffTestCase):
"""The 128x128 lena image failed for some reason."""
test_file = "Tests/images/hopper_g4.tif"
im = Image.open(test_file)
with Image.open(test_file) as im:
self.assertEqual(im.size, (128, 128))
self._assert_noerr(im)

View File

@ -17,12 +17,12 @@ class TestFileMcIdas(PillowTestCase):
saved_file = "Tests/images/cmx3g8_wv_1998.260_0745_mcidas.png"
# Act
im = Image.open(test_file)
with Image.open(test_file) as im:
im.load()
# Assert
self.assertEqual(im.format, "MCIDAS")
self.assertEqual(im.mode, "I")
self.assertEqual(im.size, (1800, 400))
im2 = Image.open(saved_file)
with Image.open(saved_file) as im2:
self.assert_image_equal(im, im2)

View File

@ -113,6 +113,13 @@ class TestFileMpo(PillowTestCase):
self.assertEqual(mpinfo[45056], b"0100")
self.assertEqual(mpinfo[45057], 2)
def test_mp_no_data(self):
# This image has been manually hexedited to have the second frame
# beyond the end of the file
with Image.open("Tests/images/sugarshack_no_data.mpo") as im:
with self.assertRaises(ValueError):
im.seek(1)
def test_mp_attribute(self):
for test_file in test_files:
with Image.open(test_file) as im:

View File

@ -16,7 +16,7 @@ class TestFileMsp(PillowTestCase):
hopper("1").save(test_file)
im = Image.open(test_file)
with Image.open(test_file) as im:
im.load()
self.assertEqual(im.mode, "1")
self.assertEqual(im.size, (128, 128))
@ -38,7 +38,7 @@ class TestFileMsp(PillowTestCase):
def test_open_windows_v1(self):
# Arrange
# Act
im = Image.open(TEST_FILE)
with Image.open(TEST_FILE) as im:
# Assert
self.assert_image_equal(im, hopper("1"))
@ -46,7 +46,7 @@ class TestFileMsp(PillowTestCase):
def _assert_file_image_equal(self, source_path, target_path):
with Image.open(source_path) as im:
target = Image.open(target_path)
with Image.open(target_path) as target:
self.assert_image_equal(im, target)
@unittest.skipIf(not os.path.exists(EXTRA_DIR), "Extra image files not installed")

View File

@ -5,7 +5,7 @@ from .helper import PillowTestCase
class TestFilePcd(PillowTestCase):
def test_load_raw(self):
im = Image.open("Tests/images/hopper.pcd")
with Image.open("Tests/images/hopper.pcd") as im:
im.load() # should not segfault.
# Note that this image was created with a resized hopper

View File

@ -7,8 +7,7 @@ class TestFilePcx(PillowTestCase):
def _roundtrip(self, im):
f = self.tempfile("temp.pcx")
im.save(f)
im2 = Image.open(f)
with Image.open(f) as im2:
self.assertEqual(im2.mode, im.mode)
self.assertEqual(im2.size, im.size)
self.assertEqual(im2.format, "PCX")
@ -42,8 +41,7 @@ class TestFilePcx(PillowTestCase):
# Check reading of files where xmin/xmax is not zero.
test_file = "Tests/images/pil184.pcx"
im = Image.open(test_file)
with Image.open(test_file) as im:
self.assertEqual(im.size, (447, 144))
self.assertEqual(im.tile[0][1], (0, 0, 447, 144))

View File

@ -107,7 +107,7 @@ class TestFilePdf(PillowTestCase):
self.assertGreater(os.path.getsize(outfile), 0)
# Append JPEG images
jpeg = Image.open("Tests/images/flower.jpg")
with Image.open("Tests/images/flower.jpg") as jpeg:
jpeg.save(outfile, save_all=True, append_images=[jpeg.copy()])
self.assertTrue(os.path.isfile(outfile))

View File

@ -7,7 +7,7 @@ TEST_FILE = "Tests/images/hopper.pxr"
class TestFilePixar(PillowTestCase):
def test_sanity(self):
im = Image.open(TEST_FILE)
with Image.open(TEST_FILE) as im:
im.load()
self.assertEqual(im.mode, "RGB")
self.assertEqual(im.size, (128, 128))

View File

@ -91,7 +91,7 @@ class TestFilePng(PillowTestCase):
for mode in ["1", "L", "P", "RGB", "I", "I;16"]:
im = hopper(mode)
im.save(test_file)
reloaded = Image.open(test_file)
with Image.open(test_file) as reloaded:
if mode == "I;16":
reloaded = reloaded.convert(mode)
self.assert_image_equal(reloaded, im)
@ -195,16 +195,14 @@ class TestFilePng(PillowTestCase):
def test_interlace(self):
test_file = "Tests/images/pil123p.png"
im = Image.open(test_file)
with Image.open(test_file) as im:
self.assert_image(im, "P", (162, 150))
self.assertTrue(im.info.get("interlace"))
im.load()
test_file = "Tests/images/pil123rgba.png"
im = Image.open(test_file)
with Image.open(test_file) as im:
self.assert_image(im, "RGBA", (162, 150))
self.assertTrue(im.info.get("interlace"))
@ -212,8 +210,7 @@ class TestFilePng(PillowTestCase):
def test_load_transparent_p(self):
test_file = "Tests/images/pil123p.png"
im = Image.open(test_file)
with Image.open(test_file) as im:
self.assert_image(im, "P", (162, 150))
im = im.convert("RGBA")
self.assert_image(im, "RGBA", (162, 150))
@ -223,7 +220,7 @@ class TestFilePng(PillowTestCase):
def test_load_transparent_rgb(self):
test_file = "Tests/images/rgb_trns.png"
im = Image.open(test_file)
with Image.open(test_file) as im:
self.assertEqual(im.info["transparency"], (0, 255, 52))
self.assert_image(im, "RGB", (64, 64))
@ -235,8 +232,7 @@ class TestFilePng(PillowTestCase):
def test_save_p_transparent_palette(self):
in_file = "Tests/images/pil123p.png"
im = Image.open(in_file)
with Image.open(in_file) as im:
# 'transparency' contains a byte string with the opacity for
# each palette entry
self.assertEqual(len(im.info["transparency"]), 256)
@ -245,7 +241,7 @@ class TestFilePng(PillowTestCase):
im.save(test_file)
# check if saved image contains same transparency
im = Image.open(test_file)
with Image.open(test_file) as im:
self.assertEqual(len(im.info["transparency"]), 256)
self.assert_image(im, "P", (162, 150))
@ -257,8 +253,7 @@ class TestFilePng(PillowTestCase):
def test_save_p_single_transparency(self):
in_file = "Tests/images/p_trns_single.png"
im = Image.open(in_file)
with Image.open(in_file) as im:
# pixel value 164 is full transparent
self.assertEqual(im.info["transparency"], 164)
self.assertEqual(im.getpixel((31, 31)), 164)
@ -267,7 +262,7 @@ class TestFilePng(PillowTestCase):
im.save(test_file)
# check if saved image contains same transparency
im = Image.open(test_file)
with Image.open(test_file) as im:
self.assertEqual(im.info["transparency"], 164)
self.assertEqual(im.getpixel((31, 31)), 164)
self.assert_image(im, "P", (64, 64))
@ -290,7 +285,7 @@ class TestFilePng(PillowTestCase):
im.save(test_file)
# check if saved image contains same transparency
im = Image.open(test_file)
with Image.open(test_file) as im:
self.assertEqual(len(im.info["transparency"]), 256)
self.assert_image(im, "P", (10, 10))
im = im.convert("RGBA")
@ -300,7 +295,7 @@ class TestFilePng(PillowTestCase):
def test_save_greyscale_transparency(self):
for mode, num_transparent in {"1": 1994, "L": 559, "I": 559}.items():
in_file = "Tests/images/" + mode.lower() + "_trns.png"
im = Image.open(in_file)
with Image.open(in_file) as im:
self.assertEqual(im.mode, mode)
self.assertEqual(im.info["transparency"], 255)
@ -310,7 +305,7 @@ class TestFilePng(PillowTestCase):
test_file = self.tempfile("temp.png")
im.save(test_file)
test_im = Image.open(test_file)
with Image.open(test_file) as test_im:
self.assertEqual(test_im.mode, mode)
self.assertEqual(test_im.info["transparency"], 255)
self.assert_image_equal(im, test_im)
@ -322,20 +317,18 @@ class TestFilePng(PillowTestCase):
def test_save_rgb_single_transparency(self):
in_file = "Tests/images/caption_6_33_22.png"
im = Image.open(in_file)
with Image.open(in_file) as im:
test_file = self.tempfile("temp.png")
im.save(test_file)
def test_load_verify(self):
# Check open/load/verify exception (@PIL150)
im = Image.open(TEST_PNG_FILE)
with Image.open(TEST_PNG_FILE) as im:
# Assert that there is no unclosed file warning
self.assert_warning(None, im.verify)
im = Image.open(TEST_PNG_FILE)
with Image.open(TEST_PNG_FILE) as im:
im.load()
self.assertRaises(RuntimeError, im.verify)
@ -350,7 +343,7 @@ class TestFilePng(PillowTestCase):
with open(TEST_PNG_FILE, "rb") as f:
test_file = f.read()[:offset]
im = Image.open(BytesIO(test_file))
with Image.open(BytesIO(test_file)) as im:
self.assertIsNotNone(im.fp)
self.assertRaises((IOError, SyntaxError), im.verify)
@ -386,8 +379,7 @@ class TestFilePng(PillowTestCase):
def test_roundtrip_dpi(self):
# Check dpi roundtripping
im = Image.open(TEST_PNG_FILE)
with Image.open(TEST_PNG_FILE) as im:
im = roundtrip(im, dpi=(100, 100))
self.assertEqual(im.info["dpi"], (100, 100))
@ -401,8 +393,7 @@ class TestFilePng(PillowTestCase):
self.assertEqual(im.info["dpi"], (72, 72))
def test_save_dpi_rounding(self):
im = Image.open(TEST_PNG_FILE)
with Image.open(TEST_PNG_FILE) as im:
im = roundtrip(im, dpi=(72.2, 72.2))
self.assertEqual(im.info["dpi"], (72, 72))
@ -412,8 +403,7 @@ class TestFilePng(PillowTestCase):
def test_roundtrip_text(self):
# Check text roundtripping
im = Image.open(TEST_PNG_FILE)
with Image.open(TEST_PNG_FILE) as im:
info = PngImagePlugin.PngInfo()
info.add_text("TXT", "VALUE")
info.add_text("ZIP", "VALUE", zip=True)
@ -480,7 +470,7 @@ class TestFilePng(PillowTestCase):
# Independent file sample provided by Sebastian Spaeth.
test_file = "Tests/images/caption_6_33_22.png"
im = Image.open(test_file)
with Image.open(test_file) as im:
self.assertEqual(im.info["transparency"], (248, 248, 248))
# check saving transparency by default
@ -498,7 +488,7 @@ class TestFilePng(PillowTestCase):
f = self.tempfile("temp.png")
im.save(f)
im2 = Image.open(f)
with Image.open(f) as im2:
self.assertIn("transparency", im2.info)
self.assert_image_equal(im2.convert("RGBA"), im.convert("RGBA"))
@ -521,20 +511,19 @@ class TestFilePng(PillowTestCase):
self.assertEqual(im.info["icc_profile"], expected_icc)
def test_discard_icc_profile(self):
im = Image.open("Tests/images/icc_profile.png")
with Image.open("Tests/images/icc_profile.png") as im:
im = roundtrip(im, icc_profile=None)
self.assertNotIn("icc_profile", im.info)
def test_roundtrip_icc_profile(self):
im = Image.open("Tests/images/icc_profile.png")
with Image.open("Tests/images/icc_profile.png") as im:
expected_icc = im.info["icc_profile"]
im = roundtrip(im)
self.assertEqual(im.info["icc_profile"], expected_icc)
def test_roundtrip_no_icc_profile(self):
im = Image.open("Tests/images/icc_profile_none.png")
with Image.open("Tests/images/icc_profile_none.png") as im:
self.assertIsNone(im.info["icc_profile"])
im = roundtrip(im)
@ -543,12 +532,12 @@ class TestFilePng(PillowTestCase):
def test_repr_png(self):
im = hopper()
repr_png = Image.open(BytesIO(im._repr_png_()))
with Image.open(BytesIO(im._repr_png_())) as repr_png:
self.assertEqual(repr_png.format, "PNG")
self.assert_image_equal(im, repr_png)
def test_chunk_order(self):
im = Image.open("Tests/images/icc_profile.png")
with Image.open("Tests/images/icc_profile.png") as im:
test_file = self.tempfile("temp.png")
im.convert("P").save(test_file, dpi=(100, 100))
@ -575,7 +564,7 @@ class TestFilePng(PillowTestCase):
self.assertEqual(len(chunks), 3)
def test_textual_chunks_after_idat(self):
im = Image.open("Tests/images/hopper.png")
with Image.open("Tests/images/hopper.png") as im:
self.assertIn("comment", im.text.keys())
for k, v in {
"date:create": "2014-09-04T09:37:08+03:00",
@ -584,12 +573,12 @@ class TestFilePng(PillowTestCase):
self.assertEqual(im.text[k], v)
# Raises a SyntaxError in load_end
im = Image.open("Tests/images/broken_data_stream.png")
with Image.open("Tests/images/broken_data_stream.png") as im:
with self.assertRaises(IOError):
self.assertIsInstance(im.text, dict)
# Raises a UnicodeDecodeError in load_end
im = Image.open("Tests/images/truncated_image.png")
with Image.open("Tests/images/truncated_image.png") as im:
# The file is truncated
self.assertRaises(IOError, lambda: im.text)
ImageFile.LOAD_TRUNCATED_IMAGES = True
@ -597,17 +586,16 @@ class TestFilePng(PillowTestCase):
ImageFile.LOAD_TRUNCATED_IMAGES = False
# Raises an EOFError in load_end
im = Image.open("Tests/images/hopper_idat_after_image_end.png")
with Image.open("Tests/images/hopper_idat_after_image_end.png") as im:
self.assertEqual(im.text, {"TXT": "VALUE", "ZIP": "VALUE"})
def test_exif(self):
im = Image.open("Tests/images/exif.png")
with Image.open("Tests/images/exif.png") as im:
exif = im._getexif()
self.assertEqual(exif[274], 1)
def test_exif_save(self):
im = Image.open("Tests/images/exif.png")
with Image.open("Tests/images/exif.png") as im:
test_file = self.tempfile("temp.png")
im.save(test_file)
@ -616,8 +604,7 @@ class TestFilePng(PillowTestCase):
self.assertEqual(exif[274], 1)
def test_exif_from_jpg(self):
im = Image.open("Tests/images/pil_sample_rgb.jpg")
with Image.open("Tests/images/pil_sample_rgb.jpg") as im:
test_file = self.tempfile("temp.png")
im.save(test_file)
@ -626,8 +613,7 @@ class TestFilePng(PillowTestCase):
self.assertEqual(exif[305], "Adobe Photoshop CS Macintosh")
def test_exif_argument(self):
im = Image.open(TEST_PNG_FILE)
with Image.open(TEST_PNG_FILE) as im:
test_file = self.tempfile("temp.png")
im.save(test_file, exif=b"exifstring")
@ -638,11 +624,11 @@ class TestFilePng(PillowTestCase):
HAVE_WEBP and _webp.HAVE_WEBPANIM, "WebP support not installed with animation"
)
def test_apng(self):
im = Image.open("Tests/images/iss634.apng")
with Image.open("Tests/images/iss634.apng") as im:
self.assertEqual(im.get_format_mimetype(), "image/apng")
# This also tests reading unknown PNG chunks (fcTL and fdAT) in load_end
expected = Image.open("Tests/images/iss634.webp")
with Image.open("Tests/images/iss634.webp") as expected:
self.assert_image_similar(im, expected, 0.23)

View File

@ -8,7 +8,7 @@ test_file = "Tests/images/hopper.ppm"
class TestFilePpm(PillowTestCase):
def test_sanity(self):
im = Image.open(test_file)
with Image.open(test_file) as im:
im.load()
self.assertEqual(im.mode, "RGB")
self.assertEqual(im.size, (128, 128))
@ -16,33 +16,33 @@ class TestFilePpm(PillowTestCase):
self.assertEqual(im.get_format_mimetype(), "image/x-portable-pixmap")
def test_16bit_pgm(self):
im = Image.open("Tests/images/16_bit_binary.pgm")
with Image.open("Tests/images/16_bit_binary.pgm") as im:
im.load()
self.assertEqual(im.mode, "I")
self.assertEqual(im.size, (20, 100))
self.assertEqual(im.get_format_mimetype(), "image/x-portable-graymap")
tgt = Image.open("Tests/images/16_bit_binary_pgm.png")
with Image.open("Tests/images/16_bit_binary_pgm.png") as tgt:
self.assert_image_equal(im, tgt)
def test_16bit_pgm_write(self):
im = Image.open("Tests/images/16_bit_binary.pgm")
with Image.open("Tests/images/16_bit_binary.pgm") as im:
im.load()
f = self.tempfile("temp.pgm")
im.save(f, "PPM")
reloaded = Image.open(f)
with Image.open(f) as reloaded:
self.assert_image_equal(im, reloaded)
def test_pnm(self):
im = Image.open("Tests/images/hopper.pnm")
with Image.open("Tests/images/hopper.pnm") as im:
self.assert_image_similar(im, hopper(), 0.0001)
f = self.tempfile("temp.pnm")
im.save(f)
reloaded = Image.open(f)
with Image.open(f) as reloaded:
self.assert_image_equal(im, reloaded)
def test_truncated_file(self):

View File

@ -9,14 +9,14 @@ class TestFileSgi(PillowTestCase):
# convert hopper.ppm -compress None sgi:hopper.rgb
test_file = "Tests/images/hopper.rgb"
im = Image.open(test_file)
with Image.open(test_file) as im:
self.assert_image_equal(im, hopper())
self.assertEqual(im.get_format_mimetype(), "image/rgb")
def test_rgb16(self):
test_file = "Tests/images/hopper16.rgb"
im = Image.open(test_file)
with Image.open(test_file) as im:
self.assert_image_equal(im, hopper())
def test_l(self):
@ -24,7 +24,7 @@ class TestFileSgi(PillowTestCase):
# convert hopper.ppm -monochrome -compress None sgi:hopper.bw
test_file = "Tests/images/hopper.bw"
im = Image.open(test_file)
with Image.open(test_file) as im:
self.assert_image_similar(im, hopper("L"), 2)
self.assertEqual(im.get_format_mimetype(), "image/sgi")
@ -33,8 +33,8 @@ class TestFileSgi(PillowTestCase):
# convert transparent.png -compress None transparent.sgi
test_file = "Tests/images/transparent.sgi"
im = Image.open(test_file)
target = Image.open("Tests/images/transparent.png")
with Image.open(test_file) as im:
with Image.open("Tests/images/transparent.png") as target:
self.assert_image_equal(im, target)
self.assertEqual(im.get_format_mimetype(), "image/sgi")
@ -43,15 +43,15 @@ class TestFileSgi(PillowTestCase):
# convert hopper.ppm hopper.sgi
test_file = "Tests/images/hopper.sgi"
im = Image.open(test_file)
target = Image.open("Tests/images/hopper.rgb")
with Image.open(test_file) as im:
with Image.open("Tests/images/hopper.rgb") as target:
self.assert_image_equal(im, target)
def test_rle16(self):
test_file = "Tests/images/tv16.sgi"
im = Image.open(test_file)
target = Image.open("Tests/images/tv.rgb")
with Image.open(test_file) as im:
with Image.open("Tests/images/tv.rgb") as target:
self.assert_image_equal(im, target)
def test_invalid_file(self):
@ -63,7 +63,7 @@ class TestFileSgi(PillowTestCase):
def roundtrip(img):
out = self.tempfile("temp.sgi")
img.save(out, format="sgi")
reloaded = Image.open(out)
with Image.open(out) as reloaded:
self.assert_image_equal(img, reloaded)
for mode in ("L", "RGB", "RGBA"):
@ -75,11 +75,11 @@ class TestFileSgi(PillowTestCase):
def test_write16(self):
test_file = "Tests/images/hopper16.rgb"
im = Image.open(test_file)
with Image.open(test_file) as im:
out = self.tempfile("temp.sgi")
im.save(out, format="sgi", bpc=2)
reloaded = Image.open(out)
with Image.open(out) as reloaded:
self.assert_image_equal(im, reloaded)
def test_unsupported_mode(self):

View File

@ -64,7 +64,7 @@ class TestImageSpider(PillowTestCase):
# Assert
fp.seek(0)
reloaded = Image.open(fp)
with Image.open(fp) as reloaded:
self.assertEqual(reloaded.mode, "F")
self.assertEqual(reloaded.size, (128, 128))
self.assertEqual(reloaded.format, "SPIDER")
@ -143,5 +143,5 @@ class TestImageSpider(PillowTestCase):
im.save(data, format="SPIDER")
data.seek(0)
im2 = Image.open(data)
with Image.open(data) as im2:
self.assert_image_equal(im, im2)

View File

@ -15,7 +15,7 @@ class TestFileSun(PillowTestCase):
test_file = "Tests/images/hopper.ras"
# Act
im = Image.open(test_file)
with Image.open(test_file) as im:
# Assert
self.assertEqual(im.size, (128, 128))
@ -26,8 +26,8 @@ class TestFileSun(PillowTestCase):
self.assertRaises(SyntaxError, SunImagePlugin.SunImageFile, invalid_file)
def test_im1(self):
im = Image.open("Tests/images/sunraster.im1")
target = Image.open("Tests/images/sunraster.im1.png")
with Image.open("Tests/images/sunraster.im1") as im:
with Image.open("Tests/images/sunraster.im1.png") as target:
self.assert_image_equal(im, target)
@unittest.skipIf(not os.path.exists(EXTRA_DIR), "Extra image files not installed")

View File

@ -22,7 +22,7 @@ class TestFileTar(PillowTestCase):
]:
if codec in codecs:
with TarIO.TarIO(TEST_TAR_FILE, test_path) as tar:
im = Image.open(tar)
with Image.open(tar) as im:
im.load()
self.assertEqual(im.mode, "RGB")
self.assertEqual(im.size, (128, 128))

View File

@ -24,7 +24,7 @@ class TestFileTga(PillowTestCase):
)
for png_path in png_paths:
reference_im = Image.open(png_path)
with Image.open(png_path) as reference_im:
self.assertEqual(reference_im.mode, mode)
path_no_ext = os.path.splitext(png_path)[0]
@ -33,11 +33,15 @@ class TestFileTga(PillowTestCase):
path_no_ext, origin, "rle" if rle else "raw"
)
original_im = Image.open(tga_path)
with Image.open(tga_path) as original_im:
self.assertEqual(original_im.format, "TGA")
self.assertEqual(original_im.get_format_mimetype(), "image/x-tga")
self.assertEqual(
original_im.get_format_mimetype(), "image/x-tga"
)
if rle:
self.assertEqual(original_im.info["compression"], "tga_rle")
self.assertEqual(
original_im.info["compression"], "tga_rle"
)
self.assertEqual(
original_im.info["orientation"],
self._ORIGIN_TO_ORIENTATION[origin],
@ -55,14 +59,15 @@ class TestFileTga(PillowTestCase):
out = self.tempfile("temp.tga")
original_im.save(out, rle=rle)
saved_im = Image.open(out)
with Image.open(out) as saved_im:
if rle:
self.assertEqual(
saved_im.info["compression"],
original_im.info["compression"],
)
self.assertEqual(
saved_im.info["orientation"], original_im.info["orientation"]
saved_im.info["orientation"],
original_im.info["orientation"],
)
if mode == "P":
self.assertEqual(
@ -93,8 +98,7 @@ class TestFileTga(PillowTestCase):
def test_save(self):
test_file = "Tests/images/tga_id_field.tga"
im = Image.open(test_file)
with Image.open(test_file) as im:
out = self.tempfile("temp.tga")
# Save
@ -110,8 +114,7 @@ class TestFileTga(PillowTestCase):
def test_save_id_section(self):
test_file = "Tests/images/rgb32rle.tga"
im = Image.open(test_file)
with Image.open(test_file) as im:
out = self.tempfile("temp.tga")
# Check there is no id section
@ -140,10 +143,9 @@ class TestFileTga(PillowTestCase):
def test_save_orientation(self):
test_file = "Tests/images/rgb32rle.tga"
im = Image.open(test_file)
self.assertEqual(im.info["orientation"], -1)
out = self.tempfile("temp.tga")
with Image.open(test_file) as im:
self.assertEqual(im.info["orientation"], -1)
im.save(out, orientation=1)
with Image.open(out) as test_im:
@ -151,7 +153,7 @@ class TestFileTga(PillowTestCase):
def test_save_rle(self):
test_file = "Tests/images/rgb32rle.tga"
im = Image.open(test_file)
with Image.open(test_file) as im:
self.assertEqual(im.info["compression"], "tga_rle")
out = self.tempfile("temp.tga")
@ -173,7 +175,7 @@ class TestFileTga(PillowTestCase):
self.assertEqual(test_im.size, (199, 199))
test_file = "Tests/images/tga_id_field.tga"
im = Image.open(test_file)
with Image.open(test_file) as im:
self.assertNotIn("compression", im.info)
# Save with compression
@ -186,14 +188,14 @@ class TestFileTga(PillowTestCase):
num_transparent = 559
in_file = "Tests/images/la.tga"
im = Image.open(in_file)
with Image.open(in_file) as im:
self.assertEqual(im.mode, "LA")
self.assertEqual(im.getchannel("A").getcolors()[0][0], num_transparent)
out = self.tempfile("temp.tga")
im.save(out)
test_im = Image.open(out)
with Image.open(out) as test_im:
self.assertEqual(test_im.mode, "LA")
self.assertEqual(test_im.getchannel("A").getcolors()[0][0], num_transparent)

View File

@ -72,8 +72,7 @@ class TestFileTiff(PillowTestCase):
# Read RGBa images from macOS [@PIL136]
filename = "Tests/images/pil136.tiff"
im = Image.open(filename)
with Image.open(filename) as im:
self.assertEqual(im.mode, "RGBA")
self.assertEqual(im.size, (55, 43))
self.assertEqual(im.tile, [("raw", (0, 0, 55, 43), 8, ("RGBa", 0, 1))])
@ -82,8 +81,7 @@ class TestFileTiff(PillowTestCase):
self.assert_image_similar_tofile(im, "Tests/images/pil136.png", 1)
def test_wrong_bits_per_sample(self):
im = Image.open("Tests/images/tiff_wrong_bits_per_sample.tiff")
with Image.open("Tests/images/tiff_wrong_bits_per_sample.tiff") as im:
self.assertEqual(im.mode, "RGBA")
self.assertEqual(im.size, (52, 53))
self.assertEqual(im.tile, [("raw", (0, 0, 52, 53), 160, ("RGBA", 0, 1))])
@ -149,12 +147,11 @@ class TestFileTiff(PillowTestCase):
def test_save_dpi_rounding(self):
outfile = self.tempfile("temp.tif")
im = Image.open("Tests/images/hopper.tif")
with Image.open("Tests/images/hopper.tif") as im:
for dpi in (72.2, 72.8):
im.save(outfile, dpi=(dpi, dpi))
reloaded = Image.open(outfile)
with Image.open(outfile) as reloaded:
reloaded.load()
self.assertEqual((round(dpi), round(dpi)), reloaded.info["dpi"])
@ -163,7 +160,7 @@ class TestFileTiff(PillowTestCase):
Image.open("Tests/images/10ct_32bit_128.tiff").save(
b, format="tiff", resolution=123.45
)
im = Image.open(b)
with Image.open(b) as im:
self.assertEqual(float(im.tag_v2[X_RESOLUTION]), 123.45)
self.assertEqual(float(im.tag_v2[Y_RESOLUTION]), 123.45)
@ -192,7 +189,7 @@ class TestFileTiff(PillowTestCase):
self.assertRaises(IOError, im.save, outfile)
def test_little_endian(self):
im = Image.open("Tests/images/16bit.cropped.tif")
with Image.open("Tests/images/16bit.cropped.tif") as im:
self.assertEqual(im.getpixel((0, 0)), 480)
self.assertEqual(im.mode, "I;16")
@ -202,18 +199,17 @@ class TestFileTiff(PillowTestCase):
self.assertEqual(b[1], ord(b"\x01"))
def test_big_endian(self):
im = Image.open("Tests/images/16bit.MM.cropped.tif")
with Image.open("Tests/images/16bit.MM.cropped.tif") as im:
self.assertEqual(im.getpixel((0, 0)), 480)
self.assertEqual(im.mode, "I;16B")
b = im.tobytes()
# Bytes are in image native order (big endian)
self.assertEqual(b[0], ord(b"\x01"))
self.assertEqual(b[1], ord(b"\xe0"))
def test_16bit_s(self):
im = Image.open("Tests/images/16bit.s.tif")
with Image.open("Tests/images/16bit.s.tif") as im:
im.load()
self.assertEqual(im.mode, "I")
self.assertEqual(im.getpixel((0, 0)), 32767)
@ -223,8 +219,7 @@ class TestFileTiff(PillowTestCase):
""" Are we generating the same interpretation
of the image as Imagemagick is? """
im = Image.open("Tests/images/12bit.cropped.tif")
with Image.open("Tests/images/12bit.cropped.tif") as im:
# to make the target --
# convert 12bit.cropped.tif -depth 16 tmp.tif
# convert tmp.tif -evaluate RightShift 4 12in16bit2.tif
@ -236,7 +231,7 @@ class TestFileTiff(PillowTestCase):
def test_32bit_float(self):
# Issue 614, specific 32-bit float format
path = "Tests/images/10ct_32bit_128.tiff"
im = Image.open(path)
with Image.open(path) as im:
im.load()
self.assertEqual(im.getpixel((0, 0)), -0.4526388943195343)
@ -292,7 +287,7 @@ class TestFileTiff(PillowTestCase):
self.assertEqual(im.convert("RGB").getpixel((0, 0)), (0, 0, 255))
def test_multipage_last_frame(self):
im = Image.open("Tests/images/multipage-lastframe.tif")
with Image.open("Tests/images/multipage-lastframe.tif") as im:
im.load()
self.assertEqual(im.size, (20, 20))
self.assertEqual(im.convert("RGB").getpixel((0, 0)), (0, 0, 255))
@ -411,7 +406,7 @@ class TestFileTiff(PillowTestCase):
def test_4bit(self):
test_file = "Tests/images/hopper_gray_4bpp.tif"
original = hopper("L")
im = Image.open(test_file)
with Image.open(test_file) as im:
self.assertEqual(im.size, (128, 128))
self.assertEqual(im.mode, "L")
self.assert_image_similar(im, original, 7.3)
@ -439,12 +434,12 @@ class TestFileTiff(PillowTestCase):
)
original = hopper("L")
for epsilon, group in test_files:
im = Image.open(group[0])
with Image.open(group[0]) as im:
self.assertEqual(im.size, (128, 128))
self.assertEqual(im.mode, "L")
self.assert_image_similar(im, original, epsilon)
for file in group[1:]:
im2 = Image.open(file)
with Image.open(file) as im2:
self.assertEqual(im2.size, (128, 128))
self.assertEqual(im2.mode, "L")
self.assert_image_equal(im, im2)
@ -467,28 +462,25 @@ class TestFileTiff(PillowTestCase):
# Test an image of all '0' values
pixel_value = 0x1234
infile = "Tests/images/uint16_1_4660.tif"
im = Image.open(infile)
with Image.open(infile) as im:
self.assertEqual(im.getpixel((0, 0)), pixel_value)
tmpfile = self.tempfile("temp.tif")
im.save(tmpfile)
reloaded = Image.open(tmpfile)
with Image.open(tmpfile) as reloaded:
self.assert_image_equal(im, reloaded)
def test_strip_raw(self):
infile = "Tests/images/tiff_strip_raw.tif"
im = Image.open(infile)
with Image.open(infile) as im:
self.assert_image_equal_tofile(im, "Tests/images/tiff_adobe_deflate.png")
def test_strip_planar_raw(self):
# gdal_translate -of GTiff -co INTERLEAVE=BAND \
# tiff_strip_raw.tif tiff_strip_planar_raw.tiff
infile = "Tests/images/tiff_strip_planar_raw.tif"
im = Image.open(infile)
with Image.open(infile) as im:
self.assert_image_equal_tofile(im, "Tests/images/tiff_adobe_deflate.png")
def test_strip_planar_raw_with_overviews(self):
@ -502,8 +494,7 @@ class TestFileTiff(PillowTestCase):
# -co BLOCKYSIZE=32 -co INTERLEAVE=BAND \
# tiff_tiled_raw.tif tiff_tiled_planar_raw.tiff
infile = "Tests/images/tiff_tiled_planar_raw.tif"
im = Image.open(infile)
with Image.open(infile) as im:
self.assert_image_equal_tofile(im, "Tests/images/tiff_adobe_deflate.png")
def test_palette(self):
@ -513,7 +504,7 @@ class TestFileTiff(PillowTestCase):
im = hopper(mode)
im.save(outfile)
reloaded = Image.open(outfile)
with Image.open(outfile) as reloaded:
self.assert_image_equal(im.convert("RGB"), reloaded.convert("RGB"))
def test_tiff_save_all(self):
@ -532,7 +523,7 @@ class TestFileTiff(PillowTestCase):
im.copy().save(mp, format="TIFF", save_all=True, append_images=ims)
mp.seek(0, os.SEEK_SET)
reread = Image.open(mp)
with Image.open(mp) as reread:
self.assertEqual(reread.n_frames, 3)
# Test appending using a generator
@ -543,7 +534,7 @@ class TestFileTiff(PillowTestCase):
im.save(mp, format="TIFF", save_all=True, append_images=imGenerator(ims))
mp.seek(0, os.SEEK_SET)
reread = Image.open(mp)
with Image.open(mp) as reread:
self.assertEqual(reread.n_frames, 3)
def test_saving_icc_profile(self):

View File

@ -130,14 +130,13 @@ class TestFileTiffMetadata(PillowTestCase):
def test_write_metadata(self):
""" Test metadata writing through the python code """
img = Image.open("Tests/images/hopper.tif")
with Image.open("Tests/images/hopper.tif") as img:
f = self.tempfile("temp.tiff")
img.save(f, tiffinfo=img.tag)
with Image.open(f) as loaded:
original = img.tag_v2.named()
with Image.open(f) as loaded:
reloaded = loaded.tag_v2.named()
for k, v in original.items():
@ -187,10 +186,10 @@ class TestFileTiffMetadata(PillowTestCase):
def test_iccprofile(self):
# https://github.com/python-pillow/Pillow/issues/1462
im = Image.open("Tests/images/hopper.iccprofile.tif")
out = self.tempfile("temp.tiff")
with Image.open("Tests/images/hopper.iccprofile.tif") as im:
im.save(out)
with Image.open(out) as reloaded:
self.assertNotIsInstance(im.info["icc_profile"], tuple)
self.assertEqual(im.info["icc_profile"], reloaded.info["icc_profile"])
@ -205,12 +204,12 @@ class TestFileTiffMetadata(PillowTestCase):
self.assertTrue(im.info["icc_profile"])
def test_iccprofile_save_png(self):
im = Image.open("Tests/images/hopper.iccprofile.tif")
with Image.open("Tests/images/hopper.iccprofile.tif") as im:
outfile = self.tempfile("temp.png")
im.save(outfile)
def test_iccprofile_binary_save_png(self):
im = Image.open("Tests/images/hopper.iccprofile_binary.tif")
with Image.open("Tests/images/hopper.iccprofile_binary.tif") as im:
outfile = self.tempfile("temp.png")
im.save(outfile)
@ -241,8 +240,7 @@ class TestFileTiffMetadata(PillowTestCase):
self.assertIn(33432, info)
def test_PhotoshopInfo(self):
im = Image.open("Tests/images/issue_2278.tif")
with Image.open("Tests/images/issue_2278.tif") as im:
self.assertEqual(len(im.tag_v2[34377]), 1)
self.assertIsInstance(im.tag_v2[34377][0], bytes)
out = self.tempfile("temp.tiff")

View File

@ -41,8 +41,7 @@ class TestFileWebp(PillowTestCase):
Does it have the bits we expect?
"""
image = Image.open("Tests/images/hopper.webp")
with Image.open("Tests/images/hopper.webp") as image:
self.assertEqual(image.mode, self.rgb_mode)
self.assertEqual(image.size, (128, 128))
self.assertEqual(image.format, "WEBP")
@ -64,8 +63,7 @@ class TestFileWebp(PillowTestCase):
temp_file = self.tempfile("temp.webp")
hopper(self.rgb_mode).save(temp_file)
image = Image.open(temp_file)
with Image.open(temp_file) as image:
self.assertEqual(image.mode, self.rgb_mode)
self.assertEqual(image.size, (128, 128))
self.assertEqual(image.format, "WEBP")
@ -93,8 +91,7 @@ class TestFileWebp(PillowTestCase):
temp_file = self.tempfile("temp.webp")
hopper("L").save(temp_file)
image = Image.open(temp_file)
with Image.open(temp_file) as image:
self.assertEqual(image.mode, self.rgb_mode)
self.assertEqual(image.size, (128, 128))
self.assertEqual(image.format, "WEBP")
@ -113,8 +110,7 @@ class TestFileWebp(PillowTestCase):
temp_file = self.tempfile("temp.webp")
hopper("P").save(temp_file)
image = Image.open(temp_file)
with Image.open(temp_file) as image:
self.assertEqual(image.mode, self.rgb_mode)
self.assertEqual(image.size, (128, 128))
self.assertEqual(image.format, "WEBP")
@ -145,8 +141,7 @@ class TestFileWebp(PillowTestCase):
def test_no_resource_warning(self):
file_path = "Tests/images/hopper.webp"
image = Image.open(file_path)
with Image.open(file_path) as image:
temp_file = self.tempfile("temp.webp")
self.assert_warning(None, image.save, temp_file)

View File

@ -26,8 +26,7 @@ class TestFileWebpAlpha(PillowTestCase):
# Generated with `cwebp transparent.png -o transparent.webp`
file_path = "Tests/images/transparent.webp"
image = Image.open(file_path)
with Image.open(file_path) as image:
self.assertEqual(image.mode, "RGBA")
self.assertEqual(image.size, (200, 150))
self.assertEqual(image.format, "WEBP")
@ -36,7 +35,7 @@ class TestFileWebpAlpha(PillowTestCase):
image.tobytes()
target = Image.open("Tests/images/transparent.png")
with Image.open("Tests/images/transparent.png") as target:
self.assert_image_similar(image, target, 20.0)
def test_write_lossless_rgb(self):
@ -56,7 +55,7 @@ class TestFileWebpAlpha(PillowTestCase):
pil_image.save(temp_file, lossless=True)
image = Image.open(temp_file)
with Image.open(temp_file) as image:
image.load()
self.assertEqual(image.mode, "RGBA")
@ -81,7 +80,7 @@ class TestFileWebpAlpha(PillowTestCase):
if _webp.WebPDecoderBuggyAlpha(self):
return
image = Image.open(temp_file)
with Image.open(temp_file) as image:
image.load()
self.assertEqual(image.mode, "RGBA")
@ -107,8 +106,7 @@ class TestFileWebpAlpha(PillowTestCase):
file_path = "Tests/images/transparent.gif"
with Image.open(file_path) as im:
im.save(temp_file)
image = Image.open(temp_file)
with Image.open(temp_file) as image:
self.assertEqual(image.mode, "RGBA")
self.assertEqual(image.size, (200, 150))
self.assertEqual(image.format, "WEBP")

View File

@ -48,7 +48,7 @@ class TestFileWebpAnimation(PillowTestCase):
temp_file = self.tempfile("temp.webp")
orig.save(temp_file, save_all=True)
im = Image.open(temp_file)
with Image.open(temp_file) as im:
self.assertEqual(im.n_frames, orig.n_frames)
# Compare first and last frames to the original animated GIF
@ -68,7 +68,7 @@ class TestFileWebpAnimation(PillowTestCase):
"""
def check(temp_file):
im = Image.open(temp_file)
with Image.open(temp_file) as im:
self.assertEqual(im.n_frames, 2)
# Compare first frame to original
@ -80,9 +80,8 @@ class TestFileWebpAnimation(PillowTestCase):
im.load()
self.assert_image_equal(im, frame2.convert("RGBA"))
frame1 = Image.open("Tests/images/anim_frame1.webp")
frame2 = Image.open("Tests/images/anim_frame2.webp")
with Image.open("Tests/images/anim_frame1.webp") as frame1:
with Image.open("Tests/images/anim_frame2.webp") as frame2:
temp_file1 = self.tempfile("temp.webp")
frame1.copy().save(
temp_file1, save_all=True, append_images=[frame2], lossless=True
@ -110,8 +109,8 @@ class TestFileWebpAnimation(PillowTestCase):
durations = [0, 10, 20, 30, 40]
temp_file = self.tempfile("temp.webp")
frame1 = Image.open("Tests/images/anim_frame1.webp")
frame2 = Image.open("Tests/images/anim_frame2.webp")
with Image.open("Tests/images/anim_frame1.webp") as frame1:
with Image.open("Tests/images/anim_frame2.webp") as frame2:
frame1.save(
temp_file,
save_all=True,
@ -119,7 +118,7 @@ class TestFileWebpAnimation(PillowTestCase):
duration=durations,
)
im = Image.open(temp_file)
with Image.open(temp_file) as im:
self.assertEqual(im.n_frames, 5)
self.assertTrue(im.is_animated)
@ -141,8 +140,8 @@ class TestFileWebpAnimation(PillowTestCase):
dur = 33
temp_file = self.tempfile("temp.webp")
frame1 = Image.open("Tests/images/anim_frame1.webp")
frame2 = Image.open("Tests/images/anim_frame2.webp")
with Image.open("Tests/images/anim_frame1.webp") as frame1:
with Image.open("Tests/images/anim_frame2.webp") as frame2:
frame1.save(
temp_file,
save_all=True,
@ -150,7 +149,7 @@ class TestFileWebpAnimation(PillowTestCase):
duration=dur,
)
im = Image.open(temp_file)
with Image.open(temp_file) as im:
self.assertEqual(im.n_frames, 5)
self.assertTrue(im.is_animated)

View File

@ -26,7 +26,7 @@ class TestFileWebpLossless(PillowTestCase):
hopper(self.rgb_mode).save(temp_file, lossless=True)
image = Image.open(temp_file)
with Image.open(temp_file) as image:
image.load()
self.assertEqual(image.mode, self.rgb_mode)

View File

@ -42,16 +42,14 @@ class TestFileWebpMetadata(PillowTestCase):
def test_write_exif_metadata(self):
file_path = "Tests/images/flower.jpg"
image = Image.open(file_path)
expected_exif = image.info["exif"]
test_buffer = BytesIO()
with Image.open(file_path) as image:
expected_exif = image.info["exif"]
image.save(test_buffer, "webp", exif=expected_exif)
test_buffer.seek(0)
webp_image = Image.open(test_buffer)
with Image.open(test_buffer) as webp_image:
webp_exif = webp_image.info.get("exif", None)
self.assertTrue(webp_exif)
if webp_exif:
@ -74,16 +72,14 @@ class TestFileWebpMetadata(PillowTestCase):
def test_write_icc_metadata(self):
file_path = "Tests/images/flower2.jpg"
image = Image.open(file_path)
expected_icc_profile = image.info["icc_profile"]
test_buffer = BytesIO()
with Image.open(file_path) as image:
expected_icc_profile = image.info["icc_profile"]
image.save(test_buffer, "webp", icc_profile=expected_icc_profile)
test_buffer.seek(0)
webp_image = Image.open(test_buffer)
with Image.open(test_buffer) as webp_image:
webp_icc_profile = webp_image.info.get("icc_profile", None)
self.assertTrue(webp_icc_profile)
@ -94,16 +90,14 @@ class TestFileWebpMetadata(PillowTestCase):
def test_read_no_exif(self):
file_path = "Tests/images/flower.jpg"
image = Image.open(file_path)
self.assertIn("exif", image.info)
test_buffer = BytesIO()
with Image.open(file_path) as image:
self.assertIn("exif", image.info)
image.save(test_buffer, "webp")
test_buffer.seek(0)
webp_image = Image.open(test_buffer)
with Image.open(test_buffer) as webp_image:
self.assertFalse(webp_image._getexif())
def test_write_animated_metadata(self):
@ -115,8 +109,8 @@ class TestFileWebpMetadata(PillowTestCase):
xmp_data = b"<xmp_data>"
temp_file = self.tempfile("temp.webp")
frame1 = Image.open("Tests/images/anim_frame1.webp")
frame2 = Image.open("Tests/images/anim_frame2.webp")
with Image.open("Tests/images/anim_frame1.webp") as frame1:
with Image.open("Tests/images/anim_frame2.webp") as frame2:
frame1.save(
temp_file,
save_all=True,

View File

@ -12,7 +12,7 @@ class TestFileWmf(PillowTestCase):
# Currently, support for WMF/EMF is Windows-only
im.load()
# Compare to reference rendering
imref = Image.open("Tests/images/drawing_emf_ref.png")
with Image.open("Tests/images/drawing_emf_ref.png") as imref:
imref.load()
self.assert_image_similar(im, imref, 0)
@ -22,7 +22,7 @@ class TestFileWmf(PillowTestCase):
# Currently, support for WMF/EMF is Windows-only
im.load()
# Compare to reference rendering
imref = Image.open("Tests/images/drawing_wmf_ref.png")
with Image.open("Tests/images/drawing_wmf_ref.png") as imref:
imref.load()
self.assert_image_similar(im, imref, 2.0)

View File

@ -30,8 +30,7 @@ static char basic_bits[] = {
class TestFileXbm(PillowTestCase):
def test_pil151(self):
im = Image.open(BytesIO(PIL151))
with Image.open(BytesIO(PIL151)) as im:
im.load()
self.assertEqual(im.mode, "1")
self.assertEqual(im.size, (32, 32))

View File

@ -7,7 +7,7 @@ TEST_FILE = "Tests/images/hopper.xpm"
class TestFileXpm(PillowTestCase):
def test_sanity(self):
im = Image.open(TEST_FILE)
with Image.open(TEST_FILE) as im:
im.load()
self.assertEqual(im.mode, "P")
self.assertEqual(im.size, (128, 128))

View File

@ -8,7 +8,7 @@ TEST_FILE = "Tests/images/hopper.p7"
class TestFileXVThumb(PillowTestCase):
def test_open(self):
# Act
im = Image.open(TEST_FILE)
with Image.open(TEST_FILE) as im:
# Assert
self.assertEqual(im.format, "XVThumb")

View File

@ -5,8 +5,7 @@ from .helper import PillowTestCase
class TestFormatLab(PillowTestCase):
def test_white(self):
i = Image.open("Tests/images/lab.tif")
with Image.open("Tests/images/lab.tif") as i:
i.load()
self.assertEqual(i.mode, "LAB")
@ -14,12 +13,13 @@ class TestFormatLab(PillowTestCase):
self.assertEqual(i.getbands(), ("L", "A", "B"))
k = i.getpixel((0, 0))
self.assertEqual(k, (255, 128, 128))
L = i.getdata(0)
a = i.getdata(1)
b = i.getdata(2)
self.assertEqual(k, (255, 128, 128))
self.assertEqual(list(L), [255] * 100)
self.assertEqual(list(a), [128] * 100)
self.assertEqual(list(b), [128] * 100)
@ -27,15 +27,13 @@ class TestFormatLab(PillowTestCase):
def test_green(self):
# l= 50 (/100), a = -100 (-128 .. 128) b=0 in PS
# == RGB: 0, 152, 117
i = Image.open("Tests/images/lab-green.tif")
with Image.open("Tests/images/lab-green.tif") as i:
k = i.getpixel((0, 0))
self.assertEqual(k, (128, 28, 128))
def test_red(self):
# l= 50 (/100), a = 100 (-128 .. 128) b=0 in PS
# == RGB: 255, 0, 124
i = Image.open("Tests/images/lab-red.tif")
with Image.open("Tests/images/lab-red.tif") as i:
k = i.getpixel((0, 0))
self.assertEqual(k, (128, 228, 128))

View File

@ -98,7 +98,7 @@ class TestImage(PillowTestCase):
self.assertEqual(im.mode, "P")
self.assertEqual(im.size, (10, 10))
im = Image.open(Path("Tests/images/hopper.jpg"))
with Image.open(Path("Tests/images/hopper.jpg")) as im:
self.assertEqual(im.mode, "RGB")
self.assertEqual(im.size, (128, 128))
@ -127,7 +127,7 @@ class TestImage(PillowTestCase):
with tempfile.TemporaryFile() as fp:
im.save(fp, "JPEG")
fp.seek(0)
reloaded = Image.open(fp)
with Image.open(fp) as reloaded:
self.assert_image_similar(im, reloaded, 20)
def test_unknown_extension(self):
@ -150,7 +150,7 @@ class TestImage(PillowTestCase):
temp_file = self.tempfile("temp.bmp")
shutil.copy("Tests/images/rgb32bf-rgba.bmp", temp_file)
im = Image.open(temp_file)
with Image.open(temp_file) as im:
self.assertTrue(im.readonly)
im.save(temp_file)
@ -365,7 +365,7 @@ class TestImage(PillowTestCase):
# Assert
self.assertEqual(im.size, (512, 512))
im2 = Image.open("Tests/images/effect_mandelbrot.png")
with Image.open("Tests/images/effect_mandelbrot.png") as im2:
self.assert_image_equal(im, im2)
def test_effect_mandelbrot_bad_arguments(self):
@ -407,7 +407,7 @@ class TestImage(PillowTestCase):
# Assert
self.assertEqual(im.size, (128, 128))
im3 = Image.open("Tests/images/effect_spread.png")
with Image.open("Tests/images/effect_spread.png") as im3:
self.assert_image_similar(im2, im3, 110)
def test_check_size(self):
@ -475,7 +475,8 @@ class TestImage(PillowTestCase):
self.assertEqual(im.mode, mode)
self.assertEqual(im.getpixel((0, 0)), 0)
self.assertEqual(im.getpixel((255, 255)), 255)
target = Image.open(target_file).convert(mode)
with Image.open(target_file) as target:
target = target.convert(mode)
self.assert_image_equal(im, target)
def test_radial_gradient_wrong_mode(self):
@ -499,7 +500,8 @@ class TestImage(PillowTestCase):
self.assertEqual(im.mode, mode)
self.assertEqual(im.getpixel((0, 0)), 255)
self.assertEqual(im.getpixel((128, 128)), 0)
target = Image.open(target_file).convert(mode)
with Image.open(target_file) as target:
target = target.convert(mode)
self.assert_image_equal(im, target)
def test_register_extensions(self):

View File

@ -52,3 +52,8 @@ class TestImageArray(PillowTestCase):
self.assertEqual(test("RGB"), ("RGB", (128, 100), True))
self.assertEqual(test("RGBA"), ("RGBA", (128, 100), True))
self.assertEqual(test("RGBX"), ("RGBA", (128, 100), True))
# Test mode is None with no "typestr" in the array interface
with self.assertRaises(TypeError):
wrapped = Wrapper(test("L"), {"shape": (100, 128)})
Image.fromarray(wrapped)

View File

@ -53,15 +53,15 @@ class TestImageConvert(PillowTestCase):
self.assertEqual(orig, converted)
def test_8bit(self):
im = Image.open("Tests/images/hopper.jpg")
with Image.open("Tests/images/hopper.jpg") as im:
self._test_float_conversion(im.convert("L"))
def test_16bit(self):
im = Image.open("Tests/images/16bit.cropped.tif")
with Image.open("Tests/images/16bit.cropped.tif") as im:
self._test_float_conversion(im)
def test_16bit_workaround(self):
im = Image.open("Tests/images/16bit.cropped.tif")
with Image.open("Tests/images/16bit.cropped.tif") as im:
self._test_float_conversion(im.convert("I"))
def test_rgba_p(self):
@ -210,7 +210,7 @@ class TestImageConvert(PillowTestCase):
# Assert
self.assertEqual(converted_im.mode, mode)
self.assertEqual(converted_im.size, im.size)
target = Image.open("Tests/images/hopper-XYZ.png")
with Image.open("Tests/images/hopper-XYZ.png") as target:
if converted_im.mode == "RGB":
self.assert_image_similar(converted_im, target, 3)
self.assertEqual(converted_im.info["transparency"], (105, 54, 4))

View File

@ -76,12 +76,12 @@ class TestImageCrop(PillowTestCase):
test_img = "Tests/images/bmp/g/pal8-0.bmp"
extents = (1, 1, 10, 10)
# works prepatch
img = Image.open(test_img)
with Image.open(test_img) as img:
img2 = img.crop(extents)
img2.load()
# fail prepatch
img = Image.open(test_img)
with Image.open(test_img) as img:
img = img.crop(extents)
img.load()

View File

@ -99,8 +99,8 @@ class TestImageFilter(PillowTestCase):
self.assertRaises(ValueError, lambda: ImageFilter.Kernel((3, 3), (0, 0)))
def test_consistency_3x3(self):
source = Image.open("Tests/images/hopper.bmp")
reference = Image.open("Tests/images/hopper_emboss.bmp")
with Image.open("Tests/images/hopper.bmp") as source:
with Image.open("Tests/images/hopper_emboss.bmp") as reference:
kernel = ImageFilter.Kernel( # noqa: E127
(3, 3),
# fmt: off
@ -120,8 +120,8 @@ class TestImageFilter(PillowTestCase):
)
def test_consistency_5x5(self):
source = Image.open("Tests/images/hopper.bmp")
reference = Image.open("Tests/images/hopper_emboss_more.bmp")
with Image.open("Tests/images/hopper.bmp") as source:
with Image.open("Tests/images/hopper_emboss_more.bmp") as reference:
kernel = ImageFilter.Kernel( # noqa: E127
(5, 5),
# fmt: off

View File

@ -19,7 +19,7 @@ class TestImageGetExtrema(PillowTestCase):
self.assertEqual(extrema("I;16"), (0, 255))
def test_true_16(self):
im = Image.open("Tests/images/16_bit_noise.tif")
with Image.open("Tests/images/16_bit_noise.tif") as im:
self.assertEqual(im.mode, "I;16")
extrema = im.getextrema()
self.assertEqual(extrema, (106, 285))

View File

@ -42,21 +42,24 @@ class TestImageQuantize(PillowTestCase):
self.assertEqual(image.quantize().convert().mode, "RGBA")
def test_quantize(self):
image = Image.open("Tests/images/caption_6_33_22.png").convert("RGB")
with Image.open("Tests/images/caption_6_33_22.png") as image:
image = image.convert("RGB")
converted = image.quantize()
self.assert_image(converted, "P", converted.size)
self.assert_image_similar(converted.convert("RGB"), image, 1)
def test_quantize_no_dither(self):
image = hopper()
palette = Image.open("Tests/images/caption_6_33_22.png").convert("P")
with Image.open("Tests/images/caption_6_33_22.png") as palette:
palette = palette.convert("P")
converted = image.quantize(dither=0, palette=palette)
self.assert_image(converted, "P", converted.size)
def test_quantize_dither_diff(self):
image = hopper()
palette = Image.open("Tests/images/caption_6_33_22.png").convert("P")
with Image.open("Tests/images/caption_6_33_22.png") as palette:
palette = palette.convert("P")
dither = image.quantize(dither=1, palette=palette)
nodither = image.quantize(dither=0, palette=palette)

View File

@ -450,7 +450,7 @@ class CoreResampleBoxTest(PillowTestCase):
return tiled
def test_tiles(self):
im = Image.open("Tests/images/flower.jpg")
with Image.open("Tests/images/flower.jpg") as im:
self.assertEqual(im.size, (480, 360))
dst_size = (251, 188)
reference = im.resize(dst_size, Image.BICUBIC)
@ -462,7 +462,7 @@ class CoreResampleBoxTest(PillowTestCase):
def test_subsample(self):
# This test shows advantages of the subpixel resizing
# after supersampling (e.g. during JPEG decoding).
im = Image.open("Tests/images/flower.jpg")
with Image.open("Tests/images/flower.jpg") as im:
self.assertEqual(im.size, (480, 360))
dst_size = (48, 36)
# Reference is cropped image resized to destination

View File

@ -24,7 +24,7 @@ class TestImageRotate(PillowTestCase):
def test_angle(self):
for angle in (0, 90, 180, 270):
im = Image.open("Tests/images/test-card.png")
with Image.open("Tests/images/test-card.png") as im:
self.rotate(im, im.mode, angle)
def test_zero(self):
@ -38,7 +38,7 @@ class TestImageRotate(PillowTestCase):
# >>> im = im.rotate(45, resample=Image.BICUBIC, expand=True)
# >>> im.save('Tests/images/hopper_45.png')
target = Image.open("Tests/images/hopper_45.png")
with Image.open("Tests/images/hopper_45.png") as target:
for (resample, epsilon) in (
(Image.NEAREST, 10),
(Image.BILINEAR, 5),
@ -50,27 +50,27 @@ class TestImageRotate(PillowTestCase):
def test_center_0(self):
im = hopper()
target = Image.open("Tests/images/hopper_45.png")
im = im.rotate(45, center=(0, 0), resample=Image.BICUBIC)
with Image.open("Tests/images/hopper_45.png") as target:
target_origin = target.size[1] / 2
target = target.crop((0, target_origin, 128, target_origin + 128))
im = im.rotate(45, center=(0, 0), resample=Image.BICUBIC)
self.assert_image_similar(im, target, 15)
def test_center_14(self):
im = hopper()
target = Image.open("Tests/images/hopper_45.png")
im = im.rotate(45, center=(14, 14), resample=Image.BICUBIC)
with Image.open("Tests/images/hopper_45.png") as target:
target_origin = target.size[1] / 2 - 14
target = target.crop((6, target_origin, 128 + 6, target_origin + 128))
im = im.rotate(45, center=(14, 14), resample=Image.BICUBIC)
self.assert_image_similar(im, target, 10)
def test_translate(self):
im = hopper()
target = Image.open("Tests/images/hopper_45.png")
with Image.open("Tests/images/hopper_45.png") as target:
target_origin = (target.size[1] / 2 - 64) - 5
target = target.crop(
(target_origin, target_origin, target_origin + 128, target_origin + 128)
@ -102,14 +102,14 @@ class TestImageRotate(PillowTestCase):
def test_rotate_no_fill(self):
im = Image.new("RGB", (100, 100), "green")
target = Image.open("Tests/images/rotate_45_no_fill.png")
im = im.rotate(45)
with Image.open("Tests/images/rotate_45_no_fill.png") as target:
self.assert_image_equal(im, target)
def test_rotate_with_fill(self):
im = Image.new("RGB", (100, 100), "green")
target = Image.open("Tests/images/rotate_45_with_fill.png")
im = im.rotate(45, fillcolor="white")
with Image.open("Tests/images/rotate_45_with_fill.png") as target:
self.assert_image_equal(im, target)
def test_alpha_rotate_no_fill(self):

View File

@ -53,7 +53,7 @@ class TestImageSplit(PillowTestCase):
def split_open(mode):
hopper(mode).save(test_file)
im = Image.open(test_file)
with Image.open(test_file) as im:
return len(im.split())
self.assertEqual(split_open("1"), 1)

View File

@ -5,37 +5,43 @@ from .helper import PillowTestCase, fromstring, hopper, tostring
class TestImageThumbnail(PillowTestCase):
def test_sanity(self):
im = hopper()
im.thumbnail((100, 100))
self.assertIsNone(im.thumbnail((100, 100)))
self.assert_image(im, im.mode, (100, 100))
self.assertEqual(im.size, (100, 100))
def test_aspect(self):
im = hopper()
im = Image.new("L", (128, 128))
im.thumbnail((100, 100))
self.assert_image(im, im.mode, (100, 100))
self.assertEqual(im.size, (100, 100))
im = hopper().resize((128, 256))
im = Image.new("L", (128, 256))
im.thumbnail((100, 100))
self.assert_image(im, im.mode, (50, 100))
self.assertEqual(im.size, (50, 100))
im = hopper().resize((128, 256))
im = Image.new("L", (128, 256))
im.thumbnail((50, 100))
self.assert_image(im, im.mode, (50, 100))
self.assertEqual(im.size, (50, 100))
im = hopper().resize((256, 128))
im = Image.new("L", (256, 128))
im.thumbnail((100, 100))
self.assert_image(im, im.mode, (100, 50))
self.assertEqual(im.size, (100, 50))
im = hopper().resize((256, 128))
im = Image.new("L", (256, 128))
im.thumbnail((100, 50))
self.assert_image(im, im.mode, (100, 50))
self.assertEqual(im.size, (100, 50))
im = hopper().resize((128, 128))
im = Image.new("L", (128, 128))
im.thumbnail((100, 100))
self.assert_image(im, im.mode, (100, 100))
self.assertEqual(im.size, (100, 100))
im = Image.new("L", (256, 162)) # ratio is 1.5802469136
im.thumbnail((33, 33))
self.assertEqual(im.size, (33, 21)) # ratio is 1.5714285714
im = Image.new("L", (162, 256)) # ratio is 0.6328125
im.thumbnail((33, 33))
self.assertEqual(im.size, (21, 33)) # ratio is 0.6363636364
def test_no_resize(self):
# Check that draft() can resize the image to the destination size
@ -46,7 +52,7 @@ class TestImageThumbnail(PillowTestCase):
# Test thumbnail(), where only draft() is necessary to resize the image
with Image.open("Tests/images/hopper.jpg") as im:
im.thumbnail((64, 64))
self.assert_image(im, im.mode, (64, 64))
self.assertEqual(im.size, (64, 64))
def test_DCT_scaling_edges(self):
# Make an image with red borders and size (N * 8) + 1 to cross DCT grid

View File

@ -23,7 +23,7 @@ class TestImageTransform(PillowTestCase):
def test_info(self):
comment = b"File written by Adobe Photoshop\xa8 4.0"
im = Image.open("Tests/images/hopper.gif")
with Image.open("Tests/images/hopper.gif") as im:
self.assertEqual(im.info["comment"], comment)
transform = ImageTransform.ExtentTransform((0, 0, 0, 0))

View File

@ -45,8 +45,8 @@ class TestImageChops(PillowTestCase):
def test_add(self):
# Arrange
im1 = Image.open("Tests/images/imagedraw_ellipse_RGB.png")
im2 = Image.open("Tests/images/imagedraw_floodfill_RGB.png")
with Image.open("Tests/images/imagedraw_ellipse_RGB.png") as im1:
with Image.open("Tests/images/imagedraw_floodfill_RGB.png") as im2:
# Act
new = ImageChops.add(im1, im2)
@ -57,8 +57,8 @@ class TestImageChops(PillowTestCase):
def test_add_scale_offset(self):
# Arrange
im1 = Image.open("Tests/images/imagedraw_ellipse_RGB.png")
im2 = Image.open("Tests/images/imagedraw_floodfill_RGB.png")
with Image.open("Tests/images/imagedraw_ellipse_RGB.png") as im1:
with Image.open("Tests/images/imagedraw_floodfill_RGB.png") as im2:
# Act
new = ImageChops.add(im1, im2, scale=2.5, offset=100)
@ -79,8 +79,8 @@ class TestImageChops(PillowTestCase):
def test_add_modulo(self):
# Arrange
im1 = Image.open("Tests/images/imagedraw_ellipse_RGB.png")
im2 = Image.open("Tests/images/imagedraw_floodfill_RGB.png")
with Image.open("Tests/images/imagedraw_ellipse_RGB.png") as im1:
with Image.open("Tests/images/imagedraw_floodfill_RGB.png") as im2:
# Act
new = ImageChops.add_modulo(im1, im2)
@ -101,8 +101,8 @@ class TestImageChops(PillowTestCase):
def test_blend(self):
# Arrange
im1 = Image.open("Tests/images/imagedraw_ellipse_RGB.png")
im2 = Image.open("Tests/images/imagedraw_floodfill_RGB.png")
with Image.open("Tests/images/imagedraw_ellipse_RGB.png") as im1:
with Image.open("Tests/images/imagedraw_floodfill_RGB.png") as im2:
# Act
new = ImageChops.blend(im1, im2, 0.5)
@ -125,8 +125,8 @@ class TestImageChops(PillowTestCase):
def test_darker_image(self):
# Arrange
im1 = Image.open("Tests/images/imagedraw_chord_RGB.png")
im2 = Image.open("Tests/images/imagedraw_outline_chord_RGB.png")
with Image.open("Tests/images/imagedraw_chord_RGB.png") as im1:
with Image.open("Tests/images/imagedraw_outline_chord_RGB.png") as im2:
# Act
new = ImageChops.darker(im1, im2)
@ -137,7 +137,7 @@ class TestImageChops(PillowTestCase):
def test_darker_pixel(self):
# Arrange
im1 = hopper()
im2 = Image.open("Tests/images/imagedraw_chord_RGB.png")
with Image.open("Tests/images/imagedraw_chord_RGB.png") as im2:
# Act
new = ImageChops.darker(im1, im2)
@ -147,8 +147,8 @@ class TestImageChops(PillowTestCase):
def test_difference(self):
# Arrange
im1 = Image.open("Tests/images/imagedraw_arc_end_le_start.png")
im2 = Image.open("Tests/images/imagedraw_arc_no_loops.png")
with Image.open("Tests/images/imagedraw_arc_end_le_start.png") as im1:
with Image.open("Tests/images/imagedraw_arc_no_loops.png") as im2:
# Act
new = ImageChops.difference(im1, im2)
@ -159,7 +159,7 @@ class TestImageChops(PillowTestCase):
def test_difference_pixel(self):
# Arrange
im1 = hopper()
im2 = Image.open("Tests/images/imagedraw_polygon_kite_RGB.png")
with Image.open("Tests/images/imagedraw_polygon_kite_RGB.png") as im2:
# Act
new = ImageChops.difference(im1, im2)
@ -179,7 +179,7 @@ class TestImageChops(PillowTestCase):
def test_invert(self):
# Arrange
im = Image.open("Tests/images/imagedraw_floodfill_RGB.png")
with Image.open("Tests/images/imagedraw_floodfill_RGB.png") as im:
# Act
new = ImageChops.invert(im)
@ -191,8 +191,8 @@ class TestImageChops(PillowTestCase):
def test_lighter_image(self):
# Arrange
im1 = Image.open("Tests/images/imagedraw_chord_RGB.png")
im2 = Image.open("Tests/images/imagedraw_outline_chord_RGB.png")
with Image.open("Tests/images/imagedraw_chord_RGB.png") as im1:
with Image.open("Tests/images/imagedraw_outline_chord_RGB.png") as im2:
# Act
new = ImageChops.lighter(im1, im2)
@ -203,7 +203,7 @@ class TestImageChops(PillowTestCase):
def test_lighter_pixel(self):
# Arrange
im1 = hopper()
im2 = Image.open("Tests/images/imagedraw_chord_RGB.png")
with Image.open("Tests/images/imagedraw_chord_RGB.png") as im2:
# Act
new = ImageChops.lighter(im1, im2)
@ -226,7 +226,7 @@ class TestImageChops(PillowTestCase):
def test_multiply_green(self):
# Arrange
im = Image.open("Tests/images/imagedraw_floodfill_RGB.png")
with Image.open("Tests/images/imagedraw_floodfill_RGB.png") as im:
green = Image.new("RGB", im.size, "green")
# Act
@ -252,9 +252,9 @@ class TestImageChops(PillowTestCase):
def test_offset(self):
# Arrange
im = Image.open("Tests/images/imagedraw_ellipse_RGB.png")
xoffset = 45
yoffset = 20
with Image.open("Tests/images/imagedraw_ellipse_RGB.png") as im:
# Act
new = ImageChops.offset(im, xoffset, yoffset)
@ -271,8 +271,8 @@ class TestImageChops(PillowTestCase):
def test_screen(self):
# Arrange
im1 = Image.open("Tests/images/imagedraw_ellipse_RGB.png")
im2 = Image.open("Tests/images/imagedraw_floodfill_RGB.png")
with Image.open("Tests/images/imagedraw_ellipse_RGB.png") as im1:
with Image.open("Tests/images/imagedraw_floodfill_RGB.png") as im2:
# Act
new = ImageChops.screen(im1, im2)
@ -283,8 +283,8 @@ class TestImageChops(PillowTestCase):
def test_subtract(self):
# Arrange
im1 = Image.open("Tests/images/imagedraw_chord_RGB.png")
im2 = Image.open("Tests/images/imagedraw_outline_chord_RGB.png")
with Image.open("Tests/images/imagedraw_chord_RGB.png") as im1:
with Image.open("Tests/images/imagedraw_outline_chord_RGB.png") as im2:
# Act
new = ImageChops.subtract(im1, im2)
@ -296,8 +296,8 @@ class TestImageChops(PillowTestCase):
def test_subtract_scale_offset(self):
# Arrange
im1 = Image.open("Tests/images/imagedraw_chord_RGB.png")
im2 = Image.open("Tests/images/imagedraw_outline_chord_RGB.png")
with Image.open("Tests/images/imagedraw_chord_RGB.png") as im1:
with Image.open("Tests/images/imagedraw_outline_chord_RGB.png") as im2:
# Act
new = ImageChops.subtract(im1, im2, scale=2.5, offset=100)
@ -309,7 +309,7 @@ class TestImageChops(PillowTestCase):
def test_subtract_clip(self):
# Arrange
im1 = hopper()
im2 = Image.open("Tests/images/imagedraw_chord_RGB.png")
with Image.open("Tests/images/imagedraw_chord_RGB.png") as im2:
# Act
new = ImageChops.subtract(im1, im2)
@ -319,8 +319,8 @@ class TestImageChops(PillowTestCase):
def test_subtract_modulo(self):
# Arrange
im1 = Image.open("Tests/images/imagedraw_chord_RGB.png")
im2 = Image.open("Tests/images/imagedraw_outline_chord_RGB.png")
with Image.open("Tests/images/imagedraw_chord_RGB.png") as im1:
with Image.open("Tests/images/imagedraw_outline_chord_RGB.png") as im2:
# Act
new = ImageChops.subtract_modulo(im1, im2)
@ -333,7 +333,7 @@ class TestImageChops(PillowTestCase):
def test_subtract_modulo_no_clip(self):
# Arrange
im1 = hopper()
im2 = Image.open("Tests/images/imagedraw_chord_RGB.png")
with Image.open("Tests/images/imagedraw_chord_RGB.png") as im2:
# Act
new = ImageChops.subtract_modulo(im1, im2)

View File

@ -229,8 +229,7 @@ class TestImageCms(PillowTestCase):
# i.save('temp.lab.tif') # visually verified vs PS.
target = Image.open("Tests/images/hopper.Lab.tif")
with Image.open("Tests/images/hopper.Lab.tif") as target:
self.assert_image_similar(i, target, 3.5)
def test_lab_srgb(self):
@ -238,8 +237,7 @@ class TestImageCms(PillowTestCase):
pLab = ImageCms.createProfile("LAB")
t = ImageCms.buildTransform(pLab, psRGB, "LAB", "RGB")
img = Image.open("Tests/images/hopper.Lab.tif")
with Image.open("Tests/images/hopper.Lab.tif") as img:
img_srgb = ImageCms.applyTransform(img, t)
# img_srgb.save('temp.srgb.tif') # visually verified vs ps.

View File

@ -157,9 +157,9 @@ class TestImageDraw(PillowTestCase):
def test_bitmap(self):
# Arrange
small = Image.open("Tests/images/pil123rgba.png").resize((50, 50))
im = Image.new("RGB", (W, H))
draw = ImageDraw.Draw(im)
with Image.open("Tests/images/pil123rgba.png").resize((50, 50)) as small:
# Act
draw.bitmap((10, 10), small)
@ -523,7 +523,7 @@ class TestImageDraw(PillowTestCase):
# Assert
expected = "Tests/images/imagedraw_floodfill_" + mode + ".png"
im_floodfill = Image.open(expected)
with Image.open(expected) as im_floodfill:
self.assert_image_equal(im, im_floodfill)
# Test that using the same colour does not change the image
@ -603,7 +603,7 @@ class TestImageDraw(PillowTestCase):
return img, ImageDraw.Draw(img)
def test_square(self):
expected = Image.open(os.path.join(IMAGES_PATH, "square.png"))
with Image.open(os.path.join(IMAGES_PATH, "square.png")) as expected:
expected.load()
img, draw = self.create_base_image_draw((10, 10))
draw.polygon([(2, 2), (2, 7), (7, 7), (7, 2)], BLACK)
@ -616,35 +616,39 @@ class TestImageDraw(PillowTestCase):
self.assert_image_equal(img, expected, "square as normal rectangle failed")
img, draw = self.create_base_image_draw((10, 10))
draw.rectangle((7, 7, 2, 2), BLACK)
self.assert_image_equal(img, expected, "square as inverted rectangle failed")
self.assert_image_equal(
img, expected, "square as inverted rectangle failed"
)
def test_triangle_right(self):
expected = Image.open(os.path.join(IMAGES_PATH, "triangle_right.png"))
with Image.open(os.path.join(IMAGES_PATH, "triangle_right.png")) as expected:
expected.load()
img, draw = self.create_base_image_draw((20, 20))
draw.polygon([(3, 5), (17, 5), (10, 12)], BLACK)
self.assert_image_equal(img, expected, "triangle right failed")
def test_line_horizontal(self):
expected = Image.open(
with Image.open(
os.path.join(IMAGES_PATH, "line_horizontal_w2px_normal.png")
)
) as expected:
expected.load()
img, draw = self.create_base_image_draw((20, 20))
draw.line((5, 5, 14, 5), BLACK, 2)
self.assert_image_equal(
img, expected, "line straight horizontal normal 2px wide failed"
)
expected = Image.open(
with Image.open(
os.path.join(IMAGES_PATH, "line_horizontal_w2px_inverted.png")
)
) as expected:
expected.load()
img, draw = self.create_base_image_draw((20, 20))
draw.line((14, 5, 5, 5), BLACK, 2)
self.assert_image_equal(
img, expected, "line straight horizontal inverted 2px wide failed"
)
expected = Image.open(os.path.join(IMAGES_PATH, "line_horizontal_w3px.png"))
with Image.open(
os.path.join(IMAGES_PATH, "line_horizontal_w3px.png")
) as expected:
expected.load()
img, draw = self.create_base_image_draw((20, 20))
draw.line((5, 5, 14, 5), BLACK, 3)
@ -656,7 +660,9 @@ class TestImageDraw(PillowTestCase):
self.assert_image_equal(
img, expected, "line straight horizontal inverted 3px wide failed"
)
expected = Image.open(os.path.join(IMAGES_PATH, "line_horizontal_w101px.png"))
with Image.open(
os.path.join(IMAGES_PATH, "line_horizontal_w101px.png")
) as expected:
expected.load()
img, draw = self.create_base_image_draw((200, 110))
draw.line((5, 55, 195, 55), BLACK, 101)
@ -666,9 +672,9 @@ class TestImageDraw(PillowTestCase):
def test_line_h_s1_w2(self):
self.skipTest("failing")
expected = Image.open(
with Image.open(
os.path.join(IMAGES_PATH, "line_horizontal_slope1px_w2px.png")
)
) as expected:
expected.load()
img, draw = self.create_base_image_draw((20, 20))
draw.line((5, 5, 14, 6), BLACK, 2)
@ -677,25 +683,27 @@ class TestImageDraw(PillowTestCase):
)
def test_line_vertical(self):
expected = Image.open(
with Image.open(
os.path.join(IMAGES_PATH, "line_vertical_w2px_normal.png")
)
) as expected:
expected.load()
img, draw = self.create_base_image_draw((20, 20))
draw.line((5, 5, 5, 14), BLACK, 2)
self.assert_image_equal(
img, expected, "line straight vertical normal 2px wide failed"
)
expected = Image.open(
with Image.open(
os.path.join(IMAGES_PATH, "line_vertical_w2px_inverted.png")
)
) as expected:
expected.load()
img, draw = self.create_base_image_draw((20, 20))
draw.line((5, 14, 5, 5), BLACK, 2)
self.assert_image_equal(
img, expected, "line straight vertical inverted 2px wide failed"
)
expected = Image.open(os.path.join(IMAGES_PATH, "line_vertical_w3px.png"))
with Image.open(
os.path.join(IMAGES_PATH, "line_vertical_w3px.png")
) as expected:
expected.load()
img, draw = self.create_base_image_draw((20, 20))
draw.line((5, 5, 5, 14), BLACK, 3)
@ -707,16 +715,18 @@ class TestImageDraw(PillowTestCase):
self.assert_image_equal(
img, expected, "line straight vertical inverted 3px wide failed"
)
expected = Image.open(os.path.join(IMAGES_PATH, "line_vertical_w101px.png"))
with Image.open(
os.path.join(IMAGES_PATH, "line_vertical_w101px.png")
) as expected:
expected.load()
img, draw = self.create_base_image_draw((110, 200))
draw.line((55, 5, 55, 195), BLACK, 101)
self.assert_image_equal(
img, expected, "line straight vertical 101px wide failed"
)
expected = Image.open(
with Image.open(
os.path.join(IMAGES_PATH, "line_vertical_slope1px_w2px.png")
)
) as expected:
expected.load()
img, draw = self.create_base_image_draw((20, 20))
draw.line((5, 5, 6, 14), BLACK, 2)
@ -725,7 +735,9 @@ class TestImageDraw(PillowTestCase):
)
def test_line_oblique_45(self):
expected = Image.open(os.path.join(IMAGES_PATH, "line_oblique_45_w3px_a.png"))
with Image.open(
os.path.join(IMAGES_PATH, "line_oblique_45_w3px_a.png")
) as expected:
expected.load()
img, draw = self.create_base_image_draw((20, 20))
draw.line((5, 5, 14, 14), BLACK, 3)
@ -737,7 +749,9 @@ class TestImageDraw(PillowTestCase):
self.assert_image_equal(
img, expected, "line oblique 45 inverted 3px wide A failed"
)
expected = Image.open(os.path.join(IMAGES_PATH, "line_oblique_45_w3px_b.png"))
with Image.open(
os.path.join(IMAGES_PATH, "line_oblique_45_w3px_b.png")
) as expected:
expected.load()
img, draw = self.create_base_image_draw((20, 20))
draw.line((14, 5, 5, 14), BLACK, 3)

View File

@ -128,8 +128,7 @@ class TestImageFile(PillowTestCase):
if "zip_encoder" not in codecs:
self.skipTest("PNG (zlib) encoder not available")
im = Image.open("Tests/images/truncated_image.png")
with Image.open("Tests/images/truncated_image.png") as im:
ImageFile.LOAD_TRUNCATED_IMAGES = True
try:
im.load()
@ -140,7 +139,7 @@ class TestImageFile(PillowTestCase):
if "zip_encoder" not in codecs:
self.skipTest("PNG (zlib) encoder not available")
im = Image.open("Tests/images/broken_data_stream.png")
with Image.open("Tests/images/broken_data_stream.png") as im:
with self.assertRaises(IOError):
im.load()
@ -148,8 +147,7 @@ class TestImageFile(PillowTestCase):
if "zip_encoder" not in codecs:
self.skipTest("PNG (zlib) encoder not available")
im = Image.open("Tests/images/broken_data_stream.png")
with Image.open("Tests/images/broken_data_stream.png") as im:
ImageFile.LOAD_TRUNCATED_IMAGES = True
try:
im.load()
@ -246,7 +244,7 @@ class TestPyDecoder(PillowTestCase):
self.assertIsNone(im.get_format_mimetype())
def test_exif_jpeg(self):
im = Image.open("Tests/images/exif-72dpi-int.jpg") # Little endian
with Image.open("Tests/images/exif-72dpi-int.jpg") as im: # Little endian
exif = im.getexif()
self.assertNotIn(258, exif)
self.assertIn(40960, exif)
@ -266,7 +264,7 @@ class TestPyDecoder(PillowTestCase):
self.assertEqual(reloaded_exif[40963], 455)
self.assertEqual(exif[11], "Pillow test")
im = Image.open("Tests/images/no-dpi-in-exif.jpg") # Big endian
with Image.open("Tests/images/no-dpi-in-exif.jpg") as im: # Big endian
exif = im.getexif()
self.assertNotIn(258, exif)
self.assertIn(40962, exif)
@ -291,7 +289,7 @@ class TestPyDecoder(PillowTestCase):
"WebP support not installed with animation",
)
def test_exif_webp(self):
im = Image.open("Tests/images/hopper.webp")
with Image.open("Tests/images/hopper.webp") as im:
exif = im.getexif()
self.assertEqual(exif, {})
@ -313,7 +311,7 @@ class TestPyDecoder(PillowTestCase):
check_exif()
def test_exif_png(self):
im = Image.open("Tests/images/exif.png")
with Image.open("Tests/images/exif.png") as im:
exif = im.getexif()
self.assertEqual(exif, {274: 1})

View File

@ -179,7 +179,7 @@ class TestImageFont(PillowTestCase):
draw.rectangle((10, 10, 10 + size[0], 10 + size[1]))
target = "Tests/images/rectangle_surrounding_text.png"
target_img = Image.open(target)
with Image.open(target) as target_img:
# Epsilon ~.5 fails with FreeType 2.7
self.assert_image_similar(im, target_img, self.metrics["textsize"])
@ -196,7 +196,7 @@ class TestImageFont(PillowTestCase):
y += line_spacing
target = "Tests/images/multiline_text.png"
target_img = Image.open(target)
with Image.open(target) as target_img:
# some versions of freetype have different horizontal spacing.
# setting a tight epsilon, I'm showing the original test failure
@ -213,7 +213,7 @@ class TestImageFont(PillowTestCase):
draw.text((0, 0), TEST_TEXT, font=ttf)
target = "Tests/images/multiline_text.png"
target_img = Image.open(target)
with Image.open(target) as target_img:
# Epsilon ~.5 fails with FreeType 2.7
self.assert_image_similar(im, target_img, self.metrics["multiline"])
@ -232,7 +232,7 @@ class TestImageFont(PillowTestCase):
draw.multiline_text((0, 0), TEST_TEXT, font=ttf, align=align)
target = "Tests/images/multiline_text" + ext + ".png"
target_img = Image.open(target)
with Image.open(target) as target_img:
# Epsilon ~.5 fails with FreeType 2.7
self.assert_image_similar(im, target_img, self.metrics["multiline"])
@ -297,7 +297,7 @@ class TestImageFont(PillowTestCase):
draw.multiline_text((0, 0), TEST_TEXT, font=ttf, spacing=10)
target = "Tests/images/multiline_text_spacing.png"
target_img = Image.open(target)
with Image.open(target) as target_img:
# Epsilon ~.5 fails with FreeType 2.7
self.assert_image_similar(im, target_img, self.metrics["multiline"])
@ -432,7 +432,7 @@ class TestImageFont(PillowTestCase):
draw = ImageDraw.Draw(im)
target = "Tests/images/default_font.png"
target_img = Image.open(target)
with Image.open(target) as target_img:
# Act
default_font = ImageFont.load_default()
@ -464,7 +464,7 @@ class TestImageFont(PillowTestCase):
with self.assertRaises(UnicodeEncodeError):
font.getsize("")
@unittest.skipIf(is_pypy(), "requires CPython")
@unittest.skipIf(is_pypy(), "failing on PyPy")
def test_unicode_extended(self):
# issue #3777
text = "A\u278A\U0001F12B"
@ -703,7 +703,7 @@ class TestImageFont(PillowTestCase):
d = ImageDraw.Draw(im)
d.text((10, 10), "Text", font=font, fill="black")
expected = Image.open(path)
with Image.open(path) as expected:
self.assert_image_similar(im, expected, epsilon)
font = ImageFont.truetype("Tests/fonts/AdobeVFPrototype.ttf", 36)
@ -733,7 +733,7 @@ class TestImageFont(PillowTestCase):
d = ImageDraw.Draw(im)
d.text((10, 10), "Text", font=font, fill="black")
expected = Image.open(path)
with Image.open(path) as expected:
self.assert_image_similar(im, expected, epsilon)
font = ImageFont.truetype("Tests/fonts/AdobeVFPrototype.ttf", 36)

View File

@ -25,8 +25,7 @@ class TestImagecomplextext(PillowTestCase):
draw.text((0, 0), "اهلا عمان", font=ttf, fill=500)
target = "Tests/images/test_text.png"
target_img = Image.open(target)
with Image.open(target) as target_img:
self.assert_image_similar(im, target_img, 0.5)
def test_y_offset(self):
@ -37,8 +36,7 @@ class TestImagecomplextext(PillowTestCase):
draw.text((0, 0), "العالم العربي", font=ttf, fill=500)
target = "Tests/images/test_y_offset.png"
target_img = Image.open(target)
with Image.open(target) as target_img:
self.assert_image_similar(im, target_img, 1.7)
def test_complex_unicode_text(self):
@ -49,8 +47,7 @@ class TestImagecomplextext(PillowTestCase):
draw.text((0, 0), "السلام عليكم", font=ttf, fill=500)
target = "Tests/images/test_complex_unicode_text.png"
target_img = Image.open(target)
with Image.open(target) as target_img:
self.assert_image_similar(im, target_img, 0.5)
ttf = ImageFont.truetype("Tests/fonts/KhmerOSBattambang-Regular.ttf", FONT_SIZE)
@ -60,8 +57,7 @@ class TestImagecomplextext(PillowTestCase):
draw.text((0, 0), "លោកុប្បត្តិ", font=ttf, fill=500)
target = "Tests/images/test_complex_unicode_text2.png"
target_img = Image.open(target)
with Image.open(target) as target_img:
self.assert_image_similar(im, target_img, 2.3)
def test_text_direction_rtl(self):
@ -72,8 +68,7 @@ class TestImagecomplextext(PillowTestCase):
draw.text((0, 0), "English عربي", font=ttf, fill=500, direction="rtl")
target = "Tests/images/test_direction_rtl.png"
target_img = Image.open(target)
with Image.open(target) as target_img:
self.assert_image_similar(im, target_img, 0.5)
def test_text_direction_ltr(self):
@ -84,8 +79,7 @@ class TestImagecomplextext(PillowTestCase):
draw.text((0, 0), "سلطنة عمان Oman", font=ttf, fill=500, direction="ltr")
target = "Tests/images/test_direction_ltr.png"
target_img = Image.open(target)
with Image.open(target) as target_img:
self.assert_image_similar(im, target_img, 0.5)
def test_text_direction_rtl2(self):
@ -96,8 +90,7 @@ class TestImagecomplextext(PillowTestCase):
draw.text((0, 0), "Oman سلطنة عمان", font=ttf, fill=500, direction="rtl")
target = "Tests/images/test_direction_ltr.png"
target_img = Image.open(target)
with Image.open(target) as target_img:
self.assert_image_similar(im, target_img, 0.5)
def test_text_direction_ttb(self):
@ -112,8 +105,7 @@ class TestImagecomplextext(PillowTestCase):
self.skipTest("libraqm 0.7 or greater not available")
target = "Tests/images/test_direction_ttb.png"
target_img = Image.open(target)
with Image.open(target) as target_img:
self.assert_image_similar(im, target_img, 1.15)
def test_text_direction_ttb_stroke(self):
@ -136,8 +128,7 @@ class TestImagecomplextext(PillowTestCase):
self.skipTest("libraqm 0.7 or greater not available")
target = "Tests/images/test_direction_ttb_stroke.png"
target_img = Image.open(target)
with Image.open(target) as target_img:
self.assert_image_similar(im, target_img, 12.4)
def test_ligature_features(self):
@ -147,8 +138,7 @@ class TestImagecomplextext(PillowTestCase):
draw = ImageDraw.Draw(im)
draw.text((0, 0), "filling", font=ttf, fill=500, features=["-liga"])
target = "Tests/images/test_ligature_features.png"
target_img = Image.open(target)
with Image.open(target) as target_img:
self.assert_image_similar(im, target_img, 0.5)
liga_size = ttf.getsize("fi", features=["-liga"])
@ -162,8 +152,7 @@ class TestImagecomplextext(PillowTestCase):
draw.text((0, 0), "TeToAV", font=ttf, fill=500, features=["-kern"])
target = "Tests/images/test_kerning_features.png"
target_img = Image.open(target)
with Image.open(target) as target_img:
self.assert_image_similar(im, target_img, 0.5)
def test_arabictext_features(self):
@ -180,8 +169,7 @@ class TestImagecomplextext(PillowTestCase):
)
target = "Tests/images/test_arabictext_features.png"
target_img = Image.open(target)
with Image.open(target) as target_img:
self.assert_image_similar(im, target_img, 0.5)
def test_x_max_and_y_offset(self):
@ -192,8 +180,7 @@ class TestImagecomplextext(PillowTestCase):
draw.text((0, 0), "لح", font=ttf, fill=500)
target = "Tests/images/test_x_max_and_y_offset.png"
target_img = Image.open(target)
with Image.open(target) as target_img:
self.assert_image_similar(im, target_img, 0.5)
def test_language(self):
@ -204,6 +191,5 @@ class TestImagecomplextext(PillowTestCase):
draw.text((0, 0), "абвг", font=ttf, fill=500, language="sr")
target = "Tests/images/test_language.png"
target_img = Image.open(target)
with Image.open(target) as target_img:
self.assert_image_similar(im, target_img, 0.5)

View File

@ -51,7 +51,7 @@ class MorphTests(PillowTestCase):
self.assertEqual(self.img_to_string(A), self.img_string_normalize(Bstring))
def test_str_to_img(self):
im = Image.open("Tests/images/morph_a.png")
with Image.open("Tests/images/morph_a.png") as im:
self.assert_image_equal(self.A, im)
def create_lut(self):

View File

@ -106,9 +106,9 @@ class TestImageOps(PillowTestCase):
new_im = ImageOps.pad(im, new_size, color=color, centering=centering)
self.assertEqual(new_im.size, new_size)
target = Image.open(
with Image.open(
"Tests/images/imageops_pad_" + label + "_" + str(i) + ".jpg"
)
) as target:
self.assert_image_similar(new_im, target, 6)
def test_pil163(self):
@ -140,7 +140,7 @@ class TestImageOps(PillowTestCase):
# Test the colorizing function with 2-color functionality
# Open test image (256px by 10px, black to white)
im = Image.open("Tests/images/bw_gradient.png")
with Image.open("Tests/images/bw_gradient.png") as im:
im = im.convert("L")
# Create image with original 2-color functionality
@ -173,7 +173,7 @@ class TestImageOps(PillowTestCase):
# Test the colorizing function with 2-color functionality and offset
# Open test image (256px by 10px, black to white)
im = Image.open("Tests/images/bw_gradient.png")
with Image.open("Tests/images/bw_gradient.png") as im:
im = im.convert("L")
# Create image with original 2-color functionality with offsets
@ -208,7 +208,7 @@ class TestImageOps(PillowTestCase):
# Test the colorizing function with 3-color functionality and offset
# Open test image (256px by 10px, black to white)
im = Image.open("Tests/images/bw_gradient.png")
with Image.open("Tests/images/bw_gradient.png") as im:
im = im.convert("L")
# Create image with new three color functionality with offsets
@ -261,27 +261,36 @@ class TestImageOps(PillowTestCase):
if HAVE_WEBP and _webp.HAVE_WEBPANIM:
exts.append(".webp")
for ext in exts:
base_im = Image.open("Tests/images/hopper" + ext)
with Image.open("Tests/images/hopper" + ext) as base_im:
orientations = [base_im]
for i in range(2, 9):
im = Image.open("Tests/images/hopper_orientation_" + str(i) + ext)
orientations.append(im)
for i, orientation_im in enumerate(orientations):
for im in [orientation_im, orientation_im.copy()]: # ImageFile # Image
if i == 0:
def check(orientation_im):
for im in [
orientation_im,
orientation_im.copy(),
]: # ImageFile # Image
if orientation_im is base_im:
self.assertNotIn("exif", im.info)
else:
original_exif = im.info["exif"]
transposed_im = ImageOps.exif_transpose(im)
self.assert_image_similar(base_im, transposed_im, 17)
if i == 0:
if orientation_im is base_im:
self.assertNotIn("exif", im.info)
else:
self.assertNotEqual(transposed_im.info["exif"], original_exif)
self.assertNotEqual(
transposed_im.info["exif"], original_exif
)
self.assertNotIn(0x0112, transposed_im.getexif())
# Repeat the operation, to test that it does not keep transposing
# Repeat the operation
# to test that it does not keep transposing
transposed_im2 = ImageOps.exif_transpose(transposed_im)
self.assert_image_equal(transposed_im2, transposed_im)
check(base_im)
for i in range(2, 9):
with Image.open(
"Tests/images/hopper_orientation_" + str(i) + ext
) as orientation_im:
check(orientation_im)

View File

@ -128,8 +128,7 @@ class TestImagePalette(PillowTestCase):
img.putpalette(b"\xFF\x00\x00\x00\xFF\x00\x00\x00\xFF") # RGB
img.save(outfile, format="PNG")
reloaded = Image.open(outfile)
with Image.open(outfile) as reloaded:
self.assert_image_equal(img, reloaded)
def test_invalid_palette(self):

View File

@ -31,8 +31,8 @@ class TestImageTk(PillowTestCase):
def test_kw(self):
TEST_JPG = "Tests/images/hopper.jpg"
TEST_PNG = "Tests/images/hopper.png"
im1 = Image.open(TEST_JPG)
im2 = Image.open(TEST_PNG)
with Image.open(TEST_JPG) as im1:
with Image.open(TEST_PNG) as im2:
with open(TEST_PNG, "rb") as fp:
data = fp.read()
kw = {"file": TEST_JPG, "data": data}

View File

@ -44,6 +44,9 @@ class TestLibPack(PillowTestCase):
self.assert_pack("LA", "LA", 2, (1, 2), (3, 4), (5, 6))
self.assert_pack("LA", "LA;L", 2, (1, 4), (2, 5), (3, 6))
def test_La(self):
self.assert_pack("La", "La", 2, (1, 2), (3, 4), (5, 6))
def test_P(self):
self.assert_pack("P", "P;1", b"\xe4", 1, 1, 1, 0, 0, 255, 0, 0)
self.assert_pack("P", "P;2", b"\xe4", 3, 2, 1, 0)
@ -269,6 +272,9 @@ class TestLibUnpack(PillowTestCase):
self.assert_unpack("LA", "LA", 2, (1, 2), (3, 4), (5, 6))
self.assert_unpack("LA", "LA;L", 2, (1, 4), (2, 5), (3, 6))
def test_La(self):
self.assert_unpack("La", "La", 2, (1, 2), (3, 4), (5, 6))
def test_P(self):
self.assert_unpack("P", "P;1", b"\xe4", 1, 1, 1, 0, 0, 1, 0, 0)
self.assert_unpack("P", "P;2", b"\xe4", 3, 2, 1, 0)

View File

@ -101,7 +101,7 @@ class TestNumpy(PillowTestCase):
self.assert_deep_equal(px[x, y], np[y, x])
def test_16bit(self):
img = Image.open("Tests/images/16bit.cropped.tif")
with Image.open("Tests/images/16bit.cropped.tif") as img:
np_img = numpy.array(img)
self._test_img_equals_nparray(img, np_img)
self.assertEqual(np_img.dtype, numpy.dtype("<u2"))
@ -217,7 +217,7 @@ class TestNumpy(PillowTestCase):
from numpy import array
test_file = "Tests/images/hopper.png"
im = Image.open(test_file)
with Image.open(test_file) as im:
# Act/Assert
self.assert_warning(None, lambda: array(im))

View File

@ -6,7 +6,7 @@ from .helper import PillowTestCase
class TestPickle(PillowTestCase):
def helper_pickle_file(self, pickle, protocol=0, mode=None):
# Arrange
im = Image.open("Tests/images/hopper.jpg")
with Image.open("Tests/images/hopper.jpg") as im:
filename = self.tempfile("temp.pkl")
if mode:
im = im.convert(mode)
@ -23,7 +23,7 @@ class TestPickle(PillowTestCase):
def helper_pickle_string(
self, pickle, protocol=0, test_file="Tests/images/hopper.jpg", mode=None
):
im = Image.open(test_file)
with Image.open(test_file) as im:
if mode:
im = im.convert(mode)
@ -97,8 +97,8 @@ class TestPickle(PillowTestCase):
# Arrange
import pickle
im = Image.open("Tests/images/hopper.jpg")
filename = self.tempfile("temp.pkl")
with Image.open("Tests/images/hopper.jpg") as im:
im = im.convert("PA")
# Act / Assert

View File

@ -9,7 +9,6 @@ from .helper import PillowTestCase
class TestPsDraw(PillowTestCase):
def _create_document(self, ps):
im = Image.open("Tests/images/hopper.ppm")
title = "hopper"
box = (1 * 72, 2 * 72, 7 * 72, 10 * 72) # in points
@ -20,6 +19,7 @@ class TestPsDraw(PillowTestCase):
ps.line((7 * 72, 2 * 72), (1 * 72, 10 * 72))
# draw the image (75 dpi)
with Image.open("Tests/images/hopper.ppm") as im:
ps.image(box, im, 75)
ps.rectangle(box)

View File

@ -43,7 +43,7 @@ class TestToQImage(PillowQtTestCase, PillowTestCase):
data.save(tempfile)
# Check that it actually worked.
reloaded = Image.open(tempfile)
with Image.open(tempfile) as reloaded:
self.assert_image_equal(reloaded, src)
def test_segfault(self):

View File

@ -38,7 +38,7 @@ class TestShellInjection(PillowTestCase):
@unittest.skipUnless(cjpeg_available(), "cjpeg not available")
def test_save_cjpeg_filename(self):
im = Image.open(TEST_JPG)
with Image.open(TEST_JPG) as im:
self.assert_save_filename_check(im, JpegImagePlugin._save_cjpeg)
@unittest.skipUnless(netpbm_available(), "netpbm not available")

View File

@ -1,57 +0,0 @@
import io
import queue
import sys
import threading
import time
from PIL import Image
test_format = sys.argv[1] if len(sys.argv) > 1 else "PNG"
im = Image.open("Tests/images/hopper.ppm")
im.load()
queue = queue.Queue()
result = []
class Worker(threading.Thread):
def run(self):
while True:
im = queue.get()
if im is None:
queue.task_done()
sys.stdout.write("x")
break
f = io.BytesIO()
im.save(f, test_format, optimize=1)
data = f.getvalue()
result.append(len(data))
im = Image.open(io.BytesIO(data))
im.load()
sys.stdout.write(".")
queue.task_done()
t0 = time.time()
threads = 20
jobs = 100
for i in range(threads):
w = Worker()
w.start()
for i in range(jobs):
queue.put(im)
for i in range(threads):
queue.put(None)
queue.join()
print()
print(time.time() - t0)
print(len(result), sum(result))
print(result)

View File

@ -1,26 +0,0 @@
from PIL import Image
def version(module, version):
v = getattr(module.core, version + "_version", None)
if v:
print(version, v)
version(Image, "jpeglib")
version(Image, "zlib")
version(Image, "libtiff")
try:
from PIL import ImageFont
except ImportError:
pass
else:
version(ImageFont, "freetype2")
try:
from PIL import ImageCms
except ImportError:
pass
else:
version(ImageCms, "littlecms")

View File

@ -7,24 +7,3 @@ build & install non-packaged dependencies; useful for testing with Travis CI.
``install_extra_test_images.sh`` can be used to install additional test images
that are used for Travis CI and AppVeyor.
The other scripts can be used to install all of the dependencies for
the listed operating systems/distros. The ``ubuntu_14.04.sh`` and
``debian_8.2.sh`` scripts have been tested on bare AWS images and will
install all required dependencies for the system Python 2.7 and 3.4
for all of the optional dependencies. Git may also be required prior
to running the script to actually download Pillow.
e.g.::
$ sudo apt-get install git
$ git clone https://github.com/python-pillow/Pillow.git
$ cd Pillow/depends
$ ./debian_8.2.sh
$ cd ..
$ git checkout [branch or tag]
$ virtualenv -p /usr/bin/python2.7 ~/vpy27
$ source ~/vpy27/bin/activate
$ make install
$ make test

View File

@ -1,5 +1,5 @@
#!/bin/sh
pkg -y install python python-dev ndk-sysroot clang make \
libjpeg-turbo-dev
pkg install -y python ndk-sysroot clang make \
libjpeg-turbo

View File

@ -15,24 +15,25 @@ Notes
.. note:: Pillow is supported on the following Python versions
+-------------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
|**Python** |**2.4**|**2.5**|**2.6**|**2.7**|**3.2**|**3.3**|**3.4**|**3.5**|**3.6**|**3.7**|**3.8**|
|**Python** |**3.8**|**3.7**|**3.6**|**3.5**|**3.4**|**3.3**|**3.2**|**2.7**|**2.6**|**2.5**|**2.4**|
+-------------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
|Pillow < 2 | Yes | Yes | Yes | Yes | | | | | | | |
|Pillow >= 7 | Yes | Yes | Yes | Yes | | | | | | | |
+-------------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
|Pillow 2 - 3 | | | Yes | Yes | Yes | Yes | Yes | Yes | | | |
|Pillow 6.2.1 | Yes | Yes | Yes | Yes | | | | Yes | | | |
+-------------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
|Pillow 4 | | | | Yes | | Yes | Yes | Yes | Yes | | |
|Pillow 6.0 - 6.2.0 | | Yes | Yes | Yes | | | | Yes | | | |
+-------------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
|Pillow 5.0 - 5.1 | | | | Yes | | | Yes | Yes | Yes | | |
|Pillow 5.2 - 5.4 | | Yes | Yes | Yes | Yes | | | Yes | | | |
+-------------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
|Pillow 5.2 - 5.4 | | | | Yes | | | Yes | Yes | Yes | Yes | |
|Pillow 5.0 - 5.1 | | | Yes | Yes | Yes | | | Yes | | | |
+-------------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
|Pillow 6.0 - 6.2.0 | | | | Yes | | | | Yes | Yes | Yes | |
|Pillow 4 | | | Yes | Yes | Yes | Yes | | Yes | | | |
+-------------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
|Pillow 6.2.1 | | | | Yes | | | | Yes | Yes | Yes | Yes |
|Pillow 2 - 3 | | | | Yes | Yes | Yes | Yes | Yes | Yes | | |
+-------------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
|Pillow >= 7 | | | | | | | | Yes | Yes | Yes | Yes |
|Pillow < 2 | | | | | | | | Yes | Yes | Yes | Yes |
+-------------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
Basic Installation
@ -95,14 +96,13 @@ Pillow can be installed on FreeBSD via the official Ports or Packages systems:
**Packages**::
$ pkg install py27-pillow
$ pkg install py36-pillow
.. note::
The `Pillow FreeBSD port
<https://www.freshports.org/graphics/py-pillow/>`_ and packages
are tested by the ports team with all supported FreeBSD versions
and against Python 2.7 and 3.x.
are tested by the ports team with all supported FreeBSD versions.
Building From Source
@ -174,8 +174,6 @@ Many of Pillow's features require external libraries:
* Libimagequant is licensed GPLv3, which is more restrictive than
the Pillow license, therefore we will not be distributing binaries
with libimagequant support enabled.
* Windows support: Libimagequant requires VS2015/MSVC 19 to compile,
so it is unlikely to work with Python 2.7 on Windows.
* **libraqm** provides complex text layout support.
@ -189,7 +187,7 @@ Many of Pillow's features require external libraries:
libraqm.
* libraqm is dynamically loaded in Pillow 5.0.0 and above, so support
is available if all the libraries are installed.
* Windows support: Raqm support is currently unsupported on Windows.
* Windows support: Raqm is not included in prebuilt wheels
Once you have installed the prerequisites, run::
@ -361,8 +359,8 @@ Building on Android
Basic Android support has been added for compilation within the Termux
environment. The dependencies can be installed by::
$ pkg -y install python python-dev ndk-sysroot clang make \
libjpeg-turbo-dev
$ pkg install -y python ndk-sysroot clang make \
libjpeg-turbo
This has been tested within the Termux app on ChromeOS, on x86.
@ -381,40 +379,44 @@ Continuous Integration Targets
These platforms are built and tested for every change.
+----------------------------------+-------------------------------+-----------------------+
|**Operating system** |**Tested Python versions** |**Tested Architecture**|
+----------------------------------+-------------------------------+-----------------------+
| Alpine | 2.7, 3.7 |x86-64 |
+----------------------------------+-------------------------------+-----------------------+
| Arch | 2.7, 3.7 |x86-64 |
+----------------------------------+-------------------------------+-----------------------+
| Amazon Linux 1 | 2.7, 3.6 |x86-64 |
+----------------------------------+-------------------------------+-----------------------+
| Amazon Linux 2 | 2.7, 3.6 |x86-64 |
+----------------------------------+-------------------------------+-----------------------+
| CentOS 6 | 2.7, 3.6 |x86-64 |
+----------------------------------+-------------------------------+-----------------------+
| CentOS 7 | 2.7, 3.6 |x86-64 |
+----------------------------------+-------------------------------+-----------------------+
| Debian 9 Stretch | 2.7, 3.5 |x86 |
+----------------------------------+-------------------------------+-----------------------+
| Debian 10 Buster | 2.7, 3.7 |x86 |
+----------------------------------+-------------------------------+-----------------------+
| Fedora 30 | 2.7, 3.7 |x86-64 |
+----------------------------------+-------------------------------+-----------------------+
+----------------------------------+--------------------------+-----------------------+
|**Operating system** |**Tested Python versions**|**Tested architecture**|
+----------------------------------+--------------------------+-----------------------+
| Alpine | 3.7 |x86-64 |
+----------------------------------+--------------------------+-----------------------+
| Arch | 3.7 |x86-64 |
+----------------------------------+--------------------------+-----------------------+
| Amazon Linux 1 | 3.6 |x86-64 |
+----------------------------------+--------------------------+-----------------------+
| Amazon Linux 2 | 3.6 |x86-64 |
+----------------------------------+--------------------------+-----------------------+
| CentOS 6 | 3.6 |x86-64 |
+----------------------------------+--------------------------+-----------------------+
| CentOS 7 | 3.6 |x86-64 |
+----------------------------------+--------------------------+-----------------------+
| Debian 9 Stretch | 3.5 |x86 |
+----------------------------------+--------------------------+-----------------------+
| Debian 10 Buster | 3.7 |x86 |
+----------------------------------+--------------------------+-----------------------+
| Fedora 30 | 3.7 |x86-64 |
+----------------------------------+--------------------------+-----------------------+
| Fedora 31 | 3.7 |x86-64 |
+----------------------------------+-------------------------------+-----------------------+
| macOS 10.13 High Sierra* | 2.7, 3.5, 3.6, 3.7, 3.8 |x86-64 |
+----------------------------------+-------------------------------+-----------------------+
| Ubuntu Linux 16.04 LTS | 2.7, 3.5, 3.6, 3.7, 3.8, |x86-64 |
| | PyPy, PyPy3 | |
+----------------------------------+-------------------------------+-----------------------+
| Windows Server 2012 R2 | 2.7, 3.5, 3.6, 3.7, 3.8 |x86, x86-64 |
| +-------------------------------+-----------------------+
| | PyPy, 3.7/MinGW |x86 |
+----------------------------------+-------------------------------+-----------------------+
+----------------------------------+--------------------------+-----------------------+
| macOS 10.13 High Sierra | 3.5, 3.6, 3.7, 3.8 |x86-64 |
+----------------------------------+--------------------------+-----------------------+
| macOS 10.15 Catalina | PyPy3 |x86-64 |
+----------------------------------+--------------------------+-----------------------+
| Ubuntu Linux 16.04 LTS | 3.5, 3.6, 3.7, 3.8, PyPy3|x86-64 |
+----------------------------------+--------------------------+-----------------------+
| Windows Server 2012 R2 | 3.5, 3.8 |x86, x86-64 |
| +--------------------------+-----------------------+
| | PyPy3, 3.7/MinGW |x86 |
+----------------------------------+--------------------------+-----------------------+
| Windows Server 2019 | 3.5, 3.6, 3.7, 3.8 |x86, x86-64 |
| +--------------------------+-----------------------+
| | PyPy3 |x86 |
+----------------------------------+--------------------------+-----------------------+
\* macOS CI is not run for every commit, but is run for every release.
Other Platforms
^^^^^^^^^^^^^^^

View File

@ -2140,11 +2140,11 @@ class Image:
# preserve aspect ratio
x, y = self.size
if x > size[0]:
y = int(max(y * size[0] / x, 1))
x = int(size[0])
y = max(round(y * size[0] / x), 1)
x = size[0]
if y > size[1]:
x = int(max(x * size[1] / y, 1))
y = int(size[1])
x = max(round(x * size[1] / y), 1)
y = size[1]
size = x, y
box = None
@ -2576,9 +2576,12 @@ def fromarray(obj, mode=None):
if mode is None:
try:
typekey = (1, 1) + shape[2:], arr["typestr"]
mode, rawmode = _fromarray_typemap[typekey]
except KeyError:
raise TypeError("Cannot handle this data type")
try:
mode, rawmode = _fromarray_typemap[typekey]
except KeyError:
raise TypeError("Cannot handle this data type: %s, %s" % typekey)
else:
rawmode = mode
if mode in ["1", "L", "I", "P", "F"]:

View File

@ -109,7 +109,10 @@ def APP(self, marker):
while blocks[offset : offset + 4] == b"8BIM":
offset += 4
# resource code
try:
code = i16(blocks, offset)
except struct.error:
break
offset += 2
# resource name (usually empty)
name_len = i8(blocks[offset])

View File

@ -33,7 +33,10 @@ Possible subsampling values are 0, 1 and 2 that correspond to 4:4:4, 4:2:2 and
4:2:0.
You can get the subsampling of a JPEG with the
`JpegImagePlugin.get_subsampling(im)` function.
`JpegImagePlugin.get_sampling(im)` function.
In JPEG compressed data a JPEG marker is used instead of an EXIF tag.
(ref.: https://www.exiv2.org/tags.html)
Quantization tables

View File

@ -82,7 +82,10 @@ class MpoImageFile(JpegImagePlugin.JpegImageFile):
self.offset = self.__mpoffsets[frame]
self.fp.seek(self.offset + 2) # skip SOI marker
if i16(self.fp.read(2)) == 0xFFE1: # APP1
segment = self.fp.read(2)
if not segment:
raise ValueError("No data found for frame")
if i16(segment) == 0xFFE1: # APP1
n = i16(self.fp.read(2)) - 2
self.info["exif"] = ImageFile._safe_read(self.fp, n)

View File

@ -326,24 +326,12 @@ getfont(PyObject* self_, PyObject* args, PyObject* kw)
static int
font_getchar(PyObject* string, int index, FT_ULong* char_out)
{
#if (defined(PYPY_VERSION_NUM))
if (PyUnicode_Check(string)) {
Py_UNICODE* p = PyUnicode_AS_UNICODE(string);
int size = PyUnicode_GET_SIZE(string);
if (index >= size)
return 0;
*char_out = p[index];
return 1;
}
#else
if (PyUnicode_Check(string)) {
if (index >= PyUnicode_GET_LENGTH(string))
return 0;
*char_out = PyUnicode_READ_CHAR(string, index);
return 1;
}
#endif
return 0;
}
@ -363,7 +351,7 @@ text_layout_raqm(PyObject* string, FontObject* self, const char* dir, PyObject *
goto failed;
}
#if (defined(PYPY_VERSION_NUM))
#if (defined(PYPY_VERSION_NUM) && (PYPY_VERSION_NUM < 0x07020000))
if (PyUnicode_Check(string)) {
Py_UNICODE *text = PyUnicode_AS_UNICODE(string);
Py_ssize_t size = PyUnicode_GET_SIZE(string);
@ -392,7 +380,7 @@ text_layout_raqm(PyObject* string, FontObject* self, const char* dir, PyObject *
and raqm fails with empty strings */
goto failed;
}
int set_text = (*p_raqm.set_text)(rq, (const uint32_t *)(text), size);
int set_text = (*p_raqm.set_text)(rq, text, size);
PyMem_Free(text);
if (!set_text) {
PyErr_SetString(PyExc_ValueError, "raqm_set_text() failed");

View File

@ -555,6 +555,9 @@ static struct {
{"LA", "LA", 16, packLA},
{"LA", "LA;L", 16, packLAL},
/* greyscale w. alpha premultiplied */
{"La", "La", 16, packLA},
/* palette */
{"P", "P;1", 1, pack1},
{"P", "P;2", 2, packP2},

View File

@ -1307,6 +1307,9 @@ static struct {
{"LA", "LA", 16, unpackLA},
{"LA", "LA;L", 16, unpackLAL},
/* greyscale w. alpha premultiplied */
{"La", "La", 16, unpackLA},
/* palette */
{"P", "P;1", 1, unpackP1},
{"P", "P;2", 2, unpackP2},
@ -1384,7 +1387,6 @@ static struct {
{"RGBX", "RGBX;16N", 64, unpackRGBA16B},
#endif
/* true colour w. alpha premultiplied */
{"RGBa", "RGBa", 32, copy4},
{"RGBa", "BGRa", 32, unpackBGRA},

View File

@ -300,33 +300,6 @@ endlocal
)
def build_ghostscript(compiler, bit):
script = (
r"""
rem Build gs
setlocal
"""
+ vc_setup(compiler, bit)
+ r"""
set MSVC_VERSION="""
+ {"2010": "90", "2015": "14"}[compiler["vc_version"]]
+ r"""
set RCOMP="C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Bin\RC.Exe"
cd /D %%GHOSTSCRIPT%%
"""
)
if bit == 64:
script += r"""
set WIN64=""
"""
script += r"""
nmake -nologo -f psi/msvc.mak
copy /Y /B bin\ C:\Python27\
endlocal
"""
return script % compiler
def add_compiler(compiler, bit):
script.append(setup_compiler(compiler))
script.append(nmake_libs(compiler, bit))
@ -336,7 +309,6 @@ def add_compiler(compiler, bit):
script.append(msbuild_freetype(compiler, bit))
script.append(build_lcms2(compiler))
script.append(nmake_openjpeg(compiler, bit))
script.append(build_ghostscript(compiler, bit))
script.append(end_compiler())

View File

@ -54,11 +54,6 @@ libs = {
"filename": "lcms2-2.8.zip",
"dir": "lcms2-2.8",
},
"ghostscript": {
"url": "https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs927/ghostscript-9.27.tar.gz", # noqa: E501
"filename": "ghostscript-9.27.tar.gz",
"dir": "ghostscript-9.27",
},
"tcl-8.5": {
"url": SF_MIRROR + "/project/tcl/Tcl/8.5.19/tcl8519-src.zip",
"filename": "tcl8519-src.zip",