DEV Community

Igor Irianto
Igor Irianto

Posted on

Reducing a Screenshot Size in Mac

Motivation

I have a retina 15-inch Macbook (2016, used 🙂). Whenever I take a screenshot, it usually captures a PNG with 2-4MB in size.

Although storage is getting cheaper nowadays, for its purpose, having 2-4MB screenshots are, in my opinion, still overkill. It could be smaller. Especially if you are using these screenshots in an online storage (they cost $$!). If I can shrink a 2MB image by 90%, I can store ten times as many images!

In this article, I’ll share simple tips / tricks to reduce the size of my screenshot images. Even if you don’t use Mac, this article should largely be relevant to you - so keep reading!

ImageMagick

The main ingredient to accomplish this is a tool called ImageMagick

The quickest way to install is to use homebrew:

brew install imagemagick
Enter fullscreen mode Exit fullscreen mode

If you have a Windows or a Linux machine, or if you have Mac but want to download it outside of Homebrew, check out the ImageMagick's download page!

Converting Image With ImageMagick

ImageMagick has a long list of APIs. When you have the time, I strongly encourage you to read what else it can do.

By default, Macbook a full-screen screenshot has a size of roughly 2000 x 3000 px. Usually, half of that size is sufficient.

To reduce an image size, we can use ImageMagick's convert command. To convert an image by 50%, run:

convert MY-SCREENSHOT.png -resize 50% MY-RESIZED-SCREENSHOT.png
Enter fullscreen mode Exit fullscreen mode

The -resize 50% option will resize the input image by 50%.

Try it! Take a screenshot of the current window with Cmd + Shift + 4 then press Space. Click on the window that you want to take a screenshot on. In my case, the resulting screenshot is 2.1MB and has a dimension of 3548 x 2030. That's big!

Running the convert command above reduces the dimension to 1792 x 1015. The file size is also reduced down to about 430KB. That's a 80% reduction for a short one-line command!

Some of you may think 50% is too small - if that's the case, feel free to play around with the resize option. A good place to start is ImageMagick's resize documentation: Resize or Scaling (General Techniques).

PNGQuant

Reducing an image from 2MB to 400KB is already a significant improvement. However, we can do more. We can do a lossy compression of our image. This is a job for PNGQuant.

Although ImageMagick also performs an image compression, I prefer to use PNGQuant because it has a cleaner API and produces a better output than ImageMagick (disclaimer: I actually haven't spent that much time with ImageMagick's compression tools - take my word with a heavy grain of salt and experiment around with ImageMagick compression tools yourself!)

You can download PNGQuant with homebrew:

brew install pngquant
Enter fullscreen mode Exit fullscreen mode

Compressing with PNGQuant

By default, the screenshots taken by my Mac are PNG files, so it works right out of the box.

To compress an image, run:

pngquant MY-IMAGE.png
Enter fullscreen mode Exit fullscreen mode

I prefer to add a few extra safeties, so my preferred command is:

pngquant --skip-if-larger --force --ext=.png MY-IMAGE.png
Enter fullscreen mode Exit fullscreen mode
  • The option --skip-if-larger skips the image compression process if the resulting image is larger than the input image. I personally always run PNGQuant with this option.
  • The --ext=.png creates an image whose extension is a png. By default, PNGQuant creates an output file name ending with -fs8.png. This option prevents that.
  • The --force works in conjunction with --ext.
    • Without force, the command above will complain that the file name already exists.
    • With force, pngquant will overwrite the image with the compressed version.

The --force --ext=.png command technically mutates the image. If you prefer to keep the original copy of the image, just remove the --force --ext=.png options.

You can either run the PNGQuant command by itself against a freshly captured screenshot or against a resized screenshot. If you choose to run both the ImageMagick and PNGQuant commands together, run the ImageMagick command first. If you run the PNGQuant first followed by the ImageMagick command, you may end up with a less compressed image.

Putting Them Together and Creating a Bash Command

Inside my .bashrc / .zshrc / whatever, I created this mini utility function:

minimg() {
  echo "Resizing $1"
  convert $1 -resize 50% $1
  pngquant --skip-if-larger --force --ext=.png $1
  echo "Done!"
}
Enter fullscreen mode Exit fullscreen mode

Don't forget to save and source the file. With this, I can just run this to resize the image and compress it:

minimg MY-IMAGE.png
Enter fullscreen mode Exit fullscreen mode

On average, this reduces a screenshot image from 2MB to roughly 150KB, a 92.5% reduction. Not bad!

Extra: taking screenshot and resizing the image

If you prefer to run everything from the terminal, you can modify the function to include the screencapture command:

screencap() {
  echo "Screencapturing.."
  screenshot=$(which screencapture)
  $screenshot -i $1
  if test -f "$1"; then
    echo "Resizing $1"
    convert $1 -resize 50% $1
    pngquant --skip-if-larger --force --ext=.png $1
    echo "Done!"
  fi
}
Enter fullscreen mode Exit fullscreen mode

The screencapture command above is equivalent to Mac's Cmd + Shift + 4.

For example, to take a screencapture from CLI:

screencapture -i say-cheese.png
Enter fullscreen mode Exit fullscreen mode

After running this command, notice that your cursor turns into a screencapture crosshair (if you press Space in this mode, it turns into a window screen capture). With this function, you can take a screenshot, resize it, then compress it.

Conclusion

That's it. That’s how I resize my screenshots. I hope you're now able to take much lower resolution screenshots and make a better use of your local / cloud storage. More importantly, now that you have smaller screenshots, they will be easier to transport around. Plus you get to learn a little bit about image modification. I think it's a win-win.

Oldest comments (0)