image-processingimagemagickexiftoolhigh-resolution

Detect high-resolution images


If pasting a high-resolution image, for example, a macOS screenshot, on GitHub, it is shown in the correct size. But if pasting the same image on Stack Overflow, it is larger than it should be (macOS screenshots, for example, are x2 larger, but other can be, for example, x1.5 or x3 larger). This issue is very well described here: https://meta.stackexchange.com/q/161111.

From this I make a conclusion that high-resolution images can be detected (that is, distinguished from non-high-resolution ones) somehow.

Hence my question: Is it possible to detect them by using ImageMagick's magick identify or maybe ExifTool?

Test image: https://github.com/jsx97/test/blob/main/macos-screenshot.zip


Solution

  • An attempt to answer my own question, bases on the comment by Martin Brown:

    Based on your sample test image the problem is that it has dpi 144 but plenty of rendering engines ignore that field so that it gets rendered at 72 dpi. And so is 2x bigger. It looks to me like an acceptable fix might be if image_dpi>72 then rescale its dimensions by a factor 72/image_dpi and then set dpi to 72.

    input=input.png
    output=output.png
    
    densityX=$(magick identify -format %x $input)
    densityY=$(magick identify -format %y $input)
    
    if (( densityX > 72 || densityY > 72 )); then
      width=$(magick identify -format %w $input)
      height=$(magick identify -format %h $input)
    
      width=$(( width * 72/densityX ))
      height=$(( height * 72/densityY ))
    
      magick $input -resize ${width}x${height} -units PixelsPerInch -density 72 -sharpen 0x1 $output
    fi