Resize images based on their orientation

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
aamir4774@gmail.com
Posts: 15
Joined: 2019-03-05T22:47:03-07:00
Authentication code: 1152

Resize images based on their orientation

Post by aamir4774@gmail.com »

Hi ImageMagick Community,

I have got a requirement to resize the JPEG, TIFF, EPS and PNG image formats as per their orientation and later convert them to JPEG. I want to know if there's a way using ImageMagick by which we can find if the orientation of the image is "landscape" or "portrait". As the requirement is to resize the height of the image in case of portrait images and to resize width in case of landscape ones. Currently I'm using the below code to do so:

For Portrait I'm changing the resolution to 2048px and pixel density to 72dpi as below.

Code: Select all

convert -units PixelsPerInch <input_file> -density 72 -resize x2048 <output_file.jpeg>
And for Landscape images similar thing but for width:

Code: Select all

convert -units PixelsPerInch <input_file> -density 72 -resize 2048 <output_file.jpeg>
To sum up I just want a single command to resize height in case of portrait and width in case of landscape images, your suggestions/help will be held in high regards.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Resize images based on their orientation

Post by fmw42 »

No need for any orientation test. You can just use:

Code: Select all

convert -units PixelsPerInch <input_file> -density 72 -resize 2048x2048 <output_file.jpeg>
That should take the largest dimension of your image and make that 2048. The other dimension will be in proportion to the aspect ratio. So if the image is landscape the width will become 2048 and the height will be less. If portrait, the height will become 2048 and the width will be less.

See https://imagemagick.org/script/command- ... p#geometry
aamir4774@gmail.com
Posts: 15
Joined: 2019-03-05T22:47:03-07:00
Authentication code: 1152

Re: Resize images based on their orientation

Post by aamir4774@gmail.com »

Thanks Fred, this worked like a charm.
My requirement has got a bit changed, the output file should be less than 2Mb and I'm using the below command to do it:

Code: Select all

convert -quiet -units PixelsPerInch <input> -define jpeg:extent=2048kb -scale 50% -density 72 -resize 2048x2048 <output>
This is working fine for major of the formats but for "TIF" files the output size is more than that of the specified.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Resize images based on their orientation

Post by snibgo »

What compression are you using for your TIF? If none, you might use "-compress JPEG", perhaps with a quality setting, but this will be lossy.
snibgo's IM pages: im.snibgo.com
aamir4774@gmail.com
Posts: 15
Joined: 2019-03-05T22:47:03-07:00
Authentication code: 1152

Re: Resize images based on their orientation

Post by aamir4774@gmail.com »

Snibgo, I'm using the entire command, which I have posted in my recent comment. Do I need to append "-compress" attribute to it? If yes how that can be done along with a quality setting?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Resize images based on their orientation

Post by snibgo »

You could add compression and quality like this:

Code: Select all

convert -quiet -units PixelsPerInch <input> -scale 50% -density 72 -resize 2048x2048 -compress JPEG -quality 50 <output>
I removed "-define jpeg:extent=2048kb" because it has no effect when writing TIFF files.
snibgo's IM pages: im.snibgo.com
aamir4774@gmail.com
Posts: 15
Joined: 2019-03-05T22:47:03-07:00
Authentication code: 1152

Re: Resize images based on their orientation

Post by aamir4774@gmail.com »

Hi Snibgo,

This command didn't helped, the output file size remained somewhat in the similar range to that of we were getting using the earlier command.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Resize images based on their orientation

Post by snibgo »

What type of images are they? JPEG compression was designed for photos. We can give better advice if you supply a link to a sample input.
snibgo's IM pages: im.snibgo.com
aamir4774@gmail.com
Posts: 15
Joined: 2019-03-05T22:47:03-07:00
Authentication code: 1152

Re: Resize images based on their orientation

Post by aamir4774@gmail.com »

Not sure if I can actually share the image with you, but sharing below its properties. Also, one thing I did noticed that the converted jpeg gets saved under a megabit or two when we open it using photoshop and try "save As" option.

Code: Select all

Format: TIFF (Tagged Image File Format)
  Mime type: image/tiff
  Class: DirectClass
  Geometry: 3940x6000+0+0
  Resolution: 600x600
  Print size: 6.56667x10
  Units: PixelsPerInch
  Colorspace: CMYK
  Type: ColorSeparation
  Endianess: LSB
  Depth: 8-bit
  Channel depth:
    Cyan: 8-bit
    Magenta: 8-bit
    Yellow: 8-bit
    Black: 8-bit
  Channel statistics:
    Pixels: 23640000
    Cyan:
      min: 0  (0)
      max: 255 (1)
      mean: 56.9053 (0.223158)
      standard deviation: 57.9747 (0.227352)
      kurtosis: -0.00605325
      skewness: 0.943392
      entropy: 0.806486
    Magenta:
      min: 0  (0)
      max: 255 (1)
      mean: 74.3601 (0.291608)
      standard deviation: 79.9296 (0.31345)
      kurtosis: -1.25253
      skewness: 0.511486
      entropy: 0.742452
    Yellow:
      min: 0  (0)
      max: 255 (1)
      mean: 124.835 (0.489549)
      standard deviation: 98.8279 (0.38756)
      kurtosis: -1.67763
      skewness: -0.0985619
      entropy: 0.809484
    Black:
      min: 0  (0)
      max: 255 (1)
      mean: 52.189 (0.204663)
      standard deviation: 74.6696 (0.292822)
      kurtosis: -0.374783
      skewness: 1.09457
      entropy: 0.595722
  Image statistics:
    Overall:
      min: 0  (0)
      max: 255 (1)
      mean: 77.0724 (0.302245)
      standard deviation: 77.8505 (0.305296)
      kurtosis: -1.01552
      skewness: 0.676241
      entropy: 0.738536
  Total ink density: 375.294%
  Rendering intent: Perceptual
  Gamma: 0.454545
  Chromaticity:
    red primary: (0.64,0.33)
    green primary: (0.3,0.6)
    blue primary: (0.15,0.06)
    white point: (0.3127,0.329)
  Matte color: grey74
  Background color: white
  Border color: srgb(223,223,223)
  Transparent color: none
  Interlace: None
  Intensity: Undefined
  Compose: Over
  Page geometry: 3940x6000+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: None
  Orientation: TopLeft
  Properties:
    date:create: 2019-08-14T03:12:34-07:00
    date:modify: 2019-08-14T03:03:38-07:00
    dc:format: image/tiff
    exif:PixelXDimension: 3940
    exif:PixelYDimension: 6000
    photoshop:ColorMode: 4
    signature: ac769c4cd214fabc7cb95e9618295821caed5c8791a69fb7b3d558fc4dbdb7d5
    tiff:alpha: unspecified
    tiff:endian: lsb
    tiff:photometric: separated
    tiff:rows-per-strip: 1
    tiff:software: Adobe Photoshop CS6 (Macintosh)
    tiff:timestamp: 2019:07:29 11:57:57
    xmp:CreateDate: 2003-07-01T10:39:54-07:00
    xmp:CreatorTool: Adobe Photoshop CC 2019 (Macintosh)
    xmp:MetadataDate: 2019-07-29T11:57:57-07:00
    xmp:ModifyDate: 2019-07-29T11:57:57-07:00
    xmpMM:DerivedFrom: adobe:docid:photoshop:801975cf-ad40-11d7-8d61-9b4a859da49b
    xmpMM:DocumentID: adobe:docid:photoshop:056bd0e6-9540-117c-b58e-ea137a7d995c
    xmpMM:InstanceID: xmp.iid:06801174072068118C149164CEB99850
    xmpMM:OriginalDocumentID: adobe:docid:photoshop:801975cf-ad40-11d7-8d61-9b4a859da49b
    xmpRights:Marked: False
  Clipping path:
<?xml version="1.0" encoding="iso-8859-1"?>
<svg xmlns="http://www.w3.org/2000/svg" width="3940" height="6000">
<g>
<path fill-rule="evenodd" style="fill:#000000;stroke:#000000;stroke-width:0;stroke-antialiasing:false" d="
M 209.461 337.174
C 216.952 315.665 232.825 293.191 241.874 284.81
C 250.849 276.503 367.892 147.3 367.892 147.3
C 367.892 147.3 377.163 138.177 388.734 133.95
C 400.305 129.796 446.382 128.331 461.364 128.999
C 476.125 129.889 3667.94 422.543 3667.94 422.543
C 3667.94 422.543 3703.17 427.364 3726.09 459.182
C 3749.82 491.743 3749.23 512.213 3749.23 512.213
L 3749.23 5461.09
C 3749.23 5461.09 3746.04 5490.01 3722.9 5514.93
C 3699.61 5539.71 3674.69 5544.82 3674.69 5544.82
L 449.258 5873.17
C 449.258 5873.17 415.065 5874.95 402.826 5872.43
C 390.366 5869.91 382.726 5865.75 378.795 5861.6
C 374.493 5857.45 222.756 5695.86 222.756 5695.86
C 222.756 5695.86 210.231 5678.39 207.041 5669.27
C 203.704 5660.07 203.74 5631.69 203.74 5631.69
L 203.74 378.847
C 203.74 378.847 202.044 358.683 209.461 337.174 Z
"/>
</g>
</svg>

  Profiles:
    Profile-8bim: 10440 bytes
    Profile-xmp: 16119510 bytes
  Artifacts:
    verbose: true
  Tainted: False
  Filesize: 105.563MiB
  Number pixels: 23640000
  Pixels per second: 21.1071MP
  User time: 1.110u
  Elapsed time: 0:02.120
  Version: ImageMagick 7.0.8-16 Q16 x86_64 2018-12-11 https://imagemagick.org
Post Reply