【问题标题】:ImageMagick's convert outputs garbled imageImageMagick 的转换输出乱码图像
【发布时间】: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


【解决方案1】:

已解决:

    if 'horizontal' in request.form['mode']:
        direction = 'flip'
    elif 'vertical' in request.form['mode']:
        direction = 'flop'
        filename = request.form['image']

    # open and process image
    target = os.path.join(APP_ROOT, 'static/images')
    destination = "/".join([target, filename])
    subprocess.call(['C:\Program Files\ImageMagick-7.0.10-Q16-HDRI\magick.exe', 
                     'convert',  f'{destination}', '-'f'{direction}', f'{destination}'], 
                     shell=True)
    return send_image(filename)

【讨论】:

  • 你为什么坚持magick convert INPUT OPERATIONS OUTPUT?你应该使用magick INPUT OPERATIONS OUTPUT 没有 convert 就像我在 cmets 中建议的那样。
猜你喜欢
  • 2018-07-15
  • 1970-01-01
  • 1970-01-01
  • 2017-07-16
  • 2019-01-02
  • 2012-09-18
  • 2015-08-08
  • 2015-11-25
  • 2016-11-22
相关资源
最近更新 更多