示例#1
0
def WritePreview(list, filename, quality=90, shape=(100, 100), maxratio=4.):
    """Writes the tiled image. Shape is in PIL (width, height) order."""

    # Given shape is (samples,lines) or (width,height).

    # Define the shape of the output image in numpy order
    frameshape = (shape[1], shape[0])

    # Expand the scale to occupy as much as possible of the box
    (outshape, tiling) = BestTiling(len(list), list[0][0].shape, frameshape)

    # Make a new list of re-scaled, framed images
    frames = []
    for item in list:
        color = item[1]

        rgb = item[0]
        newrgb = ReshapeRGB(rgb, outshape, filename)

        if color == (0.5, 0.5, 0.5):  # Indicates an occultation. Kludge!
            newrgb = OverlayProfile(newrgb, profile=rgb, scale=0.5)

        framed = np.empty((outshape[0] + 2, outshape[1] + 2, 3), dtype="uint8")
        framed[:, :, :] = (np.array(color) * 255.999).astype("uint8")
        framed[1:-1, 1:-1, :] = newrgb[:, :, :]

        frames.append(framed)

    # Lay out the new tiled image
    if np.all(tiling == 1):
        result = frames[0]
    elif tiling[0] == 1:
        result = np.hstack(frames)
    elif tiling[1] == 1:
        result = np.vstack(frames)
    else:
        result = np.vstack((np.hstack(frames[0:2]), np.hstack(frames[2:4])))

    # Surround with black if necessary
    if result.shape[0] < frameshape[0] or result.shape[1] < frameshape[1]:
        black = np.zeros((frameshape[0], frameshape[1], 3), dtype="uint8")

        l0 = (frameshape[0] - result.shape[0]) / 2
        s0 = (frameshape[1] - result.shape[1]) / 2
        black[l0:l0 + result.shape[0], s0:s0 + result.shape[1]] = result[:, :]

    # Write it
    pil = picmaker.ArrayToPIL(result, rescale=False)
    picmaker.WritePIL(pil, filename, quality)
示例#2
0
def RGBtoPic(rgb, filename, quality=90, shape=None, expand=None):
    """Writes a single RGB array to a file. Re-shapes if necessary."""

    pil = picmaker.ArrayToPIL(rgb)

    if shape is None and expand is not None:
        shape = (rgb.shape[1] * expand, rgb.shape[0] * expand)

    if shape is not None:
        if shape[0] < rgb.shape[1] or shape[1] < rgb.shape[1]:
            filter = Image.ANTIALIAS
        else:
            filter = Image.NEAREST

        pil = pil.resize(shape, filter)

    picmaker.WritePIL(pil, filename, quality)