Beispiel #1
0
    def rotate(self, anticlockwise_angle = 90):
        '''
        Rotates the image and all thumbnails
        '''

        thumbnail = self._rotate_image(self.thumbnail, anticlockwise_angle)
        thumbnail_path_and_filename = upload_to(self, str(create_hash(str(self.original_image)) + '.jpg'))
        thumbnail.save(settings.MEDIA_ROOT + str(thumbnail_path_and_filename), "JPEG", quality=95)

        large_thumbnail = self._rotate_image(self.large_thumbnail, anticlockwise_angle)
        large_thumbnail_path_and_filename = upload_to(self, str(create_hash(str(self.original_image)) + '.jpg'))
        large_thumbnail.save(settings.MEDIA_ROOT + str(large_thumbnail_path_and_filename), "JPEG", quality=95)

        original_image = self._rotate_image(self.original_image, anticlockwise_angle)
        original_image_path_and_filename = upload_to(self, str(create_hash(str(self.original_image)) + '.jpg'))
        original_image.save(settings.MEDIA_ROOT + str(original_image_path_and_filename), "JPEG", quality=95)

        self.delete_local_image_files()
        self.delete_remote_image_files()

        self.thumbnail = thumbnail_path_and_filename
        self.large_thumbnail = large_thumbnail_path_and_filename
        self.original_image = original_image_path_and_filename

        self.save()
        self.upload_files_to_s3()
        self.delete_local_image_files()
Beispiel #2
0
    def crop_and_resize_photo(self, x, y, w, h):
        '''
        Crops the photo and produces a large and small thumbnail
        '''

        path_and_filename = ''.join([settings.MEDIA_ROOT, str(self.photo)])
        im = Image.open(path_and_filename)

        width, height=im.size

        x = int(x)
        y = int(y)
        w = int(w)
        h = int(h)

        small_thumb_name = ''.join([create_hash(self.name), 'small_thumb', '.jpg'])
        large_thumb_name = ''.join([create_hash(self.name), 'large_thumb', '.jpg'])

        small_thumb = im.copy()
        small_thumb.crop((x, y, x + w, y + h)
            ).resize((80,80), Image.ANTIALIAS
            ).save(''.join([settings.MEDIA_ROOT, 'profile_photos/', small_thumb_name]), "JPEG", quality=75)
        self.small_thumbnail = 'profile_photos/' + small_thumb_name

        large_thumb = im.copy()
        large_thumb.crop((x, y, x + w, y + h)
            ).resize((200,200), Image.ANTIALIAS
            ).save(''.join([settings.MEDIA_ROOT, 'profile_photos/', large_thumb_name]), "JPEG", quality=75)

        self.large_thumbnail = 'profile_photos/' + large_thumb_name

        upload_file_to_s3(self.small_thumbnail)
        upload_file_to_s3(self.large_thumbnail)
    def crop_and_resize_photo(self, x, y, w, h, display_height):
        '''
        Crops the photo and produces a large and small thumbnail
        '''

        path_and_filename = ''.join([settings.MEDIA_ROOT, str(self.photo)])
        im = Image.open(path_and_filename)

        width, height=im.size
        ratio = height / display_height

        #Prevent picture becoming too big during crop
        if ratio > 6:
            raise Exception(tran("Invalid image!"))

        x = int(x * ratio)
        y = int(y * ratio)
        w = int(w * ratio)
        h = int(h * ratio)

        small_thumb_name = ''.join([create_hash(self.name), 'small_thumb', '.jpg'])
        large_thumb_name = ''.join([create_hash(self.name), 'large_thumb', '.jpg'])

        small_thumb = im.copy()
        small_thumb.crop((x, y, x + w, y + h)
            ).resize((80,80), Image.ANTIALIAS
            ).save(''.join([settings.MEDIA_ROOT, 'profile_photos/', small_thumb_name]), "JPEG", quality=75)
        self.small_thumbnail = 'profile_photos/' + small_thumb_name

        large_thumb = im.copy()
        large_thumb.crop((x, y, x + w, y + h)
            ).resize((200,200), Image.ANTIALIAS
            ).save(''.join([settings.MEDIA_ROOT, 'profile_photos/', large_thumb_name]), "JPEG", quality=75)
        self.large_thumbnail = 'profile_photos/' + large_thumb_name
Beispiel #4
0
    def create(self, request):
        '''
        Image upload
        '''
        queryset = Gallery.objects.filter(family_id=request.user.family_id)
        gallery_id, gallery_id_valid = intTryParse(
            request.data.get("gallery_id"))

        if not gallery_id_valid:
            raise ParseError('Invalid gallery_id')

        # Check gallery is part of family
        gallery = get_object_or_404(queryset, pk=gallery_id)

        try:
            uploaded = request.FILES['picture']
            name, ext = os.path.splitext(uploaded.name)

            if uploaded.size > MAX_FILE_SIZE:
                raise ParseError('File too big')

            filename = create_hash(uploaded.name) + '.jpg'
            image = Image(gallery_id=gallery.id,
                          family_id=gallery.family_id,
                          title=name,
                          uploaded_by=request.user)

            path = upload_to(image, filename)

            #Write the file to the destination
            destination = open(os.path.join(settings.MEDIA_ROOT, path), 'wb+')

            for chunk in uploaded.chunks():
                destination.write(chunk)
            destination.close()

            image.original_image = path
            PIL.Image.open(
                os.path.join(settings.MEDIA_ROOT,
                             str(image.original_image))).verify()
            image.save()

            image.upload_files_to_s3()
            image.delete_local_image_files()

            create_message('image_face_detect', image.id)

            serializer = ImageSerializer(image)
            return Response(serializer.data)

        except Exception as e:

            if image:
                image.delete_local_image_files()
                image.delete()

            raise ParseError(str(e))
    def rotate_photo(self, anticlockwise_angle):
        '''
        Rotates the photo
        '''
        path_and_filename = ''.join([settings.MEDIA_ROOT, str(self.photo)])
        im = Image.open(path_and_filename)

        new_image = im.rotate(anticlockwise_angle, resample=Image.BICUBIC, expand=True)
        self.photo = ''.join(['profile_photos/', create_hash(self.name), '.jpg'])
        new_image.save(''.join([settings.MEDIA_ROOT, str(self.photo)]))

        os.remove(path_and_filename)
Beispiel #6
0
def image_upload(request, person_id=0, person=None):
    '''
    View that receives the uploaded image
    '''

    #Ensure that profile is not locked
    if request.user.id != person.user_id and person.locked == True:
        return HttpResponse("1")
        raise Http404

    try:
        uploaded = request.FILES['picture']
    except Exception as e:
        return HttpResponse(str(e))
        raise Http404

    #get the name, and extension and create a unique filename
    name, ext = os.path.splitext(uploaded.name)
    filename = create_hash(person.name) + '.jpg'
    photo_file = ''.join([settings.MEDIA_ROOT, 'profile_photos/', filename])

    result = {
        'name': uploaded.name,
        'size': uploaded.size,
        'url': '/media/profile_photos/' + filename,
        'filename': filename
    }

    if uploaded.size > MAX_FILE_SIZE:
        result['error'] = _('File is too big')
        return HttpResponse(json.dumps(result),
                            content_type='application/json')

    #Write the file to the destination
    destination = open(photo_file, 'wb+')

    for chunk in uploaded.chunks():
        destination.write(chunk)
    destination.close()

    #Check this is a valid image
    try:
        person.set_hires_photo(filename)

    except Exception as ex:
        result['error'] = str(ex)

        return HttpResponse(json.dumps(result),
                            content_type='application/json')

    person.save()

    return HttpResponse(json.dumps(result), content_type='application/json')
Beispiel #7
0
    def update(self, request, pk=None):

        queryset = Person.objects.filter(family_id = request.user.family_id)
        person = get_object_or_404(queryset, pk=pk)

        #Make sure we can't change locked profiles
        if person.locked and person.user_id != request.user.id:
            raise PermissionDenied('Access denied to locked profile')


        x, x_valid = intTryParse(request.data.get("x"))
        y, y_valid = intTryParse(request.data.get("y"))
        w, w_valid = intTryParse(request.data.get("w"))
        h, h_valid = intTryParse(request.data.get("h"))
        r, r_valid = intTryParse(request.data.get("r"))

        if not (x_valid and y_valid and w_valid and h_valid and r_valid):
            raise ParseError('Invalid crop parameters')



        try:
            uploaded = request.FILES['picture']

            if uploaded.size > MAX_FILE_SIZE:
                raise ValueError('File is too big')

            filename =  create_hash(person.name) +'.jpg'
            photo_file = ''.join([settings.MEDIA_ROOT, 'profile_photos/', filename])

            #Write the file to the destination
            destination = open(photo_file, 'wb+')


            for chunk in uploaded.chunks():
                destination.write(chunk)
            destination.close()

            person.set_profile_image_crop_rotate_resize(photo_file, x, y, w, h, r)
            person.save()

        except Exception as e:
            
            raise ParseError(str(e))


        create_message('profile_photo_process', person.id)

        serializer = PersonListSerializer(person)
        return Response(serializer.data)
Beispiel #8
0
    def save(self, *args, **kwargs):
        '''
        Overrides the save method
        '''
        #New object
        if not self.id:

            #Check email does not already exist

            self.confirmation_key = create_hash(self.email_address)
            self.send_email()

        super(SignUp, self).save(*args,
                                 **kwargs)  # Call the "real" save() method.
Beispiel #9
0
    def _create_thumbnail(self, size, image = None):
        '''
        Creates the thumbnails
        '''
        if not image:
            image = PIL.Image.open(self._get_absolute_image_path())

        image.thumbnail(size)

        filename = create_hash(str(self.original_image)) + '.jpg'
        path_and_filename = upload_to(self, str(filename))

        image = image.convert('RGB')
        image.save(settings.MEDIA_ROOT + str(path_and_filename), "JPEG", quality=90)

        return path_and_filename, image
Beispiel #10
0
def process_image(filename, file, gallery):
    '''
    Processes each image file
    '''
    name, ext = os.path.splitext(file.name)
    filename =  create_hash(name) +'.jpg'

    im = Image(gallery_id=gallery.id, family_id=gallery.family_id, title=name)
    upload_name = upload_to(im, filename)

    result = {
        'name': basename(name),
        'size': file.size,
        'url': settings.MEDIA_URL + str(upload_name),
        'filename': filename
    }

    if file.size > MAX_FILE_SIZE:
        result['error'] = tran('File is too big')
        return result

    #Write the file to the destination
    destination = open(os.path.join(settings.MEDIA_ROOT, str(upload_name)), 'wb+')

    for chunk in file.chunks():
        destination.write(chunk)
    destination.close()

    im.original_image = upload_name

    #Check this is a valid image
    try:
        PIL.Image.open(os.path.join(settings.MEDIA_ROOT, str(im.original_image))).verify()
        im.save()
        im.upload_files_to_s3()
        im.delete_local_image_files()

        result['image_id'] = im.id


    except:
        im.delete_local_image_files()
        result['error'] = tran('Invalid image!')

    return result
Beispiel #11
0
 def generate_confirmation_key(self):
     '''
     Creates a confirmation key to be emailed to new user
     '''
     self.confirmation_key = create_hash(self.email_address)