2

I am stuck with a basic problem to which I was not able to find any answer nor solution no matter what I tried. Please help me out and enlighten me, what I am doing wrong :-)

Task:

Make a Numpy array of Pixels, manipulate them by an algorithm and then print an image from that array.

Occuring Problem:

When I manipulate single pixels this way, there is noise artifacts appearing next to manipulated pixels (see example pictures)

Details - What I want to do:

I have a numpy array to create an image from. The array is created as black:

shape = np.zeros((100, 100, 3), dtype=np.uint8)

Now I want to manipulate single pixels by an algorithm, for instance:

color = (255, 255, 255)
shape[50, 50] = color

There will be 100s and 1000s of pixels manipulated in color this way.

At the end, I want to make an image from that shape array and print it to the screen:

arr_image = Image.fromarray(shape, 'RGB')
arr_image.save('test.jpg')

Details - What I tried:

No matter, what I do, I get pixel noise next to created pixels in images created using the example code!

I tried:

  • Searching the Internet/Stackoverflow: No such problem described/found
  • Taking working code from other examples and manipulate to my needs: Same artifacts occuring after manipulation
  • Drawing pixels in the created image after making a black-only (0,0,0) image from a numpy array by using Image method putpixel: Same artifacts appearing
  • Changing up syntax: Same artifacts appearing
  • Checking the underlying numpy arrays for grey values at the position of the artifacts: THERE IS NO SUCH GREY VALUES IN THE ARRAYS!!
  • When using Pillow method show(), the pixel noise is gone (!) in the windows popup, however (!!!), when I open the image from outside python, the pixel noise is in the exact same image visible (!!!!!!!!!!!!!)

Example code that produces this problem:

import numpy as np
from PIL import Image

shape = np.zeros((100, 100, 3), dtype=np.uint8)

white = (255, 255, 255)

shape[50][50] = white

arr_image = Image.fromarray(shape, 'RGB')
# arr_image.putpixel((50, 50), white)
arr_image.save('test.jpg')

Example images:

3
  • Try saving as a .png rather than jpg. Jpg reduce image file size by reducing image quality (and adding noise as a result). Png files save all image data without reducing quality
    – dwb
    Apr 22, 2021 at 10:47
  • JPEG is lossy. That means it can change your pixels to save space. Try with loss-less PNG format. Apr 22, 2021 at 10:47
  • Oh wow, thanks guys. I did not think of this as JPG is fully supported by Pillow, but now I actually learnt something about image compression, thank you guys!
    – flow
    Apr 22, 2021 at 13:20

1 Answer 1

0

The solution to this is: Do not use .jpg data, the compression of this format causes observed pattern.

When using .png, the bug was instantly solved! In this image (.png), you can now see that there is 0 pixel noise nor strange artifact patterns, when generating an array-based image as described above: enter image description here

Thank you @dantechguy and @Mark Setchell, you guys saved me! :-))

2
  • Just wanted to say that is a super well asked first question on Stackoverflow, and that your level of research and attempts really shows. We need more people like you here, so thank you!
    – dwb
    Apr 22, 2021 at 19:57
  • 1
    @dantechguy Thank you for such a warm welcome! :)
    – flow
    Apr 23, 2021 at 17:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.