【发布时间】:2021-04-07 09:10:19
【问题描述】:
我有下面的代码。由于某种原因,它输出了这个乱码:
原文:
(在这个练习中我不允许使用任何现成的 Python 库)
@app.route("/flip", methods=["POST"])
def flip():
# retrieve parameters from html form
if 'horizontal' in request.form['mode']:
direction = 90
elif 'vertical' in request.form['mode']:
direction = -90
filename = request.form['image']
# open and process image
target = os.path.join(APP_ROOT, 'static/images')
destination = "/".join([target, filename])
img = Image.open(destination)
subprocess.call(['C:\Program Files\ImageMagick-7.0.10-Q16-HDRI\magick.exe', 'convert', f'{destination}', '-rotate', f'{direction}', f'{destination}'], shell=True)
# save and return image
destination = "/".join([target, 'flipped.jpg'])
if os.path.isfile(destination):
os.remove(destination)
img.save(destination)
return send_image('flipped.jpg')
编辑:当我将subprocess.call 中的目标图像设置为flipped.jpg - 输出是原始图像。为什么?
【问题讨论】:
-
您几乎肯定不想要
magick.exe convert INPUT OUTPUT,因为这会导致特定的、很少需要的行为。你更可能想要magick.exe INPUT OUTPUT,所以省略'convert'。虽然我怀疑这会解决它。在命令提示符中单独尝试该命令,看看它的行为是否相同,如果是,我们可以排除 Python 作为原因。 -
请也分享确切输入图像文件 - 是JPEG还是PNG?
-
为什么你有
img = Image.open()和img.save()在里面?你不应该使用 PIL。 -
@MarkSetchell jpg。正确,我忘了删除 PIL 的东西。
-
@MarkSetchell Still - 如何将源图像转换为旋转图像,以便每次调用
flip的输入都会旋转最新(旋转)图像?意思是,如何在不使用 PIL 的情况下重写 PIL 代码?
标签: python flask imagemagick imagemagick-convert