本文整理汇总了Python中nnmware.core.file.get_path_from_url函数的典型用法代码示例。如果您正苦于以下问题:Python get_path_from_url函数的具体用法?Python get_path_from_url怎么用?Python get_path_from_url使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_path_from_url函数的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: make_watermark
def make_watermark(photo_url, align='lt', root=settings.MEDIA_ROOT, url_root=settings.MEDIA_URL):
""" create watermark """
photo_path = get_path_from_url(photo_url, root, url_root)
watermark_path = settings.WATERMARK
wm_url = _get_thumbnail_path(photo_url, watermark=1)
wm_path = get_path_from_url(wm_url, root, url_root)
if _has_thumbnail(photo_url, watermark=1):
# thumbnail already exists
if not (os.path.getmtime(photo_path) > os.path.getmtime(wm_path)) and \
not (os.path.getmtime(watermark_path) > os.path.getmtime(wm_path)):
# if photo mtime is newer than thumbnail recreate thumbnail
return wm_url
try:
base_im = Image.open(photo_path)
logo_im = Image.open(watermark_path) # transparent image
except IOError:
return None
if align == 'center':
base_im.paste(logo_im, ((base_im.size[0] - logo_im.size[0]) / 2, (base_im.size[1] - logo_im.size[1]) / 2),
logo_im)
else:
base_im.paste(logo_im, (base_im.size[0] - logo_im.size[0], base_im.size[1] - logo_im.size[1]), logo_im)
base_im.convert('RGB')
base_im.save(wm_path, "JPEG")
return wm_url
示例2: make_thumbnail
def make_thumbnail(photo_url, width=None, height=None, aspect=None,
root=settings.MEDIA_ROOT, url_root=settings.MEDIA_URL):
""" create thumbnail """
# one of width/height is required
assert (width is not None) or (height is not None)
if not photo_url:
return None
th_url = _get_thumbnail_path(photo_url, width, height, aspect)
th_path = get_path_from_url(th_url, root, url_root)
photo_path = get_path_from_url(photo_url, root, url_root)
if _has_thumbnail(photo_url, width, height, root, url_root, aspect):
# thumbnail already exists
if not (os.path.getmtime(photo_path) > os.path.getmtime(th_path)):
# if photo mtime is newer than thumbnail recreate thumbnail
return th_url
# make thumbnail
# get original image size
orig_w, orig_h = get_image_size(photo_url, root, url_root)
if (orig_w is None) and (orig_h is None):
# something is wrong with image
return photo_url
# make proper size
if (width is not None) and (height is not None):
if (orig_w == width) and (orig_h == height):
# same dimensions
return None
size = (width, height)
elif width is not None:
if orig_w == width:
# same dimensions
return None
size = (width, orig_h)
elif height is not None:
if orig_h == height:
# same dimensions
return None
size = (orig_w, height)
try:
img = Image.open(photo_path).copy()
if aspect:
img = ImageOps.fit(img, size, Image.ANTIALIAS, (0.5, 0.5))
img.thumbnail(size, Image.ANTIALIAS)
img.save(th_path, quality=settings.THUMBNAIL_QUALITY)
except Exception, err:
# this goes to webserver error log
print >> sys.stderr, '[MAKE THUMBNAIL] error %s for file %r' % (err, photo_url)
return photo_url
示例3: _has_thumbnail
def _has_thumbnail(photo_url, width=None, height=None,
root=settings.MEDIA_ROOT, url_root=settings.MEDIA_URL, aspect=None, watermark=None):
# one of width/height is required
assert (width is not None) or (height is not None) or (watermark is not None)
return os.path.isfile(get_path_from_url(_get_thumbnail_path(photo_url,
width, height,aspect,watermark), root, url_root))
示例4: remove_thumbnails
def remove_thumbnails(pic_url, root=settings.MEDIA_ROOT, url_root=settings.MEDIA_URL):
if not pic_url:
return # empty url
file_name = get_path_from_url(pic_url, root, url_root)
base, ext = os.path.splitext(os.path.basename(file_name))
basedir = os.path.dirname(file_name)
for file in fnmatch.filter(os.listdir(str(basedir)), _THUMBNAIL_GLOB % (base, ext)):
path = os.path.join(basedir, file)
try:
os.remove(path)
except OSError:
pass
for file in fnmatch.filter(os.listdir(str(basedir)), _THUMBNAIL_ASPECT % (base, ext)):
path = os.path.join(basedir, file)
try:
os.remove(path)
except OSError:
pass
for file in fnmatch.filter(os.listdir(str(basedir)), _WATERMARK % (base, ext)):
path = os.path.join(basedir, file)
try:
os.remove(path)
except OSError:
pass
示例5: remove_file
def remove_file(f_url, root=settings.MEDIA_ROOT, url_root=settings.MEDIA_URL):
if not f_url:
return # empty url
file_name = get_path_from_url(f_url, root, url_root)
try:
os.remove(file_name)
except:
print >> sys.stderr, "Could not delete file: %s" % file_name
示例6: remove_file
def remove_file(f_url, root=settings.MEDIA_ROOT, url_root=settings.MEDIA_URL):
if not f_url:
return # empty url
file_name = get_path_from_url(f_url, root, url_root)
try:
os.remove(file_name)
except:
pass
示例7: get_image_size
def get_image_size(photo_url, root=settings.MEDIA_ROOT, url_root=settings.MEDIA_URL):
""" returns image size.
"""
path = get_path_from_url(photo_url, root, url_root)
try:
size = Image.open(path).size
except :
# this goes to webserver error log
return None, None
return size
示例8: get_image_size
def get_image_size(photo_url, root=settings.MEDIA_ROOT, url_root=settings.MEDIA_URL):
""" returns image size.
"""
path = get_path_from_url(photo_url, root, url_root)
try:
size = Image.open(path).size
except Exception, err:
# this goes to webserver error log
import sys
print >> sys.stderr, '[GET IMAGE SIZE] error %s for file %r' % (err, photo_url)
return None, None
示例9: save
def save(self, *args, **kwargs):
pics = Pic.objects.for_object(self.content_object)
if self.pk:
pics = pics.exclude(pk=self.pk)
if IMG_MAX_PER_OBJECT > 1:
if self.primary:
pics = pics.filter(primary=True)
pics.update(primary=False)
else:
pics.delete()
try:
remove_thumbnails(self.pic.path)
except:
pass
fullpath = get_path_from_url(self.pic.url)
self.size = os.path.getsize(fullpath)
super(Pic, self).save(*args, **kwargs)
示例10: remove_thumbnails
def remove_thumbnails(pic_url, root=settings.MEDIA_ROOT, url_root=settings.MEDIA_URL):
if not pic_url:
return # empty url
file_name = get_path_from_url(pic_url, root, url_root)
base, ext = os.path.splitext(os.path.basename(file_name))
basedir = os.path.dirname(file_name)
for item in TMB_MASKS:
try:
for f in fnmatch.filter(os.listdir(str(basedir)), item % (base, ext)):
path = os.path.join(basedir, f)
try:
os.remove(path)
except OSError:
pass
except:
pass
示例11: img_rotate
def img_rotate(request):
# Rotate image
try:
if not request.user.is_superuser:
raise AccessError
img_pk = request.POST['crop_id']
pic = get_object_or_404(Pic, id=int(img_pk))
img = get_path_from_url(pic.pic.url)
im = Image.open(img)
im = im.rotate(90)
if im.mode not in ('L', 'RGB'):
im = im.convert('RGB')
im.save(img)
pic.save()
payload = {'success': True, 'id': pic.pk}
except AccessError:
payload = {'success': False, 'error': _('You are not allowed rotate this image')}
except:
payload = {'success': False}
return ajax_answer_lazy(payload)
示例12: get_thumbnail_path
def get_thumbnail_path(url,size):
url_t = make_thumbnail(url, width=size)
return get_path_from_url(url_t)
示例13: resize_image
def resize_image(img_url, width=400, height=400, root=settings.MEDIA_ROOT, url_root=settings.MEDIA_URL):
file_name = get_path_from_url(img_url, root, url_root)
im = Image.open(file_name)
if im.size[0] > width or im.size[1] > height:
im.thumbnail((width, height), Image.ANTIALIAS )
im.save(file_name, "JPEG", quality=88 )