Exemple #1
0
    def set_hires_photo(self, filename):
        '''
        Checks file is an image and converts it to a small jpeg
        DEPRECATED
        '''
        #Check this is a valid image
        try:

            path_and_filename = ''.join(
                [settings.MEDIA_ROOT, 'profile_photos/', filename])
            im = Image.open(path_and_filename)
            im.verify()

            #Open it again!
            #http://stackoverflow.com/questions/12413649/python-image-library-attributeerror-nonetype-object-has-no-attribute-xxx
            im = Image.open(path_and_filename).convert('RGB')  #Convert to RGB
            im.thumbnail((500, 500), Image.ANTIALIAS
                         )  #Reasonble size to allow cropping down to 200x200

            im.save(path_and_filename, "JPEG", quality=95)

            self.photo = 'profile_photos/' + filename
            upload_file_to_s3(self.photo)

        except:
            os.remove(path_and_filename)
            raise Exception(
                tran("Invalid image!")
            )  #Use tran here as it gets serialized so lazy tran fails
Exemple #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)
Exemple #3
0
 def upload_files_to_s3(self):
     '''
     Uploads image and thumbnail files to s3
     '''
     upload_file_to_s3(self.original_image)
     upload_file_to_s3(self.thumbnail)
     upload_file_to_s3(self.large_thumbnail)
Exemple #4
0
    def set_profile_image_crop_rotate_resize(self,
                                             path_and_filename,
                                             x,
                                             y,
                                             w,
                                             h,
                                             r,
                                             test=False):
        '''
        sets the profile photo, supercedes set_hires_photo, crop_and_resize_photo, rotate_photo
        '''

        try:
            media_path = Path(settings.MEDIA_ROOT)
            path = Path(path_and_filename)
            rel_path = Path(os.path.relpath(path, media_path))

            small_thumb_path = Path(rel_path.parent, rel_path.stem +
                                    '_small_thumb').with_suffix('.jpg')
            large_thumb_path = Path(rel_path.parent, rel_path.stem +
                                    '_large_thumb').with_suffix('.jpg')

            im = Image.open(path)
            im.verify()

            #Open it again!
            #https://pillow.readthedocs.io/en/3.0.x/reference/Image.html?highlight=verify#PIL.Image.Image.verify

            im = Image.open(path)
            im = im.convert('RGB')

            if r != 0:
                im = im.rotate(r, resample=Image.BICUBIC, expand=True)

            im = im.crop((x, y, x + w, y + h))

            # small thumbnail
            small_thumb = im.copy()
            small_thumb.thumbnail((80, 80), Image.ANTIALIAS)
            small_thumb.save(media_path.joinpath(small_thumb_path),
                             "JPEG",
                             quality=75)

            # large thumbnail
            large_thumb = im.copy()
            large_thumb.thumbnail((200, 200), Image.ANTIALIAS)
            large_thumb.save(media_path.joinpath(large_thumb_path),
                             "JPEG",
                             quality=75)

            old_photo = self.photo
            old_small_thumbnail = self.small_thumbnail
            old_large_thumbnail = self.large_thumbnail

            # Set model properties
            self.photo = str(rel_path)
            self.small_thumbnail = str(small_thumb_path)
            self.large_thumbnail = str(large_thumb_path)

            upload_file_to_s3(self.photo)
            upload_file_to_s3(self.small_thumbnail)
            upload_file_to_s3(self.large_thumbnail)

            # Remove old photos from S3
            if old_photo:
                old_photo_path = ''.join([settings.MEDIA_ROOT, str(old_photo)])
                remove_file_from_s3(old_photo_path)
                old_small_thumbnail_path = ''.join(
                    [settings.MEDIA_ROOT,
                     str(old_small_thumbnail)])
                remove_file_from_s3(old_small_thumbnail_path)
                old_large_thumbnail_path = ''.join(
                    [settings.MEDIA_ROOT,
                     str(old_large_thumbnail)])
                remove_file_from_s3(old_large_thumbnail_path)

        except:

            raise Exception("Invalid image!")

        finally:

            # Need to check these images if testing
            if not test:
                self.remove_local_images()