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 as ioerr: 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
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
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: return photo_url return th_url
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
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))
def ajax_image_crop(request): # Crop image # noinspection PyBroadException try: img_pk = request.POST['crop_id'] pic = get_object_or_404(Pic, id=int(img_pk)) if not request.user.is_superuser: parent_object = pic.content_object.__class__.__name__ if parent_object == 'Room': if request.user not in pic.content_object.hotel.admins.all(): raise AccessError elif parent_object == 'Hotel': if request.user not in pic.content_object.admins.all(): raise AccessError left = int(request.POST['crop_x1']) right = int(request.POST['crop_x2']) top = int(request.POST['crop_y1']) bottom = int(request.POST['crop_y2']) box = [left, top, right, bottom] img = get_path_from_url(pic.pic.url) im = Image.open(img) if im.size[0] > MAX_IMAGE_CROP_WIDTH: aspect_c = float(im.size[0]) / MAX_IMAGE_CROP_WIDTH box = map(lambda x: int(x * aspect_c), box) im = im.crop(box) if im.mode not in ('L', 'RGB'): im = im.convert('RGB') im.save(img) pic.save() payload = {'success': True, 'id': pic.pk} except AccessError as aerr: payload = {'success': False, 'error': _('You are not allowed change this image')} except: payload = {'success': False} return ajax_answer_lazy(payload)
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))
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
def ajax_image_crop(request): # Crop image try: img_pk = request.POST["crop_id"] pic = get_object_or_404(Pic, id=int(img_pk)) if not request.user.is_superuser: parent_object = pic.content_object.__class__.__name__ if parent_object == "Room": if not request.user in pic.content_object.hotel.admins.all(): raise AccessError elif parent_object == "Hotel": if not request.user in pic.content_object.admins.all(): raise AccessError left = int(request.POST["crop_x1"]) right = int(request.POST["crop_x2"]) top = int(request.POST["crop_y1"]) bottom = int(request.POST["crop_y2"]) box = [left, top, right, bottom] img = get_path_from_url(pic.pic.url) im = Image.open(img) if im.size[0] > settings.MAX_IMAGE_CROP_WIDTH: aspect_c = float(im.size[0]) / settings.MAX_IMAGE_CROP_WIDTH box = map(lambda x: int(x * aspect_c), box) im = im.crop(box) 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 change this image")} except: payload = {"success": False} return AjaxLazyAnswer(payload)
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
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
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
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)
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) # noinspection PyBroadException try: size = Image.open(path).size except: # this goes to webserver error log return None, None return 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
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: # noinspection PyBroadException 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 as oserr: pass except: pass
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)
def save(self, *args, **kwargs): pics = Pic.objects.for_object(self.content_object) if self.pk: pics = pics.exclude(pk=self.pk) if settings.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)
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
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 AjaxLazyAnswer(payload)
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)
def img_rotate(request): # Rotate image # noinspection PyBroadException 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.img.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 as aerr: payload = dict(success=False, error=_('You are not allowed rotate this image')) except: payload = dict(success=False) return ajax_answer_lazy(payload)
def ajax_image_crop(request): # Crop image # noinspection PyBroadException try: img_pk = request.POST['crop_id'] pic = get_object_or_404(Pic, id=int(img_pk)) if not request.user.is_superuser: parent_object = pic.content_object.__class__.__name__ if parent_object == 'Room': if request.user not in pic.content_object.hotel.admins.all(): raise AccessError elif parent_object == 'Hotel': if request.user not in pic.content_object.admins.all(): raise AccessError left = int(request.POST['crop_x1']) right = int(request.POST['crop_x2']) top = int(request.POST['crop_y1']) bottom = int(request.POST['crop_y2']) box = [left, top, right, bottom] img = get_path_from_url(pic.pic.url) im = Image.open(img) if im.size[0] > MAX_IMAGE_CROP_WIDTH: aspect_c = float(im.size[0]) / MAX_IMAGE_CROP_WIDTH box = map(lambda x: int(x * aspect_c), box) im = im.crop(box) if im.mode not in ('L', 'RGB'): im = im.convert('RGB') im.save(img) pic.save() payload = {'success': True, 'id': pic.pk} except AccessError as aerr: payload = { 'success': False, 'error': _('You are not allowed change this image') } except: payload = {'success': False} return ajax_answer_lazy(payload)
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 AjaxLazyAnswer(payload)
def get_thumbnail_path(url, size): url_t = make_thumbnail(url, width=size) return get_path_from_url(url_t)
def get_thumbnail_path(url,size): url_t = make_thumbnail(url, width=size) return get_path_from_url(url_t)
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 )