svgtobw.sh 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/bin/bash
  2. if [ -f "data.txt" ]; then
  3. rm data.txt
  4. fi
  5. if [ ! -d "final" ]; then
  6. mkdir final
  7. fi
  8. for f in input/*.svg
  9. do
  10. convert -antialias -density 2000 -background none -gravity center $f temp.bmp
  11. percentwhite=$( convert temp.bmp -fuzz 60% -fill white -opaque white -fill black +opaque white -format "%[fx:100*mean]" info: | sed "s/\..*//" )
  12. newname=$( echo $f | sed "s/input\///" | sed "s/.svg//" )
  13. echo $percentwhite >> data.txt
  14. echo "$percentwhite > $newname.svg"
  15. convert temp.bmp -colorspace gray -morphology edgeout:3 Disk -negate -transparent white -threshold 100% temp_extra.bmp
  16. if (( $percentwhite >= 0 && $percentwhite <= 40 )); then
  17. ./magick temp.bmp -auto-threshold Triangle temp_threshold.bmp
  18. elif (( $percentwhite > 40 && $percentwhite <= 100 )); then
  19. ./magick temp.bmp -auto-threshold OTSU temp_threshold.bmp
  20. fi
  21. convert temp.bmp -threshold 100% temp_background.bmp
  22. composite temp_threshold.bmp temp_background.bmp final/$newname.bmp
  23. composite temp_extra.bmp final/$newname.bmp final/$newname.bmp
  24. convert final/$newname.bmp -gravity center -extent 752x752 -morphology Erode Disk:4 final/$newname.bmp
  25. potrace --height 2048pt --width 2048pt -s final/$newname.bmp -o final/$newname.svg
  26. percentwhite2=$( convert final/$newname.bmp -fuzz 60% -fill white -opaque white -fill black +opaque white -format "%[fx:100*mean]" info: | sed "s/\..*//" )
  27. echo "final $percentwhite2"
  28. if (( $percentwhite2 <= 50 )); then
  29. convert final/$newname.bmp -morphology Dilate Disk:8 final/$newname.bmp
  30. potrace --height 2048pt --width 2048pt -s final/$newname.bmp -o final/$newname.svg
  31. elif (( $percentwhite2 > 50 )); then
  32. convert final/$newname.bmp -morphology Erode Disk:8 final/$newname.bmp
  33. potrace --height 2048pt --width 2048pt -s final/$newname.bmp -o final/$newname.svg
  34. fi
  35. rm final/*.bmp
  36. done
  37. rm temp.bmp temp_extra.bmp temp_threshold.bmp temp_background.bmp