5

Since I switched to a High DPI display, the screenshots I post are awkwardly big, example below.

Is there a way to quickly make them look normal?
Preferably something faster than firing up GIMP.

In particular, is there a hidden option for this in gnome-screenshot?

enter image description here

  • There is probably no perfect tool, since the way to adapt Ubuntu to High DPI is not uniform (fonts, window decorations, programs, might have different scales), but a good tool would read these settings and figure out what's the best scale to use.
  • Preferably I would like to keep the ability of selecting only part of the screen (as in SHIFT+PRTSCR), and choosing the name of the file.
  • In case that matters, I always save my screenshots in $HOME/Pictures.
4
  • I have to run, but are your screenshots always saved in Pictures? Jan 8, 2016 at 9:28
  • @JacobVlijm: Yes. Jan 8, 2016 at 9:33
  • I have the exact opposite problem. I want to make a full res screenshot of my UHD monitor, but gnome-screenshot automatically saves half-size images. How do I prevent this?
    – Redsandro
    May 16, 2016 at 13:50
  • @Redsandro: Could you please ask as a new (separate) question? Cheers! May 17, 2016 at 4:21

2 Answers 2

4

1. Make a screenshot as usual, then automatically scale the latest screenshot you took with a shortcut key.

Placed under a shortcut key, the script below will:

  1. Find the last added screenshot in your screenshot directory (which is ~/Picures, as you mentioned in a comment)
  2. Scale the image into an arbitrary percentage
  3. Rename and save the image as renamed_filename.png, where filename.png is the original filename.

How to use

  1. The script needs the python3-pil library to be installed, which might not be the case on your system:

    sudo apt-get install python3-pil
    
  2. Copy the script below into an empty file, save it as resize_screenshot.py

  3. Test- run the script by taking a screenshot and subsequently run the script by the command:

    python3 /path/to/resize_screenshot.py 80
    

    where 80 is the desired output size percentage. The script now created the resized copy of the last screenshot.

  4. If all works fine, add it to a shortcut key: System Settings > Keyboard > Shortcuts > Custom SHortcuts. Add the command:

    python3 /path/to/resize_screenshot.py 80
    

The script

#!/usr/bin/env python3
import os
import sys
from PIL import Image

percent = float(sys.argv[1])/100


pic_list = []
# list all .png files in ~/Pictures
pic_dir = os.environ["HOME"]+"/Pictures"
files = [pic_dir+"/"+f for f in os.listdir(pic_dir) if \
         all([f.endswith(".png"), not f.startswith("resized")])]
# create a sorted list + the creation date of relevant files
pics = [[f, int(os.stat(f).st_ctime)] for f in files]
pics.sort(key=lambda x: x[1])
# choose the latest one
resize = pics[-1][0]
# open the image, look up its current size
im = Image.open(resize)
size = im.size
# define the new size; current size * the percentage
newsize = [int(n * percent) for n in size]
# resize the image, save it as renamed file (keeping original)
im.thumbnail(newsize, Image.ANTIALIAS)
newfile = pic_dir+"/resized_"+resize.split("/")[-1]
im.save(newfile, "png")

An example

An example of your image, resized by:

python3 <script> 80

enter image description here


2. Fully automatic option

While the script above does its job on a shortcut key, you can make it fully automatically with a background script. All thye script does is check for new files in ~/Picures, and perform the rescale action as in the first script.

The script

#!/usr/bin/env python3
import os
import sys
from PIL import Image
import time

percent = float(sys.argv[1])/100
pic_dir = os.environ["HOME"]+"/Pictures"

def pics_list(dr):
    return [pic_dir+"/"+f for f in os.listdir(pic_dir) if \
            all([f.endswith(".png"), not f.startswith("resized")])]

def scale(f):
    #open the image, look up its current size
    im = Image.open(f)
    size = im.size
    # define the new size; current size * the percentage
    newsize = [int(n * percent) for n in size]
    # resize the image, save it as renamed file (keeping original)
    im.thumbnail(newsize, Image.ANTIALIAS)
    newfile = pic_dir+"/resized_"+f.split("/")[-1]
    im.save(newfile, "png")

p_list1 = pics_list(pic_dir)
while True:
    time.sleep(2)
    p_list2 = pics_list(pic_dir)
    for item in p_list2:
        if not item in p_list1:
            scale(item)
    p_list1 = p_list2

How to use

The setup is exactly as the script above ("How to use"), but instead of [4.], add it to Startup Applications: Dash > Startup Applications > Add. Add the command:

python3 /path/to/resize_screenshot.py 80

3. Fully automatic option with a scale dialogue

Practically the same script, but now with a scale dialogue, immediately after you saved the image to ~/Pictures:

enter image description here

This screenshot was automatically resized to 80% :)

The script

#!/usr/bin/env python3
import os
import sys
from PIL import Image
import time
import subprocess

# --- change if you like the default scale percentage, as proposed by the slider:
default_percent = 80
# --- change if you like the screenshot directory
pic_dir = os.environ["HOME"]+"/Pictures"
# ---

def pics_list(dr):
    return [pic_dir+"/"+f for f in os.listdir(pic_dir) if \
            all([f.endswith(".png"), not f.startswith("resized")])]

def scale(f, size):
    #open the image, look up its current size
    im = Image.open(f)
    currsize = im.size
    # define the new size; current size * the percentage
    newsize = [int(n * size) for n in currsize]
    # resize the image, save it as renamed file (keeping original)
    im.thumbnail(newsize, Image.ANTIALIAS)
    newfile = pic_dir+"/resized_"+f.split("/")[-1]
    im.save(newfile, "png")

p_list1 = pics_list(pic_dir)
while True:
    time.sleep(2)
    p_list2 = pics_list(pic_dir)
    for item in p_list2:
        if not item in p_list1:
            try:
                size = subprocess.check_output([
                    "zenity", "--scale",
                    "--value="+str(default_percent),
                    ]).decode("utf-8")
                scale(item, float(size)/100)
            except subprocess.CalledProcessError:
                pass
    p_list1 = p_list2

To use

Setup is exactly as above, apart from the command, now without scale percentage:

python3 /path/to/resize_screenshot.py

Note

As always, the background script practically uses no resources, unless your ~/Pictures directory is insanely huge :).

2

With gnome-screenshot we can not scale the output.

To do so automatically we may assign another screenshot terminal application to a shortcut.

Examples for screenshots at 50% size:

  1. Image Magick Install imagemagick import -resize to percent of original
  • take screenshot of the whole desktop:

         import -window root -resize 50% [-delay <value>] shot.png
    
  • take screenshot of a window (selectable by mouse):

         import -window $(xdotool selectwindow) resize 50% shot.png
    
  • take screenshot of a window and display result:

         import -window $(xdotool selectwindow) -resize 50% shot.png && display shot.png
    
  • load screenshot to any other external viewer (e.g. eog)

         import -window $(xdotool selectwindow) -resize 50% shot.png && eog shot.png
    
  1. scrot Install scrot (manpage) output to Image Magic convert for selecting a screen area

     scrot <options> -e "convert \$f -resize 50% shot.png && rm \$f"
     <options>
     -s select window or rectangle with mouse
     -u use currently focused windows
    

Below example will display, then save a half-sized (50%) screenshot of a selected area or window using scrot's default filename (date/hour/minute/second/size) in our pictures directory:

    scrot -s -e "convert \$f -resize 50% ~/Pictures/\$f && display \$f && rm \$f"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .