Apply @Arfrever patch, fixes #258

This commit is contained in:
Alex Clark 2013-06-30 07:04:42 -04:00
parent 54cb132b83
commit fd29e707e9
10 changed files with 31 additions and 37 deletions

View File

@ -26,6 +26,7 @@
__version__ = "0.1" __version__ = "0.1"
from PIL import ImageFile, ImagePalette, _binary from PIL import ImageFile, ImagePalette, _binary
from PIL._util import isPath
try: try:
import builtins import builtins
@ -77,7 +78,7 @@ def open(fp, mode = "r"):
if mode != "r": if mode != "r":
raise ValueError("bad mode") raise ValueError("bad mode")
if isinstance(fp, str): if isPath(fp):
filename = fp filename = fp
fp = builtins.open(fp, "rb") fp = builtins.open(fp, "rb")
else: else:

View File

@ -81,6 +81,7 @@ except ImportError:
from PIL import ImageMode from PIL import ImageMode
from PIL._binary import i8, o8 from PIL._binary import i8, o8
from PIL._util import isPath, isStringType
import os, sys import os, sys
@ -88,26 +89,12 @@ import os, sys
import collections import collections
import numbers import numbers
if bytes is str:
def isStringType(t):
return isinstance(t, basestring)
else:
def isStringType(t):
return isinstance(t, str)
## ##
# (Internal) Checks if an object is an image object. # (Internal) Checks if an object is an image object.
def isImageType(t): def isImageType(t):
return hasattr(t, "im") return hasattr(t, "im")
##
# (Internal) Checks if an object is a string, and that it points to a
# directory.
def isDirectory(f):
return isStringType(f) and os.path.isdir(f)
# #
# Debug level # Debug level
@ -1421,10 +1408,10 @@ class Image:
def save(self, fp, format=None, **params): def save(self, fp, format=None, **params):
"Save image to file or stream" "Save image to file or stream"
if isStringType(fp): if isPath(fp):
filename = fp filename = fp
else: else:
if hasattr(fp, "name") and isStringType(fp.name): if hasattr(fp, "name") and isPath(fp.name):
filename = fp.name filename = fp.name
else: else:
filename = "" filename = ""
@ -1455,7 +1442,7 @@ class Image:
init() init()
save_handler = SAVE[format.upper()] # unknown format save_handler = SAVE[format.upper()] # unknown format
if isStringType(fp): if isPath(fp):
fp = builtins.open(fp, "wb") fp = builtins.open(fp, "wb")
close = 1 close = 1
else: else:
@ -1984,7 +1971,7 @@ def open(fp, mode="r"):
if mode != "r": if mode != "r":
raise ValueError("bad mode") raise ValueError("bad mode")
if isStringType(fp): if isPath(fp):
filename = fp filename = fp
fp = builtins.open(fp, "rb") fp = builtins.open(fp, "rb")
else: else:

View File

@ -83,6 +83,7 @@ VERSION = "0.1.0 pil"
from PIL import Image from PIL import Image
from PIL import _imagingcms from PIL import _imagingcms
from PIL._util import isStringType
core = _imagingcms core = _imagingcms
@ -139,7 +140,7 @@ class ImageCmsProfile:
def __init__(self, profile): def __init__(self, profile):
# accepts a string (filename), a file-like object, or a low-level # accepts a string (filename), a file-like object, or a low-level
# profile object # profile object
if Image.isStringType(profile): if isStringType(profile):
self._set(core.profile_open(profile), profile) self._set(core.profile_open(profile), profile)
elif hasattr(profile, "read"): elif hasattr(profile, "read"):
self._set(core.profile_frombytes(profile.read())) self._set(core.profile_frombytes(profile.read()))

View File

@ -33,6 +33,7 @@
import numbers import numbers
from PIL import Image, ImageColor from PIL import Image, ImageColor
from PIL._util import isStringType
try: try:
import warnings import warnings
@ -98,7 +99,7 @@ class ImageDraw:
"'setink' is deprecated; use keyword arguments instead", "'setink' is deprecated; use keyword arguments instead",
DeprecationWarning, stacklevel=2 DeprecationWarning, stacklevel=2
) )
if Image.isStringType(ink): if isStringType(ink):
ink = ImageColor.getcolor(ink, self.mode) ink = ImageColor.getcolor(ink, self.mode)
if self.palette and not isinstance(ink, numbers.Number): if self.palette and not isinstance(ink, numbers.Number):
ink = self.palette.getcolor(ink) ink = self.palette.getcolor(ink)
@ -141,13 +142,13 @@ class ImageDraw:
ink = self.ink ink = self.ink
else: else:
if ink is not None: if ink is not None:
if Image.isStringType(ink): if isStringType(ink):
ink = ImageColor.getcolor(ink, self.mode) ink = ImageColor.getcolor(ink, self.mode)
if self.palette and not isinstance(ink, numbers.Number): if self.palette and not isinstance(ink, numbers.Number):
ink = self.palette.getcolor(ink) ink = self.palette.getcolor(ink)
ink = self.draw.draw_ink(ink, self.mode) ink = self.draw.draw_ink(ink, self.mode)
if fill is not None: if fill is not None:
if Image.isStringType(fill): if isStringType(fill):
fill = ImageColor.getcolor(fill, self.mode) fill = ImageColor.getcolor(fill, self.mode)
if self.palette and not isinstance(fill, numbers.Number): if self.palette and not isinstance(fill, numbers.Number):
fill = self.palette.getcolor(fill) fill = self.palette.getcolor(fill)

View File

@ -28,6 +28,7 @@
# #
from PIL import Image from PIL import Image
from PIL._util import isPath
import traceback, os import traceback, os
import io import io
@ -81,7 +82,7 @@ class ImageFile(Image.Image):
self.decoderconfig = () self.decoderconfig = ()
self.decodermaxblock = MAXBLOCK self.decodermaxblock = MAXBLOCK
if Image.isStringType(fp): if isPath(fp):
# filename # filename
self.fp = open(fp, "rb") self.fp = open(fp, "rb")
self.filename = fp self.filename = fp

View File

@ -28,6 +28,7 @@
from __future__ import print_function from __future__ import print_function
from PIL import Image from PIL import Image
from PIL._util import isDirectory, isPath
import os, sys import os, sys
try: try:
@ -45,13 +46,6 @@ try:
except ImportError: except ImportError:
core = _imagingft_not_installed() core = _imagingft_not_installed()
if bytes is str:
def isStringType(t):
return isinstance(t, basestring)
else:
def isStringType(t):
return isinstance(t, str)
# FIXME: add support for pilfont2 format (see FontFile.py) # FIXME: add support for pilfont2 format (see FontFile.py)
# -------------------------------------------------------------------- # --------------------------------------------------------------------
@ -148,7 +142,7 @@ class FreeTypeFont:
warnings.warn('file parameter deprecated, please use font parameter instead.', DeprecationWarning) warnings.warn('file parameter deprecated, please use font parameter instead.', DeprecationWarning)
font = file font = file
if isStringType(font): if isPath(font):
self.font = core.getfont(font, size, index, encoding) self.font = core.getfont(font, size, index, encoding)
else: else:
self.font_bytes = font.read() self.font_bytes = font.read()
@ -266,7 +260,12 @@ def truetype(font=None, size=10, index=0, encoding="", filename=None):
def load_path(filename): def load_path(filename):
"Load a font file, searching along the Python path." "Load a font file, searching along the Python path."
for dir in sys.path: for dir in sys.path:
if Image.isDirectory(dir): if isDirectory(dir):
if not isinstance(filename, "utf-8"):
if bytes is str:
filename = filename.encode("utf-8")
else:
filename = filename.decode("utf-8")
try: try:
return load(os.path.join(dir, filename)) return load(os.path.join(dir, filename))
except IOError: except IOError:

View File

@ -18,6 +18,7 @@
# #
from PIL import Image from PIL import Image
from PIL._util import isStringType
import operator import operator
from functools import reduce from functools import reduce
@ -43,7 +44,7 @@ def _border(border):
return left, top, right, bottom return left, top, right, bottom
def _color(color, mode): def _color(color, mode):
if Image.isStringType(color): if isStringType(color):
from PIL import ImageColor from PIL import ImageColor
color = ImageColor.getcolor(color, mode) color = ImageColor.getcolor(color, mode)
return color return color

View File

@ -16,6 +16,7 @@
# #
from PIL import Image from PIL import Image
from PIL._util import isPath
from PyQt4.QtGui import QImage, qRgb from PyQt4.QtGui import QImage, qRgb
@ -45,7 +46,7 @@ class ImageQt(QImage):
if hasattr(im, "toUtf8"): if hasattr(im, "toUtf8"):
# FIXME - is this really the best way to do this? # FIXME - is this really the best way to do this?
im = unicode(im.toUtf8(), "utf-8") im = unicode(im.toUtf8(), "utf-8")
if Image.isStringType(im): if isPath(im):
im = Image.open(im) im = Image.open(im)
if im.mode == "1": if im.mode == "1":

View File

@ -37,6 +37,7 @@ __version__ = "0.6"
import array, struct import array, struct
from PIL import Image, ImageFile, _binary from PIL import Image, ImageFile, _binary
from PIL.JpegPresets import presets from PIL.JpegPresets import presets
from PIL._util import isStringType
i8 = _binary.i8 i8 = _binary.i8
o8 = _binary.o8 o8 = _binary.o8
@ -488,7 +489,7 @@ def _save(im, fp, filename):
def validate_qtables(qtables): def validate_qtables(qtables):
if qtables is None: if qtables is None:
return qtables return qtables
if isinstance(qtables, basestring): if isStringType(qtables):
try: try:
lines = [int(num) for line in qtables.splitlines() lines = [int(num) for line in qtables.splitlines()
for num in line.split('#', 1)[0].split()] for num in line.split('#', 1)[0].split()]

View File

@ -41,6 +41,7 @@ from __future__ import print_function
import io import io
import sys import sys
from PIL import _binary from PIL import _binary
from PIL._util import isPath
if str is not bytes: if str is not bytes:
long = int long = int
@ -269,7 +270,7 @@ class OleFileIO:
def open(self, filename): def open(self, filename):
"""Open an OLE2 file""" """Open an OLE2 file"""
if isinstance(filename, str): if isPath(filename):
self.fp = open(filename, "rb") self.fp = open(filename, "rb")
else: else:
self.fp = filename self.fp = filename