f0c487
diff --git a/PIL/Image.py b/PIL/Image.py
f0c487
index cafc5a2..794ee49 100644
f0c487
--- a/PIL/Image.py
f0c487
+++ b/PIL/Image.py
f0c487
@@ -718,7 +718,7 @@ class Image:
f0c487
         if self.mode == "L" and mode == "RGBA" and "transparency" in self.info:
f0c487
             from PIL import ImagePalette
f0c487
             self.mode = "P"
f0c487
-            bytePalette = bytes([i//3 for i in range(768)])
f0c487
+            bytePalette = bytes(bytearray([i//3 for i in range(768)]))
f0c487
             self.palette = ImagePalette.raw("RGB", bytePalette)
f0c487
             self.palette.dirty = 1
f0c487
             self.load()
f0c487
diff --git a/_imaging.c b/_imaging.c
f0c487
index 2ee7eef..6ec7346 100644
f0c487
--- a/_imaging.c
f0c487
+++ b/_imaging.c
f0c487
@@ -286,6 +286,7 @@ static const char* wrong_mode = "unrecognized image mode";
f0c487
 static const char* wrong_raw_mode = "unrecognized raw mode";
f0c487
 static const char* outside_image = "image index out of range";
f0c487
 static const char* outside_palette = "palette index out of range";
f0c487
+static const char* wrong_palette_size = "invalid palette size";
f0c487
 static const char* no_palette = "image has no palette";
f0c487
 static const char* readonly = "image is readonly";
f0c487
 /* static const char* no_content = "image has no content"; */
f0c487
@@ -1412,6 +1413,11 @@ _putpalette(ImagingObject* self, PyObject* args)
f0c487
     return NULL;
f0c487
     }
f0c487
 
f0c487
+    if ( palettesize * 8 / bits > 256) {
f0c487
+    PyErr_SetString(PyExc_ValueError, wrong_palette_size);
f0c487
+    return NULL;
f0c487
+    }
f0c487
+
f0c487
     ImagingPaletteDelete(self->image->palette);
f0c487
 
f0c487
     strcpy(self->image->mode, "P");