1

I'm trying to convert pdfs to png which usually works great but I occasionally get this result.

There's two parts that are 'highlighted' which I'm not sure why since ImageMagick doesn't consistently do this.

Here's the code I'm working with:

with Image(filename=pdf, resolution=200) as src:
     src.background_color = Color('white')
     src.alpha_channel = 'remove'
     images = src.sequence
     Image(images[1]).save(filename='test.png')

I thought maybe there was a problem with transparency so the first two lines are related to this question.

How can I get this image to just show up normally like this image which looks correct? Thanks!

byteme
  • 55
  • 5

1 Answers1

0

The issue you have is that your input has an alpha channel. So just removing the alpha channel or flattening it on white leaves that area as gray, since it is in the underlying image.

The best way to fix that is using ImageMagick -lat function.

See http://www.imagemagick.org/script/command-line-options.php#lat

As I do not have your original, I can only process your resulting PNG file, which shows this behavior.

Input with transparency

enter image description here

Processing

convert input.png -background white -flatten -negate -lat 25x25+10% -negate result.png

enter image description here

fmw42
  • 28,780
  • 5
  • 37
  • 49
  • Just tested it and it works on the command line, thank you! However, since I'm working with a lot of images (on the degree of tens of thousands), is there an equivalent to the -lat function in Python code? – byteme May 23 '18 at 17:55
  • I do not see that in Python Wand. But threshold_local in Skimage looks similar, though I have not tried it. See http://scikit-image.org/docs/dev/api/skimage.filters.html#skimage.filters.threshold_local. There is also adaptive thresholding in OpenCV at https://docs.opencv.org/3.4.0/d7/d4d/tutorial_py_thresholding.html, though again I have not tried that. – fmw42 May 23 '18 at 19:14