def compress_image(filename, folder): file_path = image_set.path(filename=filename, folder=folder) image = Image.open(file_path) if image.mode != "RGB": image = image.convert("RGB") if max(image.width, image.height) > 1600: maxsize = (1600, 1600) image.thumbnail(maxsize) compressed_filename = '{}.jpg'.format(uuid.uuid4()) compressed_file_path = image_set.path(filename=compressed_filename, folder=folder) image.save(compressed_file_path, optimize=True, quality=85) original_size = os.stat(file_path).st_size compressed_size = os.stat(compressed_file_path).st_size percentage = round((original_size - compressed_size) / original_size * 100) print("The file size is reduced by {}%, from {} to {}.".format( percentage, original_size, compressed_size)) os.remove(file_path) return compressed_filename
def compress_image(filename, folder): filepath = image_set.path(filename=filename, folder=folder) image = Image.open(filepath) if image.mode != 'RGB': image = image.convert('RGB') if max(image.width, image.height) > 1600: maxsize = (1600, 1600) image.thumbnail(maxsize, Image.ANTIALIAS) compressed_filename = '{}.jpg'.format(uuid.uuid4()) compressed_filepath = image_set.path(compressed_filename, folder=folder) image.save(compressed_filepath, optimizer=True, quality=50) os.remove(filepath) return compressed_filename
def put(self, recipe_id): file = request.files.get('cover') if not file: return {'message': 'Not a valid image'}, HTTPStatus.BAD_REQUEST if not image_set.file_allowed(file, file.filename): return {'message': 'File type not allowed'}, HTTPStatus.BAD_REQUEST recipe = Recipe.get_by_id(recipe_id=recipe_id) if recipe is None: return {'message': 'Recipe not found'}, HTTPStatus.NOT_FOUND current_user = get_jwt_identity() if current_user != recipe.user_id: return {'message': 'Access is not allowed'}, HTTPStatus.FORBIDDEN if recipe.cover_image: cover_path = image_set.path(folder='recipes', filename=recipe.cover_image) if os.path.exists(cover_path): os.remove(cover_path) filename = save_image(image=file, folder='recipes') recipe.cover_image = filename recipe.save() return recipe_cover_schema.dump(recipe).data, HTTPStatus.OK
def put(self): """This method has the logic to put the user avatar image file""" file = request.files.get('avatar') # Validate image if not file: return {'message': 'Not a valid image'}, HTTPStatus.BAD_REQUEST user = User.get_by_id(id=get_jwt_identity()) if user.avatar_image: avatar_path = image_set.path(folder='avatar', filename=user.avatar_image) # Check is avatar exist and removed before we replace it with our uploaded image if os.path.exists(avatar_path): os.remove(avatar_path) # Save image filename = save_image(image=file, folder='avatars') # Store the filename of image withing 'user.avatar_image' user.avatar_image = filename # Save image update to the database user.save() # Clear cache clear_cache('/recipes') # Finally, return the URL image in a JSON format and with status code HTTP 200 OK return user_avatar_schema.dump(user).data, HTTPStatus.OK
def put(self): file = request.files.get('avatar') if not file: return {'message': 'Not a valid image'}, HTTPStatus.BAD_REQUEST if not image_set.file_allowed(file, file.filename): return {'message': 'File type not allowed'}, HTTPStatus.BAD_REQUEST user = User.get_by_id(id=get_jwt_identity()) if user.avatar_image: avatar_path = image_set.path(folder='avatars', filename=user.avatar_image) if os.path.exists(avatar_path): os.remove(avatar_path) filename = save_image(image=file, folder='avatars') user.avatar_image = filename user.save() clear_cache('/politicians') return user_avatar_schema.dump(user).data, HTTPStatus.OK
def put(self, politician_id): file = request.files.get('cover') if not file: return {'message': 'Not a valid image'}, HTTPStatus.BAD_REQUEST if not image_set.file_allowed(file, file.filename): return {'message': 'File type not allowed'}, HTTPStatus.BAD_REQUEST politician = Politician.get_by_id(politician_id=politician_id) if politician is None: return {'message': 'Politician not found'}, HTTPStatus.NOT_FOUND current_user = get_jwt_identity() if current_user != politician.user_id: return {'message': 'Access is not allowed'}, HTTPStatus.FORBIDDEN if politician.cover_image: cover_path = image_set.path(folder='politicians', filename=politician.cover_image) if os.path.exists(cover_path): os.remove(cover_path) filename = save_image(image=file, folder='politicians') politician.cover_image = filename politician.save() clear_cache('/politicians') return politician_cover_schema.dump(politician).data, HTTPStatus.OK
def compress_image(filename, folder): file_path = image_set.path(filename=filename, folder=folder) image = Image.open(file_path) if image.mode != 'RGB': image = image.convert('RGB') if max(image.width, image.height) > 1600: maxsize = (1600, 1600) image.thumbnail(maxsize, Image.ANTIALIAS) compressed_filename = f'{uuid.uuid4()}.jpg' compressed_file_path = image_set.path(filename=compressed_filename, folder=folder) image.save(compressed_file_path, optimize=True, quality=85) original_size = os.stat(file_path).st_size compressed_size = os.stat(compressed_file_path).st_size percentage = round((original_size - compressed_size) / original_size * 100) print(f'The file size is reduced by {percentage}%, from {original_size} to {compressed_size}.') os.remove(file_path) return compressed_filename
def compress_image(filename, folder): """Function to compress image""" file_path = image_set.path(filename=filename, folder=folder) # Create the image object from the image file. image = Image.open(file_path) # Check the color mode of the image and then convert the image # to the 'RGB' color mode if image.mode != 'RGB': image = image.convert('RGB') # Resize image; no bigger than 800 px if max(image.width, image.height) > 800: maxsize = (800, 800) image.thumbnail(maxsize, Image.ANTIALIAS) # Generate a new filename for our compressed image compressed_filename = '{}.jpg'.format(uuid.uuid4()) # Also, generate the new path compressed_file_path = image_set.path(filename=compressed_filename, folder=folder) # Save image with quality = 85 image.save(compressed_file_path, optimize=True, quality=85) # Get the size in bytes original_size = os.stat(file_path).st_size compressed_size = os.stat(compressed_file_path).st_size percentage = round((original_size - compressed_size) / original_size * 100) print( f'The file size is reduced by {percentage}%, from {original_size} to {compressed_size}' ) # Remove original image, return the compressed image os.remove(file_path) return compressed_filename
def put(self): file = request.files.get("profile_picture") if not file: return {"message": "Not a valid image"}, HTTPStatus.BAD_REQUEST if not image_set.file_allowed(file, file.filename): return {"message": "File type not allowed"}, HTTPStatus.BAD_REQUEST user = User.get_by_id(id=get_jwt_identity()) if user.profile_picture: profile_picture_path = image_set.path( folder="profile-pictures", filename=user.profile_picture) if os.path.exists(profile_picture_path): os.remove(profile_picture_path) filename = save_image(image=file, folder="profile-pictures") user.profile_picture = filename user.save() return user_profile_picture_schema.dump(user).data, HTTPStatus.OK
def put(self, recipe_id): """This method has got the logic to put the cover image of the recipe.""" file = request.files.get('cover') # Check if cover image exists and whether the file extension is permitted if not file: return {'message': 'Not a valid image'}, HTTPStatus.BAD_REQUEST if not image_set.file_allowed(file, file.filename): return {'message': 'File type not allowed'}, HTTPStatus.BAD_REQUEST # Retrieved the Recipe object recipe = Recipe.get_by_id(recipe_id=recipe_id) if recipe is None: return {'message': 'Recipe not found'}, HTTPStatus.NOT_FOUND # Check right to modify the recipe current_user = get_jwt_identity() if current_user != recipe.user_id: return {'message': 'Access is not allowed'}, HTTPStatus.FORBIDDEN if recipe.cover_image: cover_path = image_set.path(folder='recipes', filename=recipe.cover_image) if os.path.exists(cover_path): os.remove(cover_path) # Save the uploaded image filename = save_image(image=file, folder='recipes') recipe.cover_image = filename # Save the recipe recipe.save() # Clear cache clear_cache('/recipes') # Finally, return the URL image in a JSON format and with status code HTTP 200 OK return recipe_cover_schema.dump(recipe).data, HTTPStatus.OK
def put(self, item_id): file = request.files.get("picture") current_user = get_jwt_identity() item = Item.get_by_id(item_id=item_id) if item.user_id != current_user: return {"message": "Access not allowed"}, HTTPStatus.FORBIDDEN if not file: return {"message": "Not a valid image"}, HTTPStatus.BAD_REQUEST if not image_set.file_allowed(file, file.filename): return {"message": "File type not allowed"}, HTTPStatus.BAD_REQUEST if item.picture: picture_path = image_set.path(folder="pictures", filename=item.picture) if os.path.exists(picture_path): os.remove(picture_path) filename = save_image(image=file, folder="pictures") item.picture = filename item.save() saveItemHistory(item.id) return item_picture_schema.dump(item).data, HTTPStatus.OK
def put(self, recipe_id): """ Gets the cover image in request.files and verifies whether it exists and if the file extension is permitted """ file = request.files.get('cover') if not file: return {'message': 'Not a valid image'}, HTTPStatus.BAD_REQUEST if not image_set.file_allowed(file, file.filename): return {'message': 'File type not allowed'}, HTTPStatus.BAD_REQUEST # Check whether user hs the right to modify the recipe recipe = Recipe.get_by_id(recipe_id=recipe_id) if recipe is None: return {'message': 'Recipe not found'}, HTTPStatus.NOT_FOUND current_user = get_jwt_identity() if current_user != recipe.user_id: return {'message': 'Access in not allowed'}, HTTPStatus.FORBIDDEN # If the user has the right to, we will go # ahead and modify the cover image of the recipe if recipe.cover_image: cover_path = image_set.path(folder='recipes', filename=recipe.cover_image) if os.path.exists(cover_path): os.remove(cover_path) # User the save_image function to save the uploaded image filename = save_image(image=file, folder='recipes') recipe.cover_image = filename recipe.save() clear_cache('/recipes') # clears old cache data when updated return recipe_cover_schema.dump(recipe).data, HTTPStatus.OK