Logo

dev-resources.site

for different kinds of informations.

Reducing a Screenshot Size in Mac

Published at
8/11/2021
Categories
screenshot
image
imagemagick
png
Author
iggredible
Categories
4 categories in total
screenshot
open
image
open
imagemagick
open
png
open
Author
10 person written this
iggredible
open
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.

screenshot Article's
30 articles in total
Favicon
From 80 to 8000/m: A Journey of SEO Optimization (Part 1)
Favicon
CodeSnap : prendre des captures d'รฉcran de code dans VS Code
Favicon
rails system test, save failed screenshots
Favicon
๐Ÿš€ ๐Ÿ“ธ Creating Accessible and Stunning code screenshots
Favicon
AVIF Studio - Web page screen capture Chrome extension Made with Svelte and WebAssembly.
Favicon
Edge: Screenshots einer Seite erstellen ohne Addons
Favicon
Web Scraping Using Image Processing
Favicon
Edge: Create screenshots of a page without addons
Favicon
How to Perform Screenshot Comparison in Playwright
Favicon
How to take screenshots effectively on windows 11
Favicon
How to screenshot webpages in Golang
Favicon
Screenshot all your pages
Favicon
CodeSnap: Take Code Screenshots In VS Code
Favicon
Rendering NativeScript Angular Templates and Components into images
Favicon
How to run a code in editor in Atom IDE
Favicon
Take a Full-Page Screenshot in Browser (without extension or add-on)
Favicon
Tomar capturas de pantalla facilmente en i3wm
Favicon
Building A Serverless Screenshot Service with Lambda
Favicon
Android - How to do Screenshot Testing in Jetpack Compose
Favicon
How to capture a screenshot of the single window you want (using command line).
Favicon
How to Scan QR Code from Desktop Screen in Python
Favicon
react-native detect when user takes a screenshot
Favicon
Set Flameshot as default screenshot app on Ubuntu :)
Favicon
How to capture Screenshot within the browser
Favicon
Capture Website Screenshots with Python
Favicon
Reducing a Screenshot Size in Mac
Favicon
Employee Monitoring Tool to improve employee productivity
Favicon
Configuring screenshots in Mac
Favicon
How to take a screenshot of Jira kanban
Favicon
How To Capture Screenshots In Selenium? Guide With Examples

Featured ones: