mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-26 01:46:18 +03:00
commit
b97369f6b3
|
@ -24,13 +24,11 @@
|
|||
# See the README file for information on usage and redistribution.
|
||||
#
|
||||
|
||||
from PIL import Image, ImageFile, ImagePalette, _binary
|
||||
|
||||
__version__ = "0.9"
|
||||
|
||||
|
||||
from PIL import Image, ImageFile, ImagePalette, _binary
|
||||
|
||||
|
||||
# --------------------------------------------------------------------
|
||||
# Helpers
|
||||
|
||||
|
@ -271,7 +269,7 @@ def _save(im, fp, filename):
|
|||
pass # write uncompressed file
|
||||
|
||||
if im.mode in RAWMODE:
|
||||
imOut = im
|
||||
im_out = im
|
||||
else:
|
||||
# convert on the fly (EXPERIMENTAL -- I'm not sure PIL
|
||||
# should automatically convert images on save...)
|
||||
|
@ -279,9 +277,9 @@ def _save(im, fp, filename):
|
|||
palette_size = 256
|
||||
if im.palette:
|
||||
palette_size = len(im.palette.getdata()[1]) // 3
|
||||
imOut = im.convert("P", palette=1, colors=palette_size)
|
||||
im_out = im.convert("P", palette=1, colors=palette_size)
|
||||
else:
|
||||
imOut = im.convert("L")
|
||||
im_out = im.convert("L")
|
||||
|
||||
# header
|
||||
try:
|
||||
|
@ -290,7 +288,7 @@ def _save(im, fp, filename):
|
|||
palette = None
|
||||
im.encoderinfo["optimize"] = im.encoderinfo.get("optimize", True)
|
||||
|
||||
header, usedPaletteColors = getheader(imOut, palette, im.encoderinfo)
|
||||
header, used_palette_colors = getheader(im_out, palette, im.encoderinfo)
|
||||
for s in header:
|
||||
fp.write(s)
|
||||
|
||||
|
@ -315,26 +313,26 @@ def _save(im, fp, filename):
|
|||
else:
|
||||
transparency = int(transparency)
|
||||
# optimize the block away if transparent color is not used
|
||||
transparentColorExists = True
|
||||
transparent_color_exists = True
|
||||
# adjust the transparency index after optimize
|
||||
if usedPaletteColors is not None and len(usedPaletteColors) < 256:
|
||||
for i in range(len(usedPaletteColors)):
|
||||
if usedPaletteColors[i] == transparency:
|
||||
if used_palette_colors is not None and len(used_palette_colors) < 256:
|
||||
for i in range(len(used_palette_colors)):
|
||||
if used_palette_colors[i] == transparency:
|
||||
transparency = i
|
||||
transparentColorExists = True
|
||||
transparent_color_exists = True
|
||||
break
|
||||
else:
|
||||
transparentColorExists = False
|
||||
transparent_color_exists = False
|
||||
|
||||
# transparency extension block
|
||||
if transparentColorExists:
|
||||
if transparent_color_exists:
|
||||
fp.write(b"!" +
|
||||
o8(249) + # extension intro
|
||||
o8(4) + # length
|
||||
o8(1) + # transparency info present
|
||||
o16(0) + # duration
|
||||
o8(transparency) # transparency index
|
||||
+ o8(0))
|
||||
o8(transparency) + # transparency index
|
||||
o8(0))
|
||||
|
||||
# local image header
|
||||
fp.write(b"," +
|
||||
|
@ -344,9 +342,9 @@ def _save(im, fp, filename):
|
|||
o8(flags) + # flags
|
||||
o8(8)) # bits
|
||||
|
||||
imOut.encoderconfig = (8, interlace)
|
||||
ImageFile._save(imOut, fp, [("gif", (0, 0)+im.size, 0,
|
||||
RAWMODE[imOut.mode])])
|
||||
im_out.encoderconfig = (8, interlace)
|
||||
ImageFile._save(im_out, fp, [("gif", (0, 0)+im.size, 0,
|
||||
RAWMODE[im_out.mode])])
|
||||
|
||||
fp.write(b"\0") # end of image data
|
||||
|
||||
|
@ -422,74 +420,75 @@ def getheader(im, palette=None, info=None):
|
|||
|
||||
if im.mode == "P":
|
||||
if palette and isinstance(palette, bytes):
|
||||
sourcePalette = palette[:768]
|
||||
source_palette = palette[:768]
|
||||
else:
|
||||
sourcePalette = im.im.getpalette("RGB")[:768]
|
||||
source_palette = im.im.getpalette("RGB")[:768]
|
||||
else: # L-mode
|
||||
if palette and isinstance(palette, bytes):
|
||||
sourcePalette = palette[:768]
|
||||
source_palette = palette[:768]
|
||||
else:
|
||||
sourcePalette = bytearray([i//3 for i in range(768)])
|
||||
source_palette = bytearray([i//3 for i in range(768)])
|
||||
|
||||
usedPaletteColors = paletteBytes = None
|
||||
used_palette_colors = palette_bytes = None
|
||||
|
||||
if im.mode in ("P", "L") and optimize:
|
||||
usedPaletteColors = []
|
||||
used_palette_colors = []
|
||||
|
||||
# check which colors are used
|
||||
i = 0
|
||||
for count in im.histogram():
|
||||
if count:
|
||||
usedPaletteColors.append(i)
|
||||
used_palette_colors.append(i)
|
||||
i += 1
|
||||
|
||||
# create the new palette if not every color is used
|
||||
if len(usedPaletteColors) < 256:
|
||||
paletteBytes = b""
|
||||
newPositions = {}
|
||||
if len(used_palette_colors) < 256:
|
||||
palette_bytes = b""
|
||||
new_positions = {}
|
||||
|
||||
i = 0
|
||||
# pick only the used colors from the palette
|
||||
for oldPosition in usedPaletteColors:
|
||||
paletteBytes += sourcePalette[oldPosition*3:oldPosition*3+3]
|
||||
newPositions[oldPosition] = i
|
||||
for oldPosition in used_palette_colors:
|
||||
palette_bytes += source_palette[oldPosition*3:oldPosition*3+3]
|
||||
new_positions[oldPosition] = i
|
||||
i += 1
|
||||
|
||||
# replace the palette color id of all pixel with the new id
|
||||
imageBytes = bytearray(im.tobytes())
|
||||
for i in range(len(imageBytes)):
|
||||
imageBytes[i] = newPositions[imageBytes[i]]
|
||||
im.frombytes(bytes(imageBytes))
|
||||
newPaletteBytes = (paletteBytes +
|
||||
(768 - len(paletteBytes)) * b'\x00')
|
||||
im.putpalette(newPaletteBytes)
|
||||
im.palette = ImagePalette.ImagePalette("RGB", palette=paletteBytes,
|
||||
size=len(paletteBytes))
|
||||
image_bytes = bytearray(im.tobytes())
|
||||
for i in range(len(image_bytes)):
|
||||
image_bytes[i] = new_positions[image_bytes[i]]
|
||||
im.frombytes(bytes(image_bytes))
|
||||
new_palette_bytes = (palette_bytes +
|
||||
(768 - len(palette_bytes)) * b'\x00')
|
||||
im.putpalette(new_palette_bytes)
|
||||
im.palette = ImagePalette.ImagePalette("RGB",
|
||||
palette=palette_bytes,
|
||||
size=len(palette_bytes))
|
||||
|
||||
if not paletteBytes:
|
||||
paletteBytes = sourcePalette
|
||||
if not palette_bytes:
|
||||
palette_bytes = source_palette
|
||||
|
||||
# Logical Screen Descriptor
|
||||
# calculate the palette size for the header
|
||||
import math
|
||||
colorTableSize = int(math.ceil(math.log(len(paletteBytes)//3, 2)))-1
|
||||
if colorTableSize < 0:
|
||||
colorTableSize = 0
|
||||
color_table_size = int(math.ceil(math.log(len(palette_bytes)//3, 2)))-1
|
||||
if color_table_size < 0:
|
||||
color_table_size = 0
|
||||
# size of global color table + global color table flag
|
||||
header.append(o8(colorTableSize + 128))
|
||||
header.append(o8(color_table_size + 128))
|
||||
# background + reserved/aspect
|
||||
header.append(o8(0) + o8(0))
|
||||
# end of Logical Screen Descriptor
|
||||
|
||||
# add the missing amount of bytes
|
||||
# the palette has to be 2<<n in size
|
||||
actualTargetSizeDiff = (2 << colorTableSize) - len(paletteBytes)//3
|
||||
if actualTargetSizeDiff > 0:
|
||||
paletteBytes += o8(0) * 3 * actualTargetSizeDiff
|
||||
actual_target_size_diff = (2 << color_table_size) - len(palette_bytes)//3
|
||||
if actual_target_size_diff > 0:
|
||||
palette_bytes += o8(0) * 3 * actual_target_size_diff
|
||||
|
||||
# Header + Logical Screen Descriptor + Global Color Table
|
||||
header.append(paletteBytes)
|
||||
return header, usedPaletteColors
|
||||
header.append(palette_bytes)
|
||||
return header, used_palette_colors
|
||||
|
||||
|
||||
def getdata(im, offset=(0, 0), **params):
|
||||
|
@ -497,7 +496,7 @@ def getdata(im, offset=(0, 0), **params):
|
|||
The first string is a local image header, the rest contains
|
||||
encoded image data."""
|
||||
|
||||
class collector:
|
||||
class Collector:
|
||||
data = []
|
||||
|
||||
def write(self, data):
|
||||
|
@ -505,7 +504,7 @@ def getdata(im, offset=(0, 0), **params):
|
|||
|
||||
im.load() # make sure raster data is available
|
||||
|
||||
fp = collector()
|
||||
fp = Collector()
|
||||
|
||||
try:
|
||||
im.encoderinfo = params
|
||||
|
|
|
@ -262,7 +262,7 @@ def truetype(font=None, size=10, index=0, encoding="", filename=None):
|
|||
return FreeTypeFont(font, size, index, encoding)
|
||||
except IOError:
|
||||
ttf_filename = os.path.basename(font)
|
||||
|
||||
|
||||
dirs = []
|
||||
if sys.platform == "win32":
|
||||
# check the windows font repository
|
||||
|
@ -281,11 +281,11 @@ def truetype(font=None, size=10, index=0, encoding="", filename=None):
|
|||
elif sys.platform == 'darwin':
|
||||
dirs += ['/Library/Fonts', '/System/Library/Fonts',
|
||||
os.path.expanduser('~/Library/Fonts')]
|
||||
|
||||
|
||||
ext = os.path.splitext(ttf_filename)[1]
|
||||
firstFontWithADifferentExtension = None
|
||||
for dir in dirs:
|
||||
for walkroot, walkdir, walkfilenames in os.walk(dir):
|
||||
first_font_with_a_different_extension = None
|
||||
for directory in dirs:
|
||||
for walkroot, walkdir, walkfilenames in os.walk(directory):
|
||||
for walkfilename in walkfilenames:
|
||||
if ext and walkfilename == ttf_filename:
|
||||
fontpath = os.path.join(walkroot, walkfilename)
|
||||
|
@ -294,10 +294,11 @@ def truetype(font=None, size=10, index=0, encoding="", filename=None):
|
|||
fontpath = os.path.join(walkroot, walkfilename)
|
||||
if os.path.splitext(fontpath)[1] == '.ttf':
|
||||
return FreeTypeFont(fontpath, size, index, encoding)
|
||||
if not ext and firstFontWithADifferentExtension == None:
|
||||
firstFontWithADifferentExtension = fontpath
|
||||
if firstFontWithADifferentExtension:
|
||||
return FreeTypeFont(firstFontWithADifferentExtension, size, index, encoding)
|
||||
if not ext and first_font_with_a_different_extension is None:
|
||||
first_font_with_a_different_extension = fontpath
|
||||
if first_font_with_a_different_extension:
|
||||
return FreeTypeFont(first_font_with_a_different_extension, size,
|
||||
index, encoding)
|
||||
raise
|
||||
|
||||
|
||||
|
@ -310,15 +311,15 @@ def load_path(filename):
|
|||
:return: A font object.
|
||||
:exception IOError: If the file could not be read.
|
||||
"""
|
||||
for dir in sys.path:
|
||||
if isDirectory(dir):
|
||||
for directory in sys.path:
|
||||
if isDirectory(directory):
|
||||
if not isinstance(filename, str):
|
||||
if bytes is str:
|
||||
filename = filename.encode("utf-8")
|
||||
else:
|
||||
filename = filename.decode("utf-8")
|
||||
try:
|
||||
return load(os.path.join(dir, filename))
|
||||
return load(os.path.join(directory, filename))
|
||||
except IOError:
|
||||
pass
|
||||
raise IOError("cannot find font file")
|
||||
|
|
|
@ -12,14 +12,13 @@
|
|||
#
|
||||
# See the README file for information on usage and redistribution.
|
||||
#
|
||||
|
||||
__version__ = "0.1"
|
||||
|
||||
from PIL import Image, ImageFile
|
||||
import struct
|
||||
import os
|
||||
import io
|
||||
|
||||
__version__ = "0.1"
|
||||
|
||||
|
||||
def _parse_codestream(fp):
|
||||
"""Parse the JPEG 2000 codestream to extract the size and component
|
||||
|
@ -208,8 +207,8 @@ class Jpeg2KImageFile(ImageFile.ImageFile):
|
|||
|
||||
|
||||
def _accept(prefix):
|
||||
return (prefix[:4] == b'\xff\x4f\xff\x51'
|
||||
or prefix[:12] == b'\x00\x00\x00\x0cjP \x0d\x0a\x87\x0a')
|
||||
return (prefix[:4] == b'\xff\x4f\xff\x51' or
|
||||
prefix[:12] == b'\x00\x00\x00\x0cjP \x0d\x0a\x87\x0a')
|
||||
|
||||
|
||||
# ------------------------------------------------------------
|
||||
|
|
|
@ -22,6 +22,7 @@ try:
|
|||
self._saved = None
|
||||
self._is_saved = False
|
||||
self._value = value
|
||||
|
||||
def __enter__(self):
|
||||
# Patch the attr on the object
|
||||
if hasattr(self._parent_obj, self._attr_name):
|
||||
|
@ -31,6 +32,7 @@ try:
|
|||
else:
|
||||
setattr(self._parent_obj, self._attr_name, self._value)
|
||||
self._is_saved = False
|
||||
|
||||
def __exit__(self, type, value, traceback):
|
||||
# Restore the original value
|
||||
if self._is_saved:
|
||||
|
@ -48,14 +50,14 @@ try:
|
|||
ttf = ImageFont.truetype(FONT_PATH, FONT_SIZE)
|
||||
self.assertEqual(ttf.path, FONT_PATH)
|
||||
self.assertEqual(ttf.size, FONT_SIZE)
|
||||
|
||||
|
||||
ttf_copy = ttf.font_variant()
|
||||
self.assertEqual(ttf_copy.path, FONT_PATH)
|
||||
self.assertEqual(ttf_copy.size, FONT_SIZE)
|
||||
|
||||
|
||||
ttf_copy = ttf.font_variant(size=FONT_SIZE+1)
|
||||
self.assertEqual(ttf_copy.size, FONT_SIZE+1)
|
||||
|
||||
|
||||
second_font_path = "Tests/fonts/DejaVuSans.ttf"
|
||||
ttf_copy = ttf.font_variant(font=second_font_path)
|
||||
self.assertEqual(ttf_copy.path, second_font_path)
|
||||
|
@ -234,61 +236,77 @@ try:
|
|||
self.assert_image_equal(im, target_img)
|
||||
|
||||
def _test_fake_loading_font(self, path_to_fake, fontname):
|
||||
#Make a copy of FreeTypeFont so we can patch the original
|
||||
# Make a copy of FreeTypeFont so we can patch the original
|
||||
free_type_font = copy.deepcopy(ImageFont.FreeTypeFont)
|
||||
with SimplePatcher(ImageFont, '_FreeTypeFont', free_type_font):
|
||||
def loadable_font(filepath, size, index, encoding):
|
||||
if filepath == path_to_fake:
|
||||
return ImageFont._FreeTypeFont(FONT_PATH, size, index, encoding)
|
||||
return ImageFont._FreeTypeFont(filepath, size, index, encoding)
|
||||
return ImageFont._FreeTypeFont(FONT_PATH, size, index,
|
||||
encoding)
|
||||
return ImageFont._FreeTypeFont(filepath, size, index,
|
||||
encoding)
|
||||
with SimplePatcher(ImageFont, 'FreeTypeFont', loadable_font):
|
||||
font = ImageFont.truetype(fontname)
|
||||
#Make sure it's loaded
|
||||
# Make sure it's loaded
|
||||
name = font.getname()
|
||||
self.assertEqual(('FreeMono', 'Regular'), name)
|
||||
|
||||
|
||||
@unittest.skipIf(sys.platform.startswith('win32'), "requires Unix or MacOS")
|
||||
@unittest.skipIf(sys.platform.startswith('win32'),
|
||||
"requires Unix or MacOS")
|
||||
def test_find_linux_font(self):
|
||||
#A lot of mocking here - this is more for hitting code and catching
|
||||
#syntax like errors
|
||||
fontDirectory = '/usr/local/share/fonts'
|
||||
# A lot of mocking here - this is more for hitting code and
|
||||
# catching syntax like errors
|
||||
font_directory = '/usr/local/share/fonts'
|
||||
with SimplePatcher(sys, 'platform', 'linux'):
|
||||
patched_env = copy.deepcopy(os.environ)
|
||||
patched_env['XDG_DATA_DIRS'] = '/usr/share/:/usr/local/share/'
|
||||
with SimplePatcher(os, 'environ', patched_env):
|
||||
def fake_walker(path):
|
||||
if path == fontDirectory:
|
||||
return [(path, [], ['Arial.ttf', 'Single.otf', 'Duplicate.otf', 'Duplicate.ttf'], )]
|
||||
if path == font_directory:
|
||||
return [(path, [], [
|
||||
'Arial.ttf', 'Single.otf', 'Duplicate.otf',
|
||||
'Duplicate.ttf'], )]
|
||||
return [(path, [], ['some_random_font.ttf'], )]
|
||||
with SimplePatcher(os, 'walk', fake_walker):
|
||||
# Test that the font loads both with and without the extension
|
||||
self._test_fake_loading_font(fontDirectory+'/Arial.ttf', 'Arial.ttf')
|
||||
self._test_fake_loading_font(fontDirectory+'/Arial.ttf', 'Arial')
|
||||
|
||||
# Test that non-ttf fonts can be found without the extension
|
||||
self._test_fake_loading_font(fontDirectory+'/Single.otf', 'Single')
|
||||
|
||||
# Test that ttf fonts are preferred if the extension is not specified
|
||||
self._test_fake_loading_font(fontDirectory+'/Duplicate.ttf', 'Duplicate')
|
||||
# Test that the font loads both with and without the
|
||||
# extension
|
||||
self._test_fake_loading_font(
|
||||
font_directory+'/Arial.ttf', 'Arial.ttf')
|
||||
self._test_fake_loading_font(
|
||||
font_directory+'/Arial.ttf', 'Arial')
|
||||
|
||||
@unittest.skipIf(sys.platform.startswith('win32'), "requires Unix or MacOS")
|
||||
# Test that non-ttf fonts can be found without the
|
||||
# extension
|
||||
self._test_fake_loading_font(
|
||||
font_directory+'/Single.otf', 'Single')
|
||||
|
||||
# Test that ttf fonts are preferred if the extension is
|
||||
# not specified
|
||||
self._test_fake_loading_font(
|
||||
font_directory+'/Duplicate.ttf', 'Duplicate')
|
||||
|
||||
@unittest.skipIf(sys.platform.startswith('win32'),
|
||||
"requires Unix or MacOS")
|
||||
def test_find_osx_font(self):
|
||||
#Like the linux test, more cover hitting code rather than testing
|
||||
#correctness.
|
||||
fontDirectory = '/System/Library/Fonts'
|
||||
# Like the linux test, more cover hitting code rather than testing
|
||||
# correctness.
|
||||
font_directory = '/System/Library/Fonts'
|
||||
with SimplePatcher(sys, 'platform', 'darwin'):
|
||||
def fake_walker(path):
|
||||
if path == fontDirectory:
|
||||
return [(path, [], ['Arial.ttf', 'Single.otf', 'Duplicate.otf', 'Duplicate.ttf'], )]
|
||||
if path == font_directory:
|
||||
return [(path, [],
|
||||
['Arial.ttf', 'Single.otf',
|
||||
'Duplicate.otf', 'Duplicate.ttf'], )]
|
||||
return [(path, [], ['some_random_font.ttf'], )]
|
||||
with SimplePatcher(os, 'walk', fake_walker):
|
||||
self._test_fake_loading_font(fontDirectory+'/Arial.ttf', 'Arial.ttf')
|
||||
self._test_fake_loading_font(fontDirectory+'/Arial.ttf', 'Arial')
|
||||
|
||||
self._test_fake_loading_font(fontDirectory+'/Single.otf', 'Single')
|
||||
|
||||
self._test_fake_loading_font(fontDirectory+'/Duplicate.ttf', 'Duplicate')
|
||||
self._test_fake_loading_font(
|
||||
font_directory+'/Arial.ttf', 'Arial.ttf')
|
||||
self._test_fake_loading_font(
|
||||
font_directory+'/Arial.ttf', 'Arial')
|
||||
self._test_fake_loading_font(
|
||||
font_directory+'/Single.otf', 'Single')
|
||||
self._test_fake_loading_font(
|
||||
font_directory+'/Duplicate.ttf', 'Duplicate')
|
||||
|
||||
|
||||
except ImportError:
|
||||
|
|
|
@ -14,6 +14,7 @@ class TestPyroma(PillowTestCase):
|
|||
def setUp(self):
|
||||
try:
|
||||
import pyroma
|
||||
assert pyroma # Ignore warning
|
||||
except ImportError:
|
||||
self.skipTest("ImportError")
|
||||
|
||||
|
@ -26,9 +27,10 @@ class TestPyroma(PillowTestCase):
|
|||
|
||||
# Assert
|
||||
if 'rc' in PILLOW_VERSION:
|
||||
#Pyroma needs to chill about RC versions and not kill all our tests.
|
||||
self.assertEqual(rating, (9,
|
||||
['The packages version number does not comply with PEP-386.']))
|
||||
# Pyroma needs to chill about RC versions
|
||||
# and not kill all our tests.
|
||||
self.assertEqual(rating, (9, [
|
||||
'The packages version number does not comply with PEP-386.']))
|
||||
|
||||
else:
|
||||
# Should have a perfect score
|
||||
|
|
Loading…
Reference in New Issue
Block a user