【发布时间】:2021-11-30 23:40:33
【问题描述】:
我正在编写一个 Flask 应用程序,用户可以在其中编辑照片并旋转它们。我添加了两个左右旋转的按钮:
<img class="card-img-top"
src="data:image/jpeg;base64,{{image}}"
alt="Edit pic"
name="originalPic">
<button class="btn" name="right_button"><i class="fa fa-rotate-right"></i></button>
<button class="btn" name="left_button"><i class="fa fa-rotate-left"></i></button>
在 python 代码中,我旋转了图像,但如果我在任何按钮上多次按下,图像不会继续旋转。如果我从右侧按钮切换并按下左侧按钮,它只会改变位置。另外,在python代码中,我使用的是HTML页面上显示的原始图像,但是如果用户旋转图片,它的位置会发生变化。如何在 Python 代码中传递旋转后的图像(图像的实际位置)?将图像实际旋转多少次,用户想要多少次,他按下任何按钮的次数就多少次。
def check_rotate(img):
right_button_pushed = request.form.get('right_button')
left_button_pushed = request.form.get('left_button')
if type(right_button_pushed) == str:
print('RIGHT')
angle = 90
rotated_image = img.rotate(angle)
elif type(left_button_pushed) == str:
print('LEFT')
angle = -90
rotated_image = img.rotate(angle)
else:
rotated_image = ''
try:
data = io.BytesIO()
# First save image as in-memory.
rotated_image.save(data, "PNG")
# Then encode the saved image file.
encoded_img_data = base64.b64encode(data.getvalue())
return encoded_img_data # rotated_image
except AttributeError:
pass
而我是这样使用上面的方法的:
@app.route('/', methods=['GET', 'POST'])
def index():
original_image = "https://cdn.pixabay.com/photo/2017/09/25/13/12/cocker-spaniel-2785074__340.jpg"
response = requests.get(original_image)
img = Image.open(BytesIO(response.content))
if request.method == 'POST':
rotated_image = check_rotate(img) #How can I update the image to the image with the actual position of rotation?
return render_template("index.html", image=rotated_image.decode('utf-8'))
【问题讨论】:
标签: python html image flask python-imaging-library