Skip to content

ImageMagick, Ghostscript, poppler, jhead tips and tricks

Michael Hulse edited this page Jul 18, 2017 · 9 revisions

Some command line stuffs

Get info about a PDF:

$ brew install poppler
$ pdfimages -list foo.pdf
page   num  type   width height color comp bpc  enc interp  object ID x-ppi y-ppi size ratio
--------------------------------------------------------------------------------------------
   1     0 image    2432  2592  cmyk    4   8  jpeg   no        15  0   297   297 3493K  14%
   1     1 image    1490   583  cmyk    4   8  jpeg   no        16  0   297   297  278K 8.2%
   1     2 image     584   107  cmyk    4   8  jpeg   no        17  0   297   297 60.2K  25%
   1     3 image     928   119  cmyk    4   8  jpeg   no        18  0   297   297 36.2K 8.4%
   1     4 image    1314   195  index   1   8  image  no        19  0  1011    23  137K  55%
   1     5 image    1312   195  index   1   8  image  no        20  0  1010    23 91.3K  37%
   1     6 image     192  2594  cmyk    4   8  jpeg   no        21  0   297   297 75.9K 3.9%

Fix image orientations:

# http://stackoverflow.com/a/9130900/922323
# jhead is lossless:
$ brew install jhead
# The following works best as a method:
$ shopt -u | grep -q nullglob && changed=true && shopt -s nullglob \
$ jhead -autorot -q -se ${args.inputFile} 2> /dev/null \
$ [ $changed ] && shopt -u nullglob; unset changed

Optimize pdf with Ghostscript:

# http://stackoverflow.com/a/10453202/922323
# http://superuser.com/a/373740/201992
# http://superuser.com/a/54043/201992
# http://superuser.com/a/440573/201992

# Get info on options for Ghostscript:
$ gs -sDEVICE=pdfwrite -o /dev/null -c "currentpagedevice { exch ==only ( ) print == } forall"
$ gs -q -dNODISPLAY -c ".distillersettings /prepress get {exch ==only ( ) print ===} forall quit" | sort

# /AutoRotatePages /None
# /CannotEmbedFontPolicy /Error
# /ColorACSImageDict << /ColorTransform 1 /QFactor 0.15 /Blend 1 /HSamples [1 1 1 1] /VSamples [1 1 1 1] >>
# /ColorConversionStrategy /LeaveColorUnchanged
# /ColorImageDownsampleType /Bicubic
# /ColorImageResolution 300
# /CompatibilityLevel 1.5
# /CreateJobTicket true
# /DoThumbnails true
# /EmbedAllFonts true
# /GrayACSImageDict << /ColorTransform 1 /QFactor 0.15 /Blend 1 /HSamples [1 1 1 1] /VSamples [1 1 1 1] >>
# /GrayImageDownsampleType /Bicubic
# /GrayImageResolution 300
# /MonoImageDownsampleType /Subsample
# /MonoImageResolution 1200
# /NeverEmbed []
# /PreserveEPSInfo true
# /PreserveOPIComments true
# /PreserveOverprintSettings true
# /UCRandBGInfo /Preserve

mv input.pdf original.pdf \
&& \
gs \
-dNOPAUSE \
-dBATCH \
-dQUIET \
-dSAFER \
-sDEVICE=pdfwrite \
-dAutoRotatePages=/None \
-dColorConversionStrategy=/LeaveColorUnchanged \
-dColorImageResolution=304 \
-dColorImageDownsampleType=/Bicubic \
-dGrayImageResolution=304 \
-dGrayImageDownsampleType=/Bicubic \
-dMonoImageResolution=1200 \
-dMonoImageDownsampleType=/Subsample \
-dCompatibilityLevel=1.3 \
-dDoThumbnails=true \
-dPreserveOverprintSettings=true \
-sOutputFile=input.pdf \
original.pdf 2> /dev/null

Get details on an image (like, does it have a color profile, etc.):

$ brew install imagemagick
$ identify -verbose 001.jpg
Image: 001.jpg
  Format: JPEG (Joint Photographic Experts Group JFIF format)
  Mime type: image/jpeg
  Class: DirectClass
  Geometry: 2622x2660+0+0
  Resolution: 304x304
  Print size: 8.625x8.75
  Units: PixelsPerInch
  Type: TrueColor
  Endianess: Undefined
  Colorspace: sRGB
  Depth: 8-bit
  Channel depth:
    red: 8-bit
    green: 8-bit
    blue: 8-bit
  Channel statistics:
    Pixels: 6974520
    Red:
      min: 0 (0)
      max: 255 (1)
      mean: 66.0924 (0.259186)
      standard deviation: 80.1442 (0.314291)
      kurtosis: -0.480609
      skewness: 1.06344
      entropy: 0.854328
    Green:
      min: 0 (0)
      max: 237 (0.929412)
      mean: 65.161 (0.255533)
      standard deviation: 71.1905 (0.279179)
      kurtosis: -0.307179
      skewness: 1.03909
      entropy: 0.891357
    Blue:
      min: 0 (0)
      max: 235 (0.921569)
      mean: 48.2611 (0.189259)
      standard deviation: 69.4196 (0.272234)
      kurtosis: 0.482291
      skewness: 1.44934
      entropy: 0.804214
  Image statistics:
    Overall:
      min: 0 (0)
      max: 255 (1)
      mean: 59.8381 (0.234659)
      standard deviation: 73.7343 (0.289154)
      kurtosis: -0.0739547
      skewness: 1.19747
      entropy: 0.849966
  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)
  Background color: white
  Border color: srgb(223,223,223)
  Matte color: grey74
  Transparent color: black
  Interlace: None
  Intensity: Undefined
  Compose: Over
  Page geometry: 2622x2660+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: JPEG
  Quality: 100
  Orientation: Undefined
  Properties:
    date:create: 2016-11-30T21:57:24-08:00
    date:modify: 2016-11-30T21:57:24-08:00
    jpeg:colorspace: 2
    jpeg:sampling-factor: 1x1,1x1,1x1
    signature: c90922581b5720de5aecdb71f49fbad570f5ab38b53998b97333e0eabf0bbb87
  Artifacts:
    filename: 001.jpg
    verbose: true
  Tainted: False
  Filesize: 3.566MB
  Number pixels: 6.975M
  Pixels per second: 41.03MB
  User time: 0.170u
  Elapsed time: 0:01.170
  Version: ImageMagick 6.9.6-5 Q16 x86_64 2016-11-15 http://www.imagemagick.org

Read EXIF info:

$ identify -format '%[EXIF:*]' 010.jpg
exif:ApertureValue=7400879/1000000
exif:CustomRendered=0
exif:DateTime=2012:01:17 20:00:22
exif:DateTimeDigitized=2012:01:07 14:37:44
exif:DateTimeOriginal=2012:01:07 14:37:44
exif:ExifOffset=216
exif:ExifVersion=48, 50, 51, 48
exif:ExposureBiasValue=0/1
exif:ExposureMode=0
exif:ExposureProgram=3
exif:ExposureTime=1/40
exif:Flash=16
exif:FNumber=13/1
exif:FocalLength=70/1
exif:FocalPlaneResolutionUnit=2
exif:FocalPlaneXResolution=5616000/1459
exif:FocalPlaneYResolution=3744000/958
exif:ISOSpeedRatings=100
exif:Make=Canon
exif:MaxApertureValue=4/1
exif:MeteringMode=5
exif:Model=Canon EOS 5D Mark II
exif:ResolutionUnit=2
exif:SceneCaptureType=0
exif:ShutterSpeedValue=5321928/1000000
exif:Software=Adobe Photoshop Lightroom 3.6 (Macintosh)
exif:SubjectDistance=126/10
exif:SubSecTimeDigitized=70
exif:SubSecTimeOriginal=70
exif:thumbnail:Compression=6
exif:thumbnail:JPEGInterchangeFormat=828
exif:thumbnail:JPEGInterchangeFormatLength=22426
exif:thumbnail:ResolutionUnit=2
exif:thumbnail:XResolution=72/1
exif:thumbnail:YResolution=72/1
exif:WhiteBalance=0
exif:XResolution=304/1
exif:YResolution=304/1

Short version to get properties:

$ identify -ping test.jpg
test.jpg JPEG 2100x1500 2100x1500+0+0 8-bit sRGB 222KB 0.000u 0:00.009

Using Exiftool:

$ exiftool -a -u -g1 test.jpg
---- ExifTool ----
ExifTool Version Number         : 10.36
---- System ----
File Name                       : test.jpg
Directory                       : .
File Size                       : 217 kB
File Modification Date/Time     : 2016:12:24 12:07:52-08:00
File Access Date/Time           : 2016:12:24 12:45:18-08:00
File Inode Change Date/Time     : 2016:12:24 12:08:13-08:00
File Permissions                : rw-r--r--
---- File ----
File Type                       : JPEG
File Type Extension             : jpg
MIME Type                       : image/jpeg
Exif Byte Order                 : Big-endian (Motorola, MM)
Image Width                     : 2100
Image Height                    : 1500
Encoding Process                : Baseline DCT, Huffman coding
Bits Per Sample                 : 8
Color Components                : 3
Y Cb Cr Sub Sampling            : YCbCr4:4:4 (1 1)
---- IFD0 ----
Orientation                     : Horizontal (normal)
X Resolution                    : 300
Y Resolution                    : 300
Resolution Unit                 : inches
Software                        : Adobe Photoshop CC 2017 (Macintosh)
Modify Date                     : 2016:12:24 12:07:49
---- ExifIFD ----
Color Space                     : sRGB
Exif Image Width                : 2100
Exif Image Height               : 1500
---- IFD1 ----
Compression                     : JPEG (old-style)
X Resolution                    : 72
Y Resolution                    : 72
Resolution Unit                 : inches
Thumbnail Offset                : 322
Thumbnail Length                : 920
---- Photoshop ----
IPTC Digest                     : 00000000000000000000000000000000
Print Info 2                    : ...printOutput.PstSbool.InteenumInteClrm.printSixteenBitbool.printerNameTEXT..printProofSetupObjc.Proof Setup.proofSetup.Bltnenum.builtinProof.proofCMYK
Print Style                     : ...printOutputOptions.CptnboolClbrboolRgsMboolCrnCboolCntCboolLblsboolNgtvboolEmlDboolIntrboolBckgObjc.RGBC.Rd  doub@o�Grn doub@o�Bl  doub@o�BrdTUntF#RltBld UntF#RltRsltUntF#Pxl@r�.vectorDatabool.PgPsenumPgPsPgPCLeftUntF#RltTop UntF#RltScl UntF#Prc@Y.cropWhenPrintingbool.cropRectBottomlong.cropRectLeftlong.cropRectRightlong.cropRectToplong
X Resolution                    : 300
Displayed Units X               : inches
Y Resolution                    : 300
Displayed Units Y               : inches
Print Style                     : Centered
Print Position                  : 0 0
Print Scale                     : 1
Global Angle                    : 90
Global Altitude                 : 30
Print Flags                     : 0 0 0 0 0 0 0 0 1
Print Flags Info                : ..
Color Halftoning Info           : /ff.lff../ff.���..2.Z..5.-..
Color Transfer Funcs            : ����������������������.�����������������������.�����������������������.�����������������������.�
Grid Guides Info                : ..@.@
URL List                        :
Slices Group Name               : test
Num Slices                      : 1
Pixel Aspect Ratio              : 1
IDs Base Value                  : 1
Photoshop Thumbnail             : (Binary data 920 bytes, use -b option to extract)
Has Real Merged Data            : Yes
Writer Name                     : Adobe Photoshop
Reader Name                     : Adobe Photoshop CC 2017
Photoshop Quality               : 12
Photoshop Format                : Standard
Progressive Scans               : 3 Scans
---- XMP-x ----
XMP Toolkit                     : Adobe XMP Core 5.6-c138 79.159824, 2016/09/14-01:09:01
---- XMP-xmp ----
Creator Tool                    : Adobe Photoshop CC 2017 (Macintosh)
Create Date                     : 2016:12:24 12:07:49-08:00
Metadata Date                   : 2016:12:24 12:07:49-08:00
Modify Date                     : 2016:12:24 12:07:49-08:00
---- XMP-dc ----
Format                          : image/jpeg
---- XMP-xmpMM ----
Instance ID                     : xmp.iid:421642fc-b721-4e31-b57f-74ac6f0c4a8e
Document ID                     : xmp.did:421642fc-b721-4e31-b57f-74ac6f0c4a8e
Original Document ID            : xmp.did:421642fc-b721-4e31-b57f-74ac6f0c4a8e
History Action                  : created
History Instance ID             : xmp.iid:421642fc-b721-4e31-b57f-74ac6f0c4a8e
History When                    : 2016:12:24 12:07:49-08:00
History Software Agent          : Adobe Photoshop CC 2017 (Macintosh)
---- XMP-photoshop ----
Color Mode                      : RGB
ICC Profile Name                : sRGB IEC61966-2.1
---- ICC-header ----
Profile CMM Type                : Lino
Profile Version                 : 2.1.0
Profile Class                   : Display Device Profile
Color Space Data                : RGB
Profile Connection Space        : XYZ
Profile Date Time               : 1998:02:09 06:49:00
Profile File Signature          : acsp
Primary Platform                : Microsoft Corporation
CMM Flags                       : Not Embedded, Independent
Device Manufacturer             : IEC
Device Model                    : sRGB
Device Attributes               : Reflective, Glossy, Positive, Color
Rendering Intent                : Media-Relative Colorimetric
Connection Space Illuminant     : 0.9642 1 0.82491
Profile Creator                 : HP
Profile ID                      : 0
---- ICC_Profile ----
Profile Copyright               : Copyright (c) 1998 Hewlett-Packard Company
Profile Description             : sRGB IEC61966-2.1
Media White Point               : 0.95045 1 1.08905
Media Black Point               : 0 0 0
Red Matrix Column               : 0.43607 0.22249 0.01392
Green Matrix Column             : 0.38515 0.71687 0.09708
Blue Matrix Column              : 0.14307 0.06061 0.7141
Device Mfg Desc                 : IEC http://www.iec.ch
Device Model Desc               : IEC 61966-2.1 Default RGB colour space - sRGB
Viewing Cond Desc               : Reference Viewing Condition in IEC61966-2.1
Luminance                       : 76.03647 80 87.12462
Technology                      : Cathode Ray Tube Display
Red Tone Reproduction Curve     : (Binary data 2060 bytes, use -b option to extract)
Green Tone Reproduction Curve   : (Binary data 2060 bytes, use -b option to extract)
Blue Tone Reproduction Curve    : (Binary data 2060 bytes, use -b option to extract)
---- ICC-view ----
Viewing Cond Illuminant         : 19.6445 20.3718 16.8089
Viewing Cond Surround           : 3.92889 4.07439 3.36179
Viewing Cond Illuminant Type    : D50
---- ICC-meas ----
Measurement Observer            : CIE 1931
Measurement Backing             : 0 0 0
Measurement Geometry            : Unknown
Measurement Flare               : 0.999%
Measurement Illuminant          : D65
---- Adobe ----
DCT Encode Version              : 100
APP14 Flags 0                   : [14]
APP14 Flags 1                   : (none)
Color Transform                 : YCbCr
---- Composite ----
Image Size                      : 2100x1500
Megapixels                      : 3.1
Thumbnail Image                 : (Binary data 920 bytes, use -b option to extract)

Another tool, mdls:

$ mdls test.jpg
_kMDItemOwnerUserID            = 501
kMDItemBitsPerSample           = 32
kMDItemColorSpace              = "RGB"
kMDItemContentCreationDate     = 2016-12-24 20:07:49 +0000
kMDItemContentModificationDate = 2016-12-24 20:07:49 +0000
kMDItemContentType             = "public.jpeg"
kMDItemContentTypeTree         = (
    "public.jpeg",
    "public.item",
    "public.data",
    "public.image",
    "public.jpeg",
    "public.content"
)
kMDItemCreator                 = "Adobe Photoshop CC 2017 (Macintosh)"
kMDItemDateAdded               = 2016-12-24 20:07:52 +0000
kMDItemDisplayName             = "test.jpg"
kMDItemFinderComment           = "Hello world!"
kMDItemFSContentChangeDate     = 2016-12-24 20:07:52 +0000
kMDItemFSCreationDate          = 2016-12-24 20:07:52 +0000
kMDItemFSCreatorCode           = "8BIM"
kMDItemFSFinderFlags           = 0
kMDItemFSHasCustomIcon         = (null)
kMDItemFSInvisible             = 0
kMDItemFSIsExtensionHidden     = 0
kMDItemFSIsStationery          = (null)
kMDItemFSLabel                 = 0
kMDItemFSName                  = "test.jpg"
kMDItemFSNodeCount             = (null)
kMDItemFSOwnerGroupID          = 20
kMDItemFSOwnerUserID           = 501
kMDItemFSSize                  = 222341
kMDItemFSTypeCode              = "JPEG"
kMDItemHasAlphaChannel         = 0
kMDItemKind                    = "JPEG image"
kMDItemLastUsedDate            = 2016-12-24 20:24:09 +0000
kMDItemLogicalSize             = 222341
kMDItemOrientation             = 0
kMDItemPhysicalSize            = 225280
kMDItemPixelCount              = 3150000
kMDItemPixelHeight             = 1500
kMDItemPixelWidth              = 2100
kMDItemProfileName             = "sRGB IEC61966-2.1"
kMDItemResolutionHeightDPI     = 300
kMDItemResolutionWidthDPI      = 300
kMDItemUseCount                = 1
kMDItemUsedDates               = (
    "2016-12-24 08:00:00 +0000"
)

Just get the "Comments" section:

$ mdls -raw -name kMDItemFinderComment test.jpg

Scale a PDF using pdfScale:

# LuLu A5, including trim:
pdfscale -v -v -r 'custom in 5.955 8.515' -f disable -s 1.1 foo.pdf
Clone this wiki locally