Selaa lähdekoodia

Scan to PNG and convert after

This allows more control over jpg quality, rather than the `scanimage`
default of 75.
cinaeco 6 kuukautta sitten
vanhempi
commit
f0faac9f08
2 muutettua tiedostoa jossa 56 lisäystä ja 26 poistoa
  1. 6 0
      README.md
  2. 50 26
      scan-dialog

+ 6 - 0
README.md

@@ -0,0 +1,6 @@
+# Scan Dialog
+
+Requires:
+
+- SANE `scanimage`
+- Imagemagick `convert`

+ 50 - 26
scan-dialog

@@ -19,13 +19,17 @@ function check_filename {
   fi
   echo "$folder/$filename.$extension"
 }
-export -f check_filename # Make function available to run_scan's command substitution
+export -f check_filename # Make available to run_scan's command substitution
 
+# positional arguments: 1-folder 2-prefix 3-format 4-papersize 5-resolution 6-colormode
 function run_scan {
-  x=210 # A4 default size
-  y=297
 
-  case $2a in
+  # Paper size.
+  case $4 in
+  A4)
+    x=210
+    y=297
+    ;;
   A5)
     x=148
     y=210
@@ -36,35 +40,55 @@ function run_scan {
     ;;
   esac
 
-  filename=$(check_filename $1)
+  filename=$(check_filename $1/$2.$3)
 
-  echo "$5 $4 $3 $x $y $filename"
+  echo "$6 $5 $x $y $filename"
 
-  scanimage --mode=$5 --format=$4 --resolution=$3 -x $x -y $y -p --output-file=$filename | \
-  yad --progress --text="Scanning..." --width=300 --no-buttons --auto-close
-}
-export -f run_scan # Make function available to yad button command.
+  tempname="./temp-$(date +%s).png"
+  scanimage --mode=$6 --resolution=$5 -x $x -y $y --format=png --output-file=$tempname | \
+  yad --progress --text="Scanning to PNG..." --width=300 --no-buttons --auto-close
 
-function update_name {
-  if [[ $1 -eq 4 ]]; then
-    filenamecheck_filename $2
-  fi
+  case $3 in
+    png)
+      mv $tempname $filename
+      ;;
+    jpg)
+      convert -quality 85 $tempname $filename | \
+      yad --progress --text="Converting to $3..." --width=300 --no-buttons --auto-close
+      rm $tempname $tempname2
+      ;;
+    pdf)
+      if [[ $6 != "Lineart" ]] then
+        tempname2="${tempname%.png}.jpg"
+        convert -quality 85 $tempname $tempname2 | \
+        yad --progress --text="Converting to jpg..." --width=300 --no-buttons --auto-close
+        rm $tempname
+        tempname=$tempname2
+      fi
+      convert $tempname $filename | \
+      yad --progress --text="Converting to $3..." --width=300 --no-buttons --auto-close
+      rm $tempname
+      ;;
+  esac
 }
+export -f run_scan # Make function available to yad button command.
 
 # Display scan-dialog.
-yad --form --orient=hor --width=750 --height=250 --columns=3 \
+yad --form --orient=hor --maximized --width=750 --height=250 --columns=3 \
   --title="Scanning Options" \
-  --field="Filename:SFL" /mnt/public/scan.jpg \
+  --field="Scan Folder:SFL" /mnt/public \
+  --field="Filename Prefix:SFL" scan \
+  --field="File format:CB" jpg\!png\!pdf \
   --field="Paper Size:CB" A4\!A5\!A5R \
-  --field="Resolution (DPI):CB" 100\!^150\!300 \
-  --field="File format:CB" jpeg\!png\!pdf \
+  --field="Resolution (DPI):CB" 75\!^150\!300\!600\!1200 \
   --field="Scan Buttons:BTN" "" \
-  --field="Colour:FBTN" 'bash -c "run_scan %1 %2 %3 %4 Color"' \
-  --field="Grayscale:FBTN" 'bash -c "run_scan %1 %2 %3 %4 Gray"' \
-  --field="B&W:FBTN" 'bash -c "run_scan %1 %2 %3 %4 Lineart"' \
+  --field="Colour:FBTN" 'bash -c "run_scan %1 %2 %3 %4 %5 Color"' \
+  --field="Grayscale:FBTN" 'bash -c "run_scan %1 %2 %3 %4 %5 Gray"' \
+  --field="B&W:FBTN" 'bash -c "run_scan %1 %2 %3 %4 %5 Lineart"' \
+  --field="...:BTN" "" \
   --field="Presets:BTN" "" \
-  --field="Art (Colour PNG):FBTN" 'bash -c "run_scan %1 %2 %3 png Color"' \
-  --field="Doc (Gray PDF):FBTN" 'bash -c "run_scan %1 %2 %3 pdf Gray"' \
-  --field="Doc (B&W PDF):FBTN" 'bash -c "run_scan %1 %2 %3 pdf Lineart"' \
-  #--changed-action="@update_name" \
-  --button=Close:1
+  --field="Art (Colour PNG):FBTN" 'bash -c "run_scan %1 art png %4 %5 Color"' \
+  --field="Art (Colour JPG):FBTN" 'bash -c "run_scan %1 art jpg %4 %5 Color"' \
+  --field="Doc (Gray PDF):FBTN" 'bash -c "run_scan %1 doc pdf %4 %5 Gray"' \
+  --field="Doc (B&W PDF):FBTN" 'bash -c "run_scan %1 doc pdf %4 %5 Lineart"' \
+  --no-buttons --buttons-layout=center