Beispiel #1
0
def MosaicImageList(list_images, dims, image_name=None):
    from PIL import Image
    """It creates a dims[0] x dims[1] mosaic with the images on list_images"""
    images = [Image.open(image) for image in list_images]
    widths, heights = zip(*(i.size for i in images))

    max_width = max(widths)
    max_height = max(heights)
    new_im = Image.new('RGB',
                       ((max_width + 1) * dims[1], (max_height + 1) * dims[0]))

    for i in range(0, dims[0]):
        for j in range(0, dims[1]):
            indice = i * dims[1] + j
            if indice < len(images):
                im = images[indice]
                new_im.paste(im, (max_width * j, max_height * i))
                im.close()

    if image_name is None:
        image_name = createTempFile(params={"format": "jpg"}, prefix="mosaic_")

    new_im.save(image_name)
    new_im.close()
    return image_name
Beispiel #2
0
def MaybeDownloadThumb(image,
                       params={"format": "jpg"},
                       image_name=None,
                       path=os.getcwd(),
                       force=False,
                       footprint=None):
    """
    Download thumb on path folder.

    :param image: image to download the thumb
    :param params: params to getThumbUrl()
    :param image_name: name of the image to download. Create temp file if None
    :param path:
    :param force: (optional) overwrite image_name if exists
    :param footprint: string to print into the image
    :return: Returns the downloaded file full path
    """
    params = dict(params)
    filecreated = False
    if image_name is None:
        image_name = createTempFile(params, "thumb_", path)
        filecreated = True

    if not force and not filecreated and os.path.exists(image_name):
        return image_name

    if not filecreated and os.path.exists(image_name):
        os.remove(image_name)

    url = image.getThumbUrl(params)

    r_link = requests.get(url, stream=True)
    if r_link.status_code == 200:
        with open(image_name, 'wb') as f:
            r_link.raw.decode_content = True
            shutil.copyfileobj(r_link.raw, f)
    else:
        raise Exception("Can't download Status: %d\n %s" %
                        (r_link.status_code, r_link.text))

    if os.stat(image_name).st_size < 100:
        file_content = open(image_name).readline()
        raise Exception(
            "File downloaded '{}' is annomally small. first line: '{}'".format(
                image_name, file_content))

    if footprint is not None:
        from PIL import Image
        from PIL import ImageDraw
        img = Image.open(image_name)
        draw = ImageDraw.Draw(img)
        draw.text((0, 0), footprint, (255, 255, 255))
        img.save(image_name)

    return image_name
Beispiel #3
0
def eeFeatureCollectionToPandas(feature_col, properties=None, with_task=False,
                                filename=None, mounted_drive=False):
    """
    Converts ee.FeatureCollection server obj to pandas.DataFrame local obj.

    :param feature_col: feature collection to export
    :type feature_col: ee.FeatureCollection
    :param properties: (optional) list of columns to export
    :param with_task: (default false). If download is done throw ee.batch.Export.table
    :param filename: (optional) if None csv downloaded will be removed.
    :param mounted_drive: if drive is mounted we don't need to use pyDrive
    If present nothing will be downloaded

    :return: pandas.DataFrame object
    """
    # Features is a list of dict with the output
    remove_file = not mounted_drive
    filetype = "csv"
    if filename is None:
        filename = file_utils.createTempFile(params={"format": filetype},
                                             prefix="pandas_ftcol")
    else:
        filename_full = file_utils.addFormat(filename, filetype)
        if os.path.isfile(filename_full):
            return _readCSV(filename_full)

        remove_file = False

    prefix = file_utils.removeFormat(os.path.basename(filename), "csv")

    if with_task:
        from ee_ipl_uv.download import WaitAndDownload, WaitTask
        if properties is not None:
            feature_col = feature_col.select(properties)

        tarea = ee.batch.Export.table.toDrive(feature_col,
                                              prefix,
                                              folder="ee_ipl_uv_downloads",
                                              fileFormat=filetype)
        tarea.start()
        if mounted_drive:
            WaitTask(tarea)
            filename = os.path.join("/content/drive/My Drive/ee_ipl_uv_downloads/", prefix+".csv")
            if not os.path.exists(filename):
                logger.info("File %s not ready in drive. Waiting 30 seconds" % filename)
                time.sleep(30)
            assert os.path.exists(filename), "%s does not exists in the drive" % filename
        else:
            filename = WaitAndDownload(tarea, prefix,
                                       formato="csv", force=True)
    else:
        if properties is None:
            url = feature_col.getDownloadURL(filetype=filetype)
        else:
            properties_list = properties.getInfo()
            url = feature_col.getDownloadURL(filetype=filetype,
                                             selectors=properties_list)

        logger.debug("Downloading data from: " + url)

        r_link = requests.get(url, stream=True)
        if r_link.status_code == 200:
            with open(filename, 'wb') as f:
                r_link.raw.decode_content = True
                shutil.copyfileobj(r_link.raw, f)

    logger.debug("File downloaded, reading csv: " +filename)
    datos = _readCSV(filename)

    if remove_file:
        os.remove(filename)

    return datos