def process_avatar(file_storage, upload_set): """将图片处理为适合裁剪的大小,即长宽均不超过1000""" image = open_image(file_storage) image = center_crop(image) image = image.resize((300, 300), Image.ANTIALIAS) ext = extension(file_storage.filename) return save_image(image, upload_set, ext), image.size
def process_image(file_storage, upload_set, max_border): """ 将Flask中上传的图片进行居中裁剪、缩放,然后保存 @param file_storage: Werkzeug FileStorage对象, 如request.files['image'] @param upload_set: Flask-Uploads UploadSet对象 @param max_border: 待缩放的边长 @return: 图片filename """ # 打开图片 image = Image.open(file_storage.stream) # 居中裁剪 w, h = image.size if w > h: border = h crop_region = ((w - border) / 2, 0, (w + border) / 2, border) else: border = w crop_region = (0, (h - border) / 2, border, (h + border) / 2) image = image.crop(crop_region) # 缩放 if border > max_border: image = image.resize((max_border, max_border), Image.ANTIALIAS) # 保存 ext = extension(file_storage.filename) filename = '%s.%s' % (random_filename(), ext) folder = upload_set.config.destination filename = upload_set.resolve_conflict(folder, filename) path = os.path.join(folder, filename) image.save(path) return filename
def process_site_image(file_storage): """处理并保存全站通用图片。 Center clipping, resize and then save the avatar.""" image = open_image(file_storage) ext = extension(file_storage.filename) return save_image(image, images, ext)
def process_user_avatar(file_storage, upload_set, border): """Center clipping, resize and then save the avatar.""" image = open_image(file_storage) image = center_crop(image) image = resize_square(image, border) ext = extension(file_storage.filename) return save_image(image, upload_set, ext)
def process_channel_cover(file_storage): """处理并保存频道图标。 Center clipping, resize and then save the avatar.""" image = open_image(file_storage) image = center_crop(image) image = resize_square(image, 640) ext = extension(file_storage.filename) return save_image(image, images, ext)
def process_topic_avatar(file_storage, border): """处理并保存话题图标。 Center clipping, resize and then save the avatar. """ image = open_image(file_storage) image = center_crop(image) image = resize_square(image, border) ext = extension(file_storage.filename) return save_image(image, topic_avatars, ext)
def save_image(image, file_storage, upload_set): """Save image with random filename and original ext.""" ext = extension(file_storage.filename) filename = '%s.%s' % (random_filename(), ext) dir_path = upload_set.config.destination if not os.path.isdir(dir_path): os.mkdir(dir_path) path = os.path.join(dir_path, filename) image.save(path) return filename
def save_image(file_storage, upload_set): """获取上传图片的size,并保存""" image = Image.open(file_storage.stream) if image.mode != "RGB": image = image.convert("RGB") w, h = image.size ext = extension(file_storage.filename) filename = '%s.%s' % (random_filename(), ext) path = os.path.join(upload_set.config.destination, filename) image.save(path) return filename, w, h
def save_image(image, upload_set, file_storage): """保存图片 保存到upload_set对应的文件夹中 文件后缀使用file_storage中文件名的后缀 """ ext = extension(file_storage.filename) filename = '%s.%s' % (random_filename(), ext) dir_path = upload_set.config.destination # 若不存在此目录,则创建之 if not os.path.isdir(dir_path): os.mkdir(dir_path) path = os.path.join(dir_path, filename) image.save(path) return filename
def crop_image(filename, upload_set, top_left_x_ratio, top_left_y_ratio, bottom_right_x_ratio, bottom_right_y_ratio): """裁剪用户头像""" file_path = upload_set.path(filename) image = Image.open(file_path) image = crop_by_ratio(image, top_left_x_ratio, top_left_y_ratio, bottom_right_x_ratio, bottom_right_y_ratio) image = center_crop(image) image = resize_square(image, 160) # 删除裁剪前的图片 os.remove(file_path) # 保存裁剪后的图片 ext = extension(filename) return save_image(image, upload_set, ext)
def save_image(image, upload_set, image_type, file_storage): """保存图片 保存到upload_set对应的文件夹中 文件后缀使用file_storage中文件名的后缀 """ ext = extension(file_storage.filename) filename = '%s.%s' % (random_str(), ext) dir_path = upload_set.config.destination folder = time.strftime("%Y%m%d", time.localtime()) dir_path = os.path.join(dir_path, folder) print dir_path # 若不存在此目录,则创建之 if not os.path.isdir(dir_path): os.mkdir(dir_path) path = os.path.join(dir_path, filename) image.save(path) if image_type == 1: # 居中裁剪 w, h = image.size if w > h: border = h avatar_crop_region = ((w - border) / 2, 0, (w + border) / 2, border) else: border = w avatar_crop_region = (0, (h - border) / 2, border, (h + border) / 2) image = image.crop(avatar_crop_region) # 缩放 max_border = 100 image = image.resize((max_border, max_border), Image.ANTIALIAS) filename2 = '%s.%s' % (filename[:-4] + "_thumb", ext) path2 = os.path.join(dir_path, filename2) image.save(path2) return filename elif image_type == 'photo': filename1 = '%s.%s' % (filename[:-4] + "_medium", ext) path1 = os.path.join(dir_path, filename1) image.save(path1) filename2 = '%s.%s' % (filename[:-4] + "_thumb", ext) path2 = os.path.join(dir_path, filename2) image.save(path2) return folder, filename else: return folder + "/" + filename
def save_image(image, upload_set, image_type, file_storage): """保存图片 保存到upload_set对应的文件夹中 文件后缀使用file_storage中文件名的后缀 """ ext = extension(file_storage.filename) filename = '%s.%s' % (random_str(), ext) dir_path = upload_set.config.destination folder = time.strftime("%Y%m%d", time.localtime()) dir_path = os.path.join(dir_path, folder) print dir_path # 若不存在此目录,则创建之 if not os.path.isdir(dir_path): os.mkdir(dir_path) path = os.path.join(dir_path, filename) image.save(path) if image_type == 1: # 居中裁剪 w, h = image.size if w > h: border = h avatar_crop_region = ((w - border) / 2, 0, (w + border) / 2, border) else: border = w avatar_crop_region = (0, (h - border) / 2, border, (h + border) / 2) image = image.crop(avatar_crop_region) # 缩放 max_border = 100 image = image.resize((max_border, max_border), Image.ANTIALIAS) filename2 = '%s.%s' % (filename[:-4] + "_thumb", ext) path2 = os.path.join(dir_path, filename2) image.save(path2) return filename elif image_type == 'photo': filename1 = '%s.%s' % (filename[:-4] + "_medium", ext) path1 = os.path.join(dir_path, filename1) image.save(path1) filename2 = '%s.%s' % (filename[:-4] + "_thumb", ext) path2 = os.path.join(dir_path, filename2) image.save(path2) return folder, filename else: return folder+"/"+filename
def upload_paper(): """response function for paper uploading action""" form = UploadForm(form=request.form) if request.method == 'POST' and form.validate_on_submit(): filename, url = '', '' if 'paper' in request.files and request.files['paper'].filename: try: filename = gen_filename(form.title.data, form.author.data) +\ '.' +\ extension(request.files['paper'].filename) paper_uploader.save(request.files['paper'], name=filename) except UploadNotAllowed: return render_template('upload.html', error='Please select file to upload.', form=form, upload_paper_active=True) else: url = form.url.data paper = Paper(form.author.data, form.title.data, filename, url, date.today(), User.query.filter_by(username=session['username']).first()._id) db.session.add(paper) try: db.session.commit() except IntegrityError: db.session.rollback() # db.session.delete(paper) return render_template('upload.html', error='Filename already exists.', form=form, upload_paper_active=True) flash('Paper uploaded successfully.') return render_template('upload.html', success='Paper uploaded successfully.', form=UploadForm(), upload_paper_active=True) return render_template('upload.html', form=form, upload_paper_active=True)
def process_user_background(file_storage, border): """处理并保存用户首页背景。""" image = open_image(file_storage) ext = extension(file_storage.filename) return save_image(image, images, ext)
def process_image_for_cropping(file_storage, upload_set): """将图片处理为适合裁剪的大小,即长宽均不超过1000""" image = open_image(file_storage) image = resize_with_max(image, 1000) ext = extension(file_storage.filename) return save_image(image, upload_set, ext), image.size