Voljega
Voljega

Reputation: 184

Pygame : Getting rid of strange gradient effect on displayed image

Hello fellow programmers,

I wrote a little python program which is use to launch random games on a retrograming distribution, and I use pygame to display the image of the game before launching it

I use a background and my issue is that the background image is clean but when displaying it and the cover of the game over it, it appears with a strange ugly gradient effect as you can see there : https://i.sstatic.net/WaFV4.jpg

It appears mostly in the corner and the cover itself is entirely unaffected.

Here is my pygame code displaying both images :

    log('showPic %s' %file)
    # INITS
    pygame.init()
    pygame.mouse.set_visible(0)
    backgroundPicture = pygame.image.load(backgroundFile)
    picture = pygame.image.load(file)
    # # CREATE FULLSCREEN DISPLAY. X = 1920- Y = 1080
    fullscreen = pygame.display.set_mode((1920,1080), FULLSCREEN)
    fullscreen.blit(backgroundPicture, (0,0))
    # # PASTE PICTURE ON FULLSCREEN
    x = (1920 - picture.get_width()) /2
    y = (1080 - picture.get_height()) /2
    fullscreen.blit(picture, (x,y))
    # # SHOW FULLSCREEN 
    pygame.display.flip()
    # # WAIT 5 SECONDS (need import time)
    time.sleep(5)
    # # EXIT PYGAME (Not needed but recommanded)
    pygame.display.quit()
    pygame.quit()

backgroundPicture is the background image and picture is the cover of the game, I combined the too like it appears in second capture.

So mainly I don't know much at all about display, images, graphical libraries and all that. I think that this might be related to transparency or alpha layer or compression format of the image but I have no knowledge at all about that either. The code is launched on a raspberry pi with a linux distribution, don't know much more about it.

Also strangely, one of my users said the strange gradient effect seems to disappear after ten or so launches of the script, but I couldn't reproduce that.

So what am I missing to get rid of that ugly effect ?

Here is the background image here if its characteristic might be related to the problem : background.png

Thank you for your help !

Upvotes: 1

Views: 213

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207405

The effect you are seeing is called "banding", see Wikipedia article. It is caused by not having enough bit-depth to represent fine gradations of colour and is most noticeable in large, untextured areas.

There are not many things you can do about it. Your options are basically:

  • to go to a 16-bit setup instead of 8-bit, if pygame can do that, or
  • add a small amount of random noise, or dithering to break it up.

Upvotes: 1

Related Questions