Skip to content

Commit

Permalink
Apply @Arfrever patch, fixes #258
Browse files Browse the repository at this point in the history
  • Loading branch information
aclark4life committed Jun 30, 2013
1 parent 54cb132 commit fd29e70
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 37 deletions.
3 changes: 2 additions & 1 deletion PIL/GdImageFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
__version__ = "0.1"

from PIL import ImageFile, ImagePalette, _binary
from PIL._util import isPath

try:
import builtins
Expand Down Expand Up @@ -77,7 +78,7 @@ def open(fp, mode = "r"):
if mode != "r":
raise ValueError("bad mode")

if isinstance(fp, str):
if isPath(fp):
filename = fp
fp = builtins.open(fp, "rb")
else:
Expand Down
23 changes: 5 additions & 18 deletions PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,33 +81,20 @@ def __getattr__(self, id):

from PIL import ImageMode
from PIL._binary import i8, o8
from PIL._util import isPath, isStringType

import os, sys

# type stuff
import collections
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.

def isImageType(t):
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

Expand Down Expand Up @@ -1421,10 +1408,10 @@ def transform(x, y, matrix=matrix):
def save(self, fp, format=None, **params):
"Save image to file or stream"

if isStringType(fp):
if isPath(fp):
filename = fp
else:
if hasattr(fp, "name") and isStringType(fp.name):
if hasattr(fp, "name") and isPath(fp.name):
filename = fp.name
else:
filename = ""
Expand Down Expand Up @@ -1455,7 +1442,7 @@ def save(self, fp, format=None, **params):
init()
save_handler = SAVE[format.upper()] # unknown format

if isStringType(fp):
if isPath(fp):
fp = builtins.open(fp, "wb")
close = 1
else:
Expand Down Expand Up @@ -1984,7 +1971,7 @@ def open(fp, mode="r"):
if mode != "r":
raise ValueError("bad mode")

if isStringType(fp):
if isPath(fp):
filename = fp
fp = builtins.open(fp, "rb")
else:
Expand Down
3 changes: 2 additions & 1 deletion PIL/ImageCms.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@

from PIL import Image
from PIL import _imagingcms
from PIL._util import isStringType

core = _imagingcms

Expand Down Expand Up @@ -139,7 +140,7 @@ class ImageCmsProfile:
def __init__(self, profile):
# accepts a string (filename), a file-like object, or a low-level
# profile object
if Image.isStringType(profile):
if isStringType(profile):
self._set(core.profile_open(profile), profile)
elif hasattr(profile, "read"):
self._set(core.profile_frombytes(profile.read()))
Expand Down
7 changes: 4 additions & 3 deletions PIL/ImageDraw.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import numbers

from PIL import Image, ImageColor
from PIL._util import isStringType

try:
import warnings
Expand Down Expand Up @@ -98,7 +99,7 @@ def setink(self, ink):
"'setink' is deprecated; use keyword arguments instead",
DeprecationWarning, stacklevel=2
)
if Image.isStringType(ink):
if isStringType(ink):
ink = ImageColor.getcolor(ink, self.mode)
if self.palette and not isinstance(ink, numbers.Number):
ink = self.palette.getcolor(ink)
Expand Down Expand Up @@ -141,13 +142,13 @@ def _getink(self, ink, fill=None):
ink = self.ink
else:
if ink is not None:
if Image.isStringType(ink):
if isStringType(ink):
ink = ImageColor.getcolor(ink, self.mode)
if self.palette and not isinstance(ink, numbers.Number):
ink = self.palette.getcolor(ink)
ink = self.draw.draw_ink(ink, self.mode)
if fill is not None:
if Image.isStringType(fill):
if isStringType(fill):
fill = ImageColor.getcolor(fill, self.mode)
if self.palette and not isinstance(fill, numbers.Number):
fill = self.palette.getcolor(fill)
Expand Down
3 changes: 2 additions & 1 deletion PIL/ImageFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#

from PIL import Image
from PIL._util import isPath
import traceback, os
import io

Expand Down Expand Up @@ -81,7 +82,7 @@ def __init__(self, fp=None, filename=None):
self.decoderconfig = ()
self.decodermaxblock = MAXBLOCK

if Image.isStringType(fp):
if isPath(fp):
# filename
self.fp = open(fp, "rb")
self.filename = fp
Expand Down
17 changes: 8 additions & 9 deletions PIL/ImageFont.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from __future__ import print_function

from PIL import Image
from PIL._util import isDirectory, isPath
import os, sys

try:
Expand All @@ -45,13 +46,6 @@ def __getattr__(self, id):
except ImportError:
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)

# --------------------------------------------------------------------
Expand Down Expand Up @@ -148,7 +142,7 @@ def __init__(self, font=None, size=10, index=0, encoding="", file=None):
warnings.warn('file parameter deprecated, please use font parameter instead.', DeprecationWarning)
font = file

if isStringType(font):
if isPath(font):
self.font = core.getfont(font, size, index, encoding)
else:
self.font_bytes = font.read()
Expand Down Expand Up @@ -266,7 +260,12 @@ def truetype(font=None, size=10, index=0, encoding="", filename=None):
def load_path(filename):
"Load a font file, searching along the Python 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:
return load(os.path.join(dir, filename))
except IOError:
Expand Down
3 changes: 2 additions & 1 deletion PIL/ImageOps.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#

from PIL import Image
from PIL._util import isStringType
import operator
from functools import reduce

Expand All @@ -43,7 +44,7 @@ def _border(border):
return left, top, right, bottom

def _color(color, mode):
if Image.isStringType(color):
if isStringType(color):
from PIL import ImageColor
color = ImageColor.getcolor(color, mode)
return color
Expand Down
3 changes: 2 additions & 1 deletion PIL/ImageQt.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#

from PIL import Image
from PIL._util import isPath

from PyQt4.QtGui import QImage, qRgb

Expand Down Expand Up @@ -45,7 +46,7 @@ def __init__(self, im):
if hasattr(im, "toUtf8"):
# FIXME - is this really the best way to do this?
im = unicode(im.toUtf8(), "utf-8")
if Image.isStringType(im):
if isPath(im):
im = Image.open(im)

if im.mode == "1":
Expand Down
3 changes: 2 additions & 1 deletion PIL/JpegImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import array, struct
from PIL import Image, ImageFile, _binary
from PIL.JpegPresets import presets
from PIL._util import isStringType

i8 = _binary.i8
o8 = _binary.o8
Expand Down Expand Up @@ -488,7 +489,7 @@ def _save(im, fp, filename):
def validate_qtables(qtables):
if qtables is None:
return qtables
if isinstance(qtables, basestring):
if isStringType(qtables):
try:
lines = [int(num) for line in qtables.splitlines()
for num in line.split('#', 1)[0].split()]
Expand Down
3 changes: 2 additions & 1 deletion PIL/OleFileIO.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import io
import sys
from PIL import _binary
from PIL._util import isPath

if str is not bytes:
long = int
Expand Down Expand Up @@ -269,7 +270,7 @@ def __init__(self, filename = None):
def open(self, filename):
"""Open an OLE2 file"""

if isinstance(filename, str):
if isPath(filename):
self.fp = open(filename, "rb")
else:
self.fp = filename
Expand Down

0 comments on commit fd29e70

Please sign in to comment.