diff --git a/PIL/ImageFileIO.py b/PIL/ImageFileIO.py index c12a25738..e2f53e38a 100644 --- a/PIL/ImageFileIO.py +++ b/PIL/ImageFileIO.py @@ -12,7 +12,7 @@ # See the README file for information on usage and redistribution. # -from StringIO import StringIO +from io import BytesIO ## # The ImageFileIO module can be used to read an image from a @@ -23,7 +23,7 @@ from StringIO import StringIO # # @see ImageFile#Parser -class ImageFileIO(StringIO): +class ImageFileIO(BytesIO): ## # Adds buffering to a stream file object, in order to @@ -36,4 +36,4 @@ class ImageFileIO(StringIO): def __init__(self, fp): data = fp.read() - StringIO.__init__(self, data) + BytesIO.__init__(self, data) diff --git a/PIL/ImageFont.py b/PIL/ImageFont.py index 939887db2..0bb846fd3 100644 --- a/PIL/ImageFont.py +++ b/PIL/ImageFont.py @@ -255,12 +255,12 @@ def load_path(filename): def load_default(): "Load a default font." - from StringIO import StringIO + from io import BytesIO import base64 f = ImageFont() f._load_pilfont_data( # courB08 - StringIO(base64.decodestring(''' + BytesIO(base64.decodestring(b''' UElMZm9udAo7Ozs7OzsxMDsKREFUQQoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA @@ -352,7 +352,7 @@ AJsAEQAGAAAAAP/6AAX//wCbAAoAoAAPAAYAAAAA//oABQABAKAACgClABEABgAA////+AAGAAAA pQAKAKwAEgAGAAD////4AAYAAACsAAoAswASAAYAAP////gABgAAALMACgC6ABIABgAA////+QAG AAAAugAKAMEAEQAGAAD////4AAYAAgDBAAoAyAAUAAYAAP////kABQACAMgACgDOABMABgAA//// +QAGAAIAzgAKANUAEw== -''')), Image.open(StringIO(base64.decodestring(''' +''')), Image.open(BytesIO(base64.decodestring(b''' iVBORw0KGgoAAAANSUhEUgAAAx4AAAAUAQAAAAArMtZoAAAEwElEQVR4nABlAJr/AHVE4czCI/4u Mc4b7vuds/xzjz5/3/7u/n9vMe7vnfH/9++vPn/xyf5zhxzjt8GHw8+2d83u8x27199/nxuQ6Od9 M43/5z2I+9n9ZtmDBwMQECDRQw/eQIQohJXxpBCNVE6QCCAAAAD//wBlAJr/AgALyj1t/wINwq0g @@ -385,8 +385,8 @@ if __name__ == "__main__": font = "../Images/courB08" print(" f._load_pilfont_data(") print(" # %s" % os.path.basename(font)) - print(" StringIO(base64.decodestring('''") + print(" BytesIO(base64.decodestring(b'''") base64.encode(open(font + ".pil", "rb"), sys.stdout) - print("''')), Image.open(StringIO(base64.decodestring('''") + print("''')), Image.open(BytesIO(base64.decodestring(b'''") base64.encode(open(font + ".pbm", "rb"), sys.stdout) print("'''))))") diff --git a/PIL/ImageGrab.py b/PIL/ImageGrab.py index f3de6fde4..f938efa6f 100644 --- a/PIL/ImageGrab.py +++ b/PIL/ImageGrab.py @@ -67,6 +67,6 @@ def grabclipboard(): data = Image.core.grabclipboard(debug) if Image.isStringType(data): from . import BmpImagePlugin - import StringIO - return BmpImagePlugin.DibImageFile(StringIO.StringIO(data)) + import io + return BmpImagePlugin.DibImageFile(io.BytesIO(data)) return data diff --git a/PIL/ImageTk.py b/PIL/ImageTk.py index d4602b3d1..8f3b4c3e5 100644 --- a/PIL/ImageTk.py +++ b/PIL/ImageTk.py @@ -92,8 +92,8 @@ class PhotoImage: image = Image.open(kw["file"]) del kw["file"] elif "data" in kw: - from StringIO import StringIO - image = Image.open(StringIO(kw["data"])) + from io import BytesIO + image = Image.open(BytesIO(kw["data"])) del kw["data"] if hasattr(image, "mode") and hasattr(image, "size"): @@ -224,8 +224,8 @@ class BitmapImage: image = Image.open(kw["file"]) del kw["file"] elif "data" in kw: - from StringIO import StringIO - image = Image.open(StringIO(kw["data"])) + from io import BytesIO + image = Image.open(BytesIO(kw["data"])) del kw["data"] self.__mode = image.mode diff --git a/PIL/IptcImagePlugin.py b/PIL/IptcImagePlugin.py index c42df00ae..51e060a66 100644 --- a/PIL/IptcImagePlugin.py +++ b/PIL/IptcImagePlugin.py @@ -220,7 +220,7 @@ Image.register_extension("IPTC", ".iim") def getiptcinfo(im): from . import TiffImagePlugin, JpegImagePlugin - import StringIO + import io data = None @@ -279,7 +279,7 @@ def getiptcinfo(im): # parse the IPTC information chunk im.info = {} - im.fp = StringIO.StringIO(data) + im.fp = io.BytesIO(data) try: im._open() diff --git a/PIL/JpegImagePlugin.py b/PIL/JpegImagePlugin.py index bf1ef4a26..2a71544d9 100644 --- a/PIL/JpegImagePlugin.py +++ b/PIL/JpegImagePlugin.py @@ -362,7 +362,7 @@ class JpegImageFile(ImageFile.ImageFile): # and is likely to be replaced with something better in a future # version. from . import TiffImagePlugin - import StringIO + import io def fixup(value): if len(value) == 1: return value[0] @@ -373,7 +373,7 @@ class JpegImageFile(ImageFile.ImageFile): data = self.info["exif"] except KeyError: return None - file = StringIO.StringIO(data[6:]) + file = io.BytesIO(data[6:]) head = file.read(8) exif = {} # process dictionary diff --git a/PIL/OleFileIO.py b/PIL/OleFileIO.py index 5c1248f58..f8399eac4 100644 --- a/PIL/OleFileIO.py +++ b/PIL/OleFileIO.py @@ -38,7 +38,7 @@ from __future__ import print_function -import StringIO +import io import sys def i16(c, o = 0): @@ -81,7 +81,7 @@ WORD_CLSID = "00020900-0000-0000-C000-000000000046" # # -------------------------------------------------------------------- -class _OleStream(StringIO.StringIO): +class _OleStream(io.BytesIO): """OLE2 Stream @@ -107,11 +107,11 @@ class _OleStream(StringIO.StringIO): data.append(fp.read(sectorsize)) sect = fat[sect] - data = "".join(data) + data = b"".join(data) # print len(data), size - StringIO.StringIO.__init__(self, data[:size]) + io.BytesIO.__init__(self, data[:size]) # # -------------------------------------------------------------------- diff --git a/PIL/PdfImagePlugin.py b/PIL/PdfImagePlugin.py index 54b40b0f3..f54491871 100644 --- a/PIL/PdfImagePlugin.py +++ b/PIL/PdfImagePlugin.py @@ -23,7 +23,7 @@ __version__ = "0.4" from . import Image, ImageFile -import StringIO +import io # @@ -37,16 +37,16 @@ import StringIO # 5. page contents def _obj(fp, obj, **dict): - fp.write("%d 0 obj\n" % obj) + fp.write(b"%d 0 obj\n" % obj) if dict: - fp.write("<<\n") + fp.write(b"<<\n") for k, v in dict.items(): if v is not None: - fp.write("/%s %s\n" % (k, v)) - fp.write(">>\n") + fp.write(b"/%s %s\n" % (k, v)) + fp.write(b">>\n") def _endobj(fp): - fp.write("endobj\n") + fp.write(b"endobj\n") ## # (Internal) Image save plugin for the PDF format. @@ -60,8 +60,8 @@ def _save(im, fp, filename): xref = [0]*(5+1) # placeholders - fp.write("%PDF-1.2\n") - fp.write("% created by PIL PDF driver " + __version__ + "\n") + fp.write(b"%PDF-1.2\n") + fp.write(b"% created by PIL PDF driver " + __version__ + "\n") # # Get image characteristics @@ -76,34 +76,34 @@ def _save(im, fp, filename): params = None if im.mode == "1": - filter = "/ASCIIHexDecode" - colorspace = "/DeviceGray" - procset = "/ImageB" # grayscale + filter = b"/ASCIIHexDecode" + colorspace = b"/DeviceGray" + procset = b"/ImageB" # grayscale bits = 1 elif im.mode == "L": - filter = "/DCTDecode" + filter = b"/DCTDecode" # params = "<< /Predictor 15 /Columns %d >>" % (width-2) - colorspace = "/DeviceGray" - procset = "/ImageB" # grayscale + colorspace = b"/DeviceGray" + procset = b"/ImageB" # grayscale elif im.mode == "P": - filter = "/ASCIIHexDecode" - colorspace = "[ /Indexed /DeviceRGB 255 <" + filter = b"/ASCIIHexDecode" + colorspace = b"[ /Indexed /DeviceRGB 255 <" palette = im.im.getpalette("RGB") for i in range(256): r = ord(palette[i*3]) g = ord(palette[i*3+1]) b = ord(palette[i*3+2]) - colorspace = colorspace + "%02x%02x%02x " % (r, g, b) - colorspace = colorspace + "> ]" - procset = "/ImageI" # indexed color + colorspace = colorspace + b"%02x%02x%02x " % (r, g, b) + colorspace = colorspace + b"> ]" + procset = b"/ImageI" # indexed color elif im.mode == "RGB": - filter = "/DCTDecode" - colorspace = "/DeviceRGB" - procset = "/ImageC" # color images + filter = b"/DCTDecode" + colorspace = b"/DeviceRGB" + procset = b"/ImageC" # color images elif im.mode == "CMYK": - filter = "/DCTDecode" - colorspace = "/DeviceCMYK" - procset = "/ImageC" # color images + filter = b"/DCTDecode" + colorspace = b"/DeviceCMYK" + procset = b"/ImageC" # color images else: raise ValueError("cannot save mode %s" % im.mode) @@ -111,25 +111,25 @@ def _save(im, fp, filename): # catalogue xref[1] = fp.tell() - _obj(fp, 1, Type = "/Catalog", - Pages = "2 0 R") + _obj(fp, 1, Type = b"/Catalog", + Pages = b"2 0 R") _endobj(fp) # # pages xref[2] = fp.tell() - _obj(fp, 2, Type = "/Pages", + _obj(fp, 2, Type = b"/Pages", Count = 1, - Kids = "[4 0 R]") + Kids = b"[4 0 R]") _endobj(fp) # # image - op = StringIO.StringIO() + op = io.BytesIO() - if filter == "/ASCIIHexDecode": + if filter == b"/ASCIIHexDecode": if bits == 1: # FIXME: the hex encoder doesn't support packed 1-bit # images; do things the hard way... @@ -137,18 +137,18 @@ def _save(im, fp, filename): im = Image.new("L", (len(data), 1), None) im.putdata(data) ImageFile._save(im, op, [("hex", (0,0)+im.size, 0, im.mode)]) - elif filter == "/DCTDecode": + elif filter == b"/DCTDecode": ImageFile._save(im, op, [("jpeg", (0,0)+im.size, 0, im.mode)]) - elif filter == "/FlateDecode": + elif filter == b"/FlateDecode": ImageFile._save(im, op, [("zip", (0,0)+im.size, 0, im.mode)]) - elif filter == "/RunLengthDecode": + elif filter == b"/RunLengthDecode": ImageFile._save(im, op, [("packbits", (0,0)+im.size, 0, im.mode)]) else: raise ValueError("unsupported PDF filter (%s)" % filter) xref[3] = fp.tell() - _obj(fp, 3, Type = "/XObject", - Subtype = "/Image", + _obj(fp, 3, Type = b"/XObject", + Subtype = b"/Image", Width = width, # * 72.0 / resolution, Height = height, # * 72.0 / resolution, Length = len(op.getvalue()), @@ -157,9 +157,9 @@ def _save(im, fp, filename): DecodeParams = params, ColorSpace = colorspace) - fp.write("stream\n") + fp.write(b"stream\n") fp.write(op.getvalue()) - fp.write("\nendstream\n") + fp.write(b"\nendstream\n") _endobj(fp) @@ -168,37 +168,37 @@ def _save(im, fp, filename): xref[4] = fp.tell() _obj(fp, 4) - fp.write("<<\n/Type /Page\n/Parent 2 0 R\n"\ - "/Resources <<\n/ProcSet [ /PDF %s ]\n"\ - "/XObject << /image 3 0 R >>\n>>\n"\ - "/MediaBox [ 0 0 %d %d ]\n/Contents 5 0 R\n>>\n" %\ + fp.write(b"<<\n/Type /Page\n/Parent 2 0 R\n"\ + b"/Resources <<\n/ProcSet [ /PDF %s ]\n"\ + b"/XObject << /image 3 0 R >>\n>>\n"\ + b"/MediaBox [ 0 0 %d %d ]\n/Contents 5 0 R\n>>\n" %\ (procset, int(width * 72.0 /resolution) , int(height * 72.0 / resolution))) _endobj(fp) # # page contents - op = StringIO.StringIO() + op = io.BytesIO() - op.write("q %d 0 0 %d 0 0 cm /image Do Q\n" % (int(width * 72.0 / resolution), int(height * 72.0 / resolution))) + op.write(b"q %d 0 0 %d 0 0 cm /image Do Q\n" % (int(width * 72.0 / resolution), int(height * 72.0 / resolution))) xref[5] = fp.tell() _obj(fp, 5, Length = len(op.getvalue())) - fp.write("stream\n") + fp.write(b"stream\n") fp.write(op.getvalue()) - fp.write("\nendstream\n") + fp.write(b"\nendstream\n") _endobj(fp) # # trailer startxref = fp.tell() - fp.write("xref\n0 %d\n0000000000 65535 f \n" % len(xref)) + fp.write(b"xref\n0 %d\n0000000000 65535 f \n" % len(xref)) for x in xref[1:]: - fp.write("%010d 00000 n \n" % x) - fp.write("trailer\n<<\n/Size %d\n/Root 1 0 R\n>>\n" % len(xref)) - fp.write("startxref\n%d\n%%%%EOF\n" % startxref) + fp.write(b"%010d 00000 n \n" % x) + fp.write(b"trailer\n<<\n/Size %d\n/Root 1 0 R\n>>\n" % len(xref)) + fp.write(b"startxref\n%d\n%%%%EOF\n" % startxref) fp.flush() #