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