0

Applying

img = Image.open("original.png").convert("L") 
edited = ImageOps.autocontrast(img, cutoff=1, ignore=255, preserve_tone=True)
edited.save("autocontrast.png")

original.png

enter image description here

autocontrast.png

enter image description here

Expected result: White space above right stays white and is not black after applying ImageOps.autocontrast

-> any ideas what I do wrong?

Python 3.9.5 (tags/v3.9.5:0a7dcbd, May 3 2021, 17:27:52) [MSC v.1928 64 bit (AMD64)] on win32 Pillow 8.4.0

scriptDaddy
  • 142
  • 1
  • 8
  • Have you printed those values to make sure they are actually 255? Depending on the source, then might have a different value. You might consider using `img.getpixel` to fetch a value from that region. – Tim Roberts Jan 03 '22 at 19:30
  • When opening the image in an editor e.g. photopea , the area is transparent and values are 0, even if I set ignore=0 it does not keep the white border. – scriptDaddy Jan 04 '22 at 16:04

1 Answers1

1

This is a cute one! In your original image, those pixels actually ARE black (RGB = (0,0,0)), but they have an alpha value of 0, so the white you are seeing in the background window showing through. The alpha gets dropped when you convert to 'L'.

If you do ignore=0, it should do what you expect.

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • When opening the image in an editor e.g. photopea , the area is transparent and values are 0, even if I set ignore=0 it does not keep the white border. – scriptDaddy Jan 04 '22 at 16:04
  • It's NOT a white border. It never was. It is a TRANSPARENT border. You see it as white here because the StackOverflow window background color is white. If StackOverflow used a pink background, you would see a pink region. The white (or pink) pixels are not part of your image. If you REALLY want to, you can use `alpha_composite` to force those pixels to white before converting to `L`: https://stackoverflow.com/questions/35859140/remove-transparency-alpha-from-any-image-using-pil – Tim Roberts Jan 04 '22 at 17:58
  • Thanks , alpha_composite did the trick – scriptDaddy Jan 04 '22 at 23:08