13
submitted 4 days ago* (last edited 21 hours ago) by TheTwelveYearOld@lemmy.world to c/kde@lemmy.kde.social

Edit: I edited the script to take a screenshot asynchronously, get the region with slurp, and use magickto crop it. I also multiply the values from slurp 2x to account for the 200% display scaling I have.

  #!/bin/bash 

	die(){
    notify-send "$1"
    exit 1
  }
  cleanup(){
    [[ -n $1 ]] && rm -r "$1"
  }
  SCR_IMG=$(mktemp -d) || die "failed to take screenshot"
  trap "cleanup '$SCR_IMG'" EXIT

	spectacle -nbo "$SCR_IMG/scr.tiff" &
	region=($(slurp -b "#00000000" -c "#80808080" -w 2 -f "%w %h %x %y"))
	for i in "${!region[@]}" 
	do
		region[i]=$(expr ${region[i]} "*" "2")
	done
	magick "$SCR_IMG/scr.tiff" -crop "${region[0]}x${region[1]}+${region[2]}+${region[3]}" "$SCR_IMG/scr.tiff" 

	tesseract "$SCR_IMG/scr.tiff" "$SCR_IMG/scr" &> /dev/null || die "failed to extract text"
  wl-copy < "$SCR_IMG/scr.txt" || die "failed to copy text to clipboard"
  notify-send "Text extracted from image" "$(head -c 100 "$SCR_IMG/scr.txt")" || die "failed to send notification"
  exit

I'm using this script from HN* to select regions on the screen and copy their text, I took out the line with mogrify. It uses spectacle but it takes a moment before opening the UI, is it possible and would it be faster if Spectacle stayed open in the background? The slurp CLI starts instantly for me for selecting regions, I looked for command line screenshot tools to maybe use with it or has its own region support but didn't find any. Neither maim scrot and grim don't work on Plasma Wayland. I installed the ksnip flatpak but the option for rectangular regions doesn't show for me.

* The script:

  #!/bin/bash 
  # Dependencies: tesseract-ocr imagemagick 
  # on gnome: gnome-screenshot 
  # on kde: spectacle
  # on x11: xsel
  # on wayland: wl-clipboard

  die(){
    notify-send "$1"
    exit 1
  }
  cleanup(){
    [[ -n $1 ]] && rm -r "$1"
  }

  SCR_IMG=$(mktemp -d) || die "failed to take screenshot"

  # shellcheck disable=SC2064
  trap "cleanup '$SCR_IMG'" EXIT

  #notify-send "Select the area of the text" 
  if  which "spectacle" &> /dev/null
  then
    spectacle -n -b -r -o "$SCR_IMG/scr.png" || die "failed to take screenshot"
  else
    gnome-screenshot -a -f "$SCR_IMG/scr.png" || die "failed to take screenshot"
  fi

  # increase image quality with option -q from default 75 to 100
  mogrify -modulate 100,0 -resize 400% "$SCR_IMG/scr.png"  || die "failed to convert image"
  #should increase detection rate

  tesseract "$SCR_IMG/scr.png" "$SCR_IMG/scr" &> /dev/null || die "failed to extract text"
  if [ "$XDG_SESSION_TYPE" == "wayland" ]
  then 
    wl-copy < "$SCR_IMG/scr.txt" || die "failed to copy text to clipboard"
  else
    # xsel -b -i  < "$SCR_IMG/scr.txt" || die "failed to copy text to clipboard"
    xclip -selection clipboard -i < "$SCR_IMG/scr.txt" || die "failed to copy text to clipboard"  
  fi
  # Notify the user what was copied but truncate the text to 100 characters
  notify-send "Text extracted from image" "$(head -c 100 "$SCR_IMG/scr.txt")" || die "failed to send notification"
  exit  #!/bin/bash 
  # Dependencies: tesseract-ocr imagemagick 
  # on gnome: gnome-screenshot 
  # on kde: spectacle
  # on x11: xsel
  # on wayland: wl-clipboard

  die(){
    notify-send "$1"
    exit 1
  }
  cleanup(){
    [[ -n $1 ]] && rm -r "$1"
  }

  SCR_IMG=$(mktemp -d) || die "failed to take screenshot"

  # shellcheck disable=SC2064
  trap "cleanup '$SCR_IMG'" EXIT

  #notify-send "Select the area of the text" 
  if  which "spectacle" &> /dev/null
  then
    spectacle -n -b -r -o "$SCR_IMG/scr.png" || die "failed to take screenshot"
  else
    gnome-screenshot -a -f "$SCR_IMG/scr.png" || die "failed to take screenshot"
  fi

  # increase image quality with option -q from default 75 to 100
  mogrify -modulate 100,0 -resize 400% "$SCR_IMG/scr.png"  || die "failed to convert image"
  #should increase detection rate

  tesseract "$SCR_IMG/scr.png" "$SCR_IMG/scr" &> /dev/null || die "failed to extract text"
  if [ "$XDG_SESSION_TYPE" == "wayland" ]
  then 
    wl-copy < "$SCR_IMG/scr.txt" || die "failed to copy text to clipboard"
  else
    # xsel -b -i  < "$SCR_IMG/scr.txt" || die "failed to copy text to clipboard"
    xclip -selection clipboard -i < "$SCR_IMG/scr.txt" || die "failed to copy text to clipboard"  
  fi
  # Notify the user what was copied but truncate the text to 100 characters
  notify-send "Text extracted from image" "$(head -c 100 "$SCR_IMG/scr.txt")" || die "failed to send notification"
  exit
you are viewing a single comment's thread
view the rest of the comments
[-] TheTwelveYearOld@lemmy.world 0 points 4 days ago

lmao, thanks for trying to help though.

[-] naeap@sopuli.xyz 2 points 4 days ago

Yeah, sorry...
Was just skimming over your post and immediately after sending it, I read the Wayland part and that you already tried scrot

Well yeah, I guess the intention counts? ;⁠-⁠)

(Down votes didn't came from me btw)

[-] TheTwelveYearOld@lemmy.world 2 points 21 hours ago

I solved it on my own. I edited the post to include the code, basically I just run spectacle asynchronously so slurp immediately opens and then crops the image after I make a sleection.

[-] naeap@sopuli.xyz 2 points 20 hours ago

Thanks!
Probably good to know, if/when I switch to wayland

this post was submitted on 28 Mar 2025
13 points (88.2% liked)

KDE

6253 readers
162 users here now

KDE is an international technology team creating user-friendly free and open source software for desktop and portable computing. KDE’s software runs on GNU/Linux, BSD and other operating systems, including Windows.

Plasma 6 Bugs

If you encounter a bug, proceed to https://bugs.kde.org, check whether it has been reported.

If it hasn't, report it yourself.

PLEASE THINK CAREFULLY BEFORE POSTING HERE.

Developers do not look for reports on social media, so they will not see it and all it does is clutter up the feed.

founded 2 years ago
MODERATORS