[Image-SIG] First cut at porting PIL to py3k

Jason R. Coombs jaraco at jaraco.com
Tue Jun 16 04:44:29 CEST 2009


On Mon, Dec 29, 2008 at 2:26 PM, Guilherme Polo <ggpolo at gmail.com> wrote:
> Hi there,
>
> So.. I ported PIL 1.1.6 to py3k today and I believe someone else
> around here might be interested on it. It is very likely to exist
> problems caused by the transition to bytes in py3k that I didn't fix
> yet, but all the tests pass. I also didn't fix display.c (since it is
> all Windows dependent) neither Sane/.
>

I've cloned Guilherme's repository and made a couple of fixes that enable
PIL to compile and install on Windows.  I'm unsure how to compile in JPEG
and PNG and font support, so I wasn't able to save any images, but this
patch works to create Image instances in Python 3.1rc2 on Windows.  This is
a patch against the repo at git://gpolo.ath.cx:4242/pil-py3k.git .

Regards,
Jason

diff --git a/PIL/Image.py b/PIL/Image.py
index 7c3cf7f..68d90e9 100644
--- a/PIL/Image.py
+++ b/PIL/Image.py
@@ -94,7 +94,15 @@ def isImageType(t):
 def isDirectory(f):
     return isStringType(f) and os.path.isdir(f)
 
-from operator import isNumberType, isSequenceType
+from numbers import Number
+
+def isNumberType(t):
+	return isinstance(t, Number)
+
+from collections import Sequence
+
+def isSequenceType(t):
+	return isinstance(t, Sequence)
 
 #
 # Debug level
diff --git a/display.c b/display.c
index 415e03c..f21ce7f 100644
--- a/display.c
+++ b/display.c
@@ -200,7 +200,7 @@ _tostring(ImagingDisplayObject* display, PyObject* args)
     if (!PyArg_ParseTuple(args, ":tostring"))
 	return NULL;
 
-    return PyString_FromStringAndSize(
+    return PyUnicode_FromStringAndSize(
         display->dib->bits, display->dib->ysize * display->dib->linesize
         );
 }
@@ -330,7 +330,7 @@ PyImaging_GrabScreenWin32(PyObject* self, PyObject*
args)
 
     /* step 3: extract bits from bitmap */
 
-    buffer = PyString_FromStringAndSize(NULL, height * ((width*3 + 3) &
-4));
+    buffer = PyUnicode_FromStringAndSize(NULL, height * ((width*3 + 3) &
-4));
     if (!buffer)
         return NULL;
 
@@ -339,7 +339,7 @@ PyImaging_GrabScreenWin32(PyObject* self, PyObject*
args)
     core.bcHeight = height;
     core.bcPlanes = 1;
     core.bcBitCount = 24;
-    if (!GetDIBits(screen_copy, bitmap, 0, height,
PyString_AS_STRING(buffer),
+    if (!GetDIBits(screen_copy, bitmap, 0, height,
PyUnicode_AS_UNICODE(buffer),
                    (BITMAPINFO*) &core, DIB_RGB_COLORS))
         goto error;
 
@@ -370,11 +370,11 @@ static BOOL CALLBACK list_windows_callback(HWND hwnd,
LPARAM lParam)
     /* get window title */
     title_size = GetWindowTextLength(hwnd);
     if (title_size > 0) {
-        title = PyString_FromStringAndSize(NULL, title_size);
+        title = PyUnicode_FromStringAndSize(NULL, title_size);
         if (title)
-            GetWindowText(hwnd, PyString_AS_STRING(title), title_size+1);
+            GetWindowText(hwnd, PyUnicode_AS_UNICODE(title), title_size+1);
     } else
-        title = PyString_FromString("");
+        title = PyUnicode_FromString("");
     if (!title)
         return 0;
 
@@ -528,7 +528,7 @@ PyImaging_GrabClipboardWin32(PyObject* self, PyObject*
args)
         size = wcslen(data) * 2;
 #endif
 
-    result = PyString_FromStringAndSize(data, size);
+    result = PyUnicode_FromStringAndSize(data, size);
 
     GlobalUnlock(handle);
 
diff --git a/map.c b/map.c
index d8249d5..97dc708 100644
--- a/map.c
+++ b/map.c
@@ -381,10 +381,10 @@ PyImaging_MapBuffer(PyObject* self, PyObject* args)
     /* setup file pointers */
     if (ystep > 0)
         for (y = 0; y < ysize; y++)
-            im->image[y] = ptr + offset + y * stride;
+            im->image[y] = (char*)ptr + offset + y * stride;
     else
         for (y = 0; y < ysize; y++)
-            im->image[ysize-y-1] = ptr + offset + y * stride;
+            im->image[ysize-y-1] = (char*)ptr + offset + y * stride;
 
     im->destroy = mapping_destroy_buffer;

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 6998 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/image-sig/attachments/20090615/9d47af29/attachment.bin>


More information about the Image-SIG mailing list