<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def convert(ctype, img, palette_img, dither=False):
 """Convert an image to palette type. Parameters ctype : `int` Conversion type. img : `PIL.Image` Image to convert. palette_img : `PIL.Image` Palette source image. dither : `bool`, optional Enable dithering (default: `False`). Raises ------ ValueError If palette_img has no palette. Returns ------- `PIL.Image` Converted image. """

    if ctype == 0:
        img2 = img.convert(mode='P')
        img2.putpalette(palette_img.getpalette())
        return img2

    img.load()
    palette_img.load()
    if palette_img.palette is None:
        raise ValueError('invalid palette image')
    im = img.im.convert('P', int(dither), palette_img.im)
    return img._new(im)