Exemple #1
0
def image_from_url(url):
    upload_dir = settings.THUMBNAIL_DIR
    img_file = urllib2.urlopen(url)
    im = StringIO(img_file.read())
    image = Image.open(im)
    if image.mode not in ('L', 'RGB'):
        image = image.convert('RGB')
    image = fit(image, 300)
    image = aspect_ratio(image, 16, 9)
    timedir = get_date_directory()
    filename = urlparse(url).path.split('/')[-1]
    ext = os.path.splitext(filename)[1]
    new_filename = md5(filename.encode('utf8')).hexdigest() + ext
    path = os.path.join(settings.MEDIA_ROOT, upload_dir, timedir, new_filename)
    try:
        os.makedirs(os.path.realpath(os.path.dirname(path)))
    except:
        pass
    image.save(path, 'jpeg')
    return upload_dir + "/" + timedir + "/" + new_filename
Exemple #2
0
def image_from_url(url):
    upload_dir = settings.THUMBNAIL_DIR
    img_file = urllib2.urlopen(url)
    im = StringIO(img_file.read())
    image = Image.open(im)
    if image.mode not in ('L', 'RGB'):
        image = image.convert('RGB')
    image = fit(image, 300)
    image = aspect_ratio(image, 16, 9)
    timedir = get_date_directory()
    filename = urlparse(url).path.split('/')[-1]
    ext = os.path.splitext(filename)[1]
    new_filename = md5(filename.encode('utf8')).hexdigest() + ext
    path = os.path.join(settings.MEDIA_ROOT, upload_dir, timedir, new_filename)
    try:
        os.makedirs(os.path.realpath(os.path.dirname(path)))
    except:
        pass
    image.save(path, 'jpeg')
    return upload_dir + "/" + timedir + "/" + new_filename
Exemple #3
0
def image_from_url(url):
    upload_dir = setting("THUMBNAIL_DIR", "thumbnails")
    img_file = urlopen(url)
    im = StringIO(img_file.read())
    image = Image.open(im)
    if image.mode not in ("L", "RGB"):
        image = image.convert("RGB")
    image = fit(image, 300)
    image = aspect_ratio(image, 16, 9)
    timedir = get_date_directory()
    filename = urlparse(url).path.split("/")[-1]
    ext = os.path.splitext(filename)[1]
    new_filename = md5(filename.encode("utf8")).hexdigest() + ext
    path = os.path.join(settings.MEDIA_ROOT, upload_dir, timedir, new_filename)
    # noinspection PyBroadException
    try:
        os.makedirs(os.path.realpath(os.path.dirname(path)))
    except:
        pass
    image.save(path, "jpeg")
    return upload_dir + "/" + timedir + "/" + new_filename