imagemagick convert:如何判断图像是否需要旋转? [英] imagemagick convert: how to tell if images need to be rotated?

查看:93
本文介绍了imagemagick convert:如何判断图像是否需要旋转?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Rails应用程序中,我让用户上传图像以用作其制作资源的缩略图.我使用imagemagick的convert命令进行所有图像处理:我将其转换为jpg,将其信箱转换为800x600px,然后制作一些较小的缩略图版本.

In my rails app, i let the user upload images to use as thumbnails for resources they've made. I do all the image processing with imagemagick's convert command: i convert to jpg, letterbox it to 800x600px and then make some smaller thumbnail versions of that.

一切正常.问题是,例如,某些从我的iPhone上掉下来的图像的方向错误.我的桌面似乎对此有不同的处理方式:当我在文件选择器对话框中看到图像的微小缩略图时,它的方向错误,但是如果在图像查看工具中打开它,则它的方向正确.

This is all working fine. The problem is that some images that have come off my iPhone, for example, have the wrong orientation. My desktop seems to variously deal with this: when i see the tiny thumbnail for the image in the file chooser dialog, it's got the wrong orientation, but if I open it in the image viewing tool, it's got the correct orientation.

大概图像文件中有一些标头或元数据或某些内容,它们告诉应用程序需要旋转文件吗?我认为我需要在对图像进行其他任何处理之前先阅读此内容,以便在进行所有其他操作之前可以旋转图像(如有必要).

Presumably there's some header or metadata or something in the image file which tells the app that it needs to rotate the file? I think that I need to read this in before I do any other processing of the image, so that I can rotate the image (if necessary) before doing all the other stuff.

有人可以告诉我如何在Ubuntu服务器上的bash中做到这一点吗?

Can anyone tell me how I can do this, in bash on an Ubuntu server?

谢谢,麦克斯

有点谷歌搜索表明这与EXIF标头有关...

a bit of googling suggests that this is to to with EXIF headers...

推荐答案

的确,您必须查找读取Image的Exif元数据的方向值.使用imagemagick可以通过

It's true that you will have to look for orientation value reading Exif metadata of Image. Using imagemagick you can get this value by

identify -format '%[exif:orientation]' <Path to Image> 
//or by using
identify -format '%[orientation]' <Path to Image>

Exif方向值的范围是1到8,并映射到以下方向:

Exif Orientation values range from 1 to 8 and maps to orientation as below:

  • 1 =水平(正常)
  • 2 =水平镜像
  • 3 =旋转180
  • 4 =垂直镜像
  • 5 =水平镜像并旋转270 CW
  • 6 =旋转90度
  • 7 =水平镜像并旋转90 CW
  • 8 =旋转270 CW

因此,如果此元数据标签返回的值为非1,则图像将需要进行方向校正.如果此标签没有返回任何值,则可以认为它具有正常方向.

Thus, image would require orientation correction if this metadata tag returns with a non 1 value. If this tag returns with no value it can be assumed to be having normal orientation.

要使方向正确,您可以使用以下命令

To correct orientation to normal you can use following command

convert -auto-orient <inputImagePath> <outputImagePath> 

这篇关于imagemagick convert:如何判断图像是否需要旋转?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆