admin管理员组

文章数量:1287878

Below is a bash script I have written to correct a certain type of skew produced in some scanned images of text.

The script works fine except for the final command, intended to change white background to the original background color. Varying the fuzz factor in this command shows it is working, except it has no effect on the white triangles introduced by the preceding -distort command when $2 is negative. They remain unaffected by any variation in -fuzz.

Included are images before and after the script, but the white triangles at top and bottom of the right hand image do not show against the background

str(){ # stretch/compress text trapezoid to rectangle, str %f [pixels, def 5]
# NB pixels + stretches right hand side, pixels - compresses it
  a=($(identify $1|cut -d ' ' -f 3|tr 'x' ' '))
  pxl=$(convert $1 -format "%[pixel:s.p{0,0}]" info:)
  c=${2:-5}  # pixels to stretch, default 5
  let b=a[1]-c
  exp='0,0 0,0, '${a[0]}','$2' '${a[0]}',0, 0,'${a[1]}' 0,'${a[1]}', '${a[0]}','$b' '${a[0]}','${a[1]}
  convert $1 -alpha set -virtual-pixel transparent -distort Perspective "$exp" +repage q.png
  convert +repage q.png -fuzz 10% -fill $pxl -opaque white -| xv -
}

Below is a bash script I have written to correct a certain type of skew produced in some scanned images of text.

The script works fine except for the final command, intended to change white background to the original background color. Varying the fuzz factor in this command shows it is working, except it has no effect on the white triangles introduced by the preceding -distort command when $2 is negative. They remain unaffected by any variation in -fuzz.

Included are images before and after the script, but the white triangles at top and bottom of the right hand image do not show against the background

str(){ # stretch/compress text trapezoid to rectangle, str %f [pixels, def 5]
# NB pixels + stretches right hand side, pixels - compresses it
  a=($(identify $1|cut -d ' ' -f 3|tr 'x' ' '))
  pxl=$(convert $1 -format "%[pixel:s.p{0,0}]" info:)
  c=${2:-5}  # pixels to stretch, default 5
  let b=a[1]-c
  exp='0,0 0,0, '${a[0]}','$2' '${a[0]}',0, 0,'${a[1]}' 0,'${a[1]}', '${a[0]}','$b' '${a[0]}','${a[1]}
  convert $1 -alpha set -virtual-pixel transparent -distort Perspective "$exp" +repage q.png
  convert +repage q.png -fuzz 10% -fill $pxl -opaque white -| xv -
}

Share Improve this question asked Feb 23 at 0:35 user985675user985675 2913 silver badges10 bronze badges 3
  • 2 Sorry, I do not see any white triangles. Which is your input and which is the output. I suspect the second is your input and first is your output. There is no right hand side image. Please clarify. Please show your intermediate q.png image also. Note you should be able to merge your two convert commands into one chained command so that you do not have any intermediate images to write to disk (after you resolve your issue). If you post your q.png image, perhaps I can see why you are having an issue. Note that convert is now deprecated. You should install Imagemagick 7. – fmw42 Commented Feb 23 at 1:23
  • 2 I'm assuming by 'triangle' you're talking about the long sliver along the top/bottom of the 2nd photo; this reminds me of the results of a scan of an input that's not quite square; if you hold a straight line (eg, the edge of a small window) up to the text you'll also see the text is not quite square, too (the 2nd image is worse than the 1st); it looks like a slight rotation has been applied to the images with the resulting 'triangles' actually being the background peeping out from behind the images – markp-fuso Commented Feb 23 at 14:51
  • Use set -x to see what's happening – Diego Torres Milano Commented Feb 24 at 2:03
Add a comment  | 

1 Answer 1

Reset to default 0

My thanks to all who responded. The solution turned out to be quite simple. I replaced the 2 'convert' commands at the end with this one.

convert $1 -alpha set -virtual-pixel transparent -distort Perspective "$exp" -background $pxl -| xv -

本文标签: bashCannot fill white triangles generated by 39Imagemagick distort39 with background colorStack Overflow