Images distorted using pygame - python

Trying to build my first pygame project and wanted to import the following map. Used image:
My code is:
import pygame
pygame.init()
size = (1300, 700)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("My Game")
done = False
clock = pygame.time.Clock()
map = pygame.image.load('map.jpg')
map = map.convert()
map = pygame.transform.scale(map, (466,700))
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
screen.fill((255,255,255))
screen.blit(map, (430, 0))
pygame.display.flip()
clock.tick(60)
pygame.quit()
and the output looks like:
I have also tried converting the jpg to a bmp but still the same results. I cannot figure out what is distorting the image, trying with other images it appears that the outputted images is slightly wider than the original. I suspect that pygame is just reading the width wrong and then putting the pixels into an array with that width.
Any advice on fixing this would be much appreciated.

There is a bug in SDL_image (which Pygame is based on) that corrupts images in OS X 10.11.
https://bugzilla.libsdl.org/show_bug.cgi?id=3154
Currently, the only known workaround is to downgrade to SDL_image 1.2.10, but I have not been able to confirm.

Related

why wont python pygame wont work in spyder?

I recently downloaded pygame on my school pc(I have experience with it before), but when I try to open a window it does not work and are black even though I colored the window in the code.
I even copied this code:
import pygame
background_colour = (255,255,255)
(width, height) = (300, 200)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Tutorial 1')
screen.fill(background_colour)
pygame.display.flip()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
But it does the same thing.
Extra:
I use Spyder with python 3.6
Thanks in advance:)
Your code work in my mac. If you are in Windows, try to restart the PC. Also, try to execute your example from a command line.

Pygame - Text won't appear on the window

I was displaying some text on the window but when i executed the code, it only showed a blank black window. I had no idea what was wrong, as no exceptions occurred. It just would not work.
Well i am an absolute beginner who have just passed some Python course for a couple of weeks, so i was just playing around the pygame module, instead of having big plans like game developing.
I also tried to search for similar problems but they are all so complicated that I can not understand quite well due to the long pieces of code.
I checked and no syntax is wrong, the font file is present, and the names of the objects are in the right place i.e. they are not using the wrong methods.
i don't know what else i can try...
import pygame as pg
pg.init()
win = pg.display.set_mode((720,540))
consolas = pg.font.SysFont("Consolas.ttf", 100)
text = consolas.render("hello", False , (255,255,255))
win.blit(text , (0,0))
i expected the string "hello" will be blited on the surface with the size of 100 and color to be completely white, but the whole thing did not show up at all.
You have to call pygame.display.flip or pygame.display.update to actually update the screen with the content of the win surface.
Also, you should have a main loop that handles events by calling pygame.event.get. This ensures your window stays open. Also, if you don't process events, your window becomes unresponsive and maybe doesn't even draw anything (depending on your OS/window manager)
So add this to your code:
run = True
while run:
for e in pg.event.get():
if e.type == pg.QUIT:
run = False
pg.display.update()
You need to use pg.display.update() or pg.display.flip() after you have drawn the text i.e. after win.blit line. The difference and use of the two can be found here : Difference between pygame.display.update and pygame.display.flip
As people have already commented that you have to call pygame.display.update() or pygame.display.flip()
Here's the full code:
import pygame as pg
pg.init()
win = pg.display.set_mode((720,540))
consolas = pg.font.SysFont("Consolas.ttf", 100)
running = True
while running:
text = consolas.render("hello", False , (255,255,255))
for event in pg.event.get():
if event.type == pg.QUIT:
running = False
win.blit(text , (0,0))
pg.display.flip()
pg.quit()

Trying to display a png file in pygame using pygame.display.update, and it shows for less than a second then disappears.

The image is a playing card. We are using pygame 4.5 community edition and pycharm 2.6.9 because 2.7 does not support pygame (this is a school). Here is the code:
import pygame
pygame.init()
picture=pygame.image.load("cards/S01.png")
pygame.display.set_mode(picture.get_size())
main_surface = pygame.display.get_surface()
main_surface.blit(picture, (0,0))
pygame.display.update()
Why does the window disappear?
Try this:
import pygame
pygame.init()
picture=pygame.image.load("cards/S01.png")
pygame.display.set_mode(picture.get_size())
main_surface = pygame.display.get_surface()
main_surface.blit(picture, (0,0))
while True:
main_surface.blit(picture, (0,0))
pygame.display.update()
pygame.display.update() updates a frame. There are multiple frames per second depending on what what you draw onto the surface.
The problem is, after you update the screen with pygame.display.update(), you do nothing, and your program simply ends. pygame.display.update() does not block.
You need what is usually called a main loop. Here's a simple example with event handling:
import pygame
pygame.init()
picture = pygame.image.load("cards/S01.png")
# display.set_mode already returns the screen surface
screen = pygame.display.set_mode(picture.get_size())
# a simple flag to show if the application is running
# there are other ways to do this, of course
running = True
while running:
# it's important to get all events from the
# event queue; otherwise it may get stuck
for e in pygame.event.get():
# if there's a QUIT event (someone wants to close the window)
# then set the running flag to False so the while loop ends
if e.type == pygame.QUIT:
running = False
# draw stuff
screen.blit(picture, (0,0))
pygame.display.update()
This way, your application does not, only when someone closes the window.

Blurring in PyGame

I'm working on a short intro in PyGame, and I need to blur some static lines. After two days of searching, I can't find anything.
Does PyGame have a built-in method to blur a shape or surface? Will I need to do it manually? What would be the best way to approach this?
To clarify, I'm not looking for motion blur – I just need a simple in-place blur, almost like a glowing effect.
There's no built-in way to blur a Surface.
Workarounds are:
scale the Surface with pygame.transform.smoothscale and then back to its original size (ugly)
iterating over each pixel, get the color of the neighbour pixels, calculate the average value for red, green, and blue, and set the color of that pixel (slow as hell if not using something like numpy)
I recommend using the Python Imaging Library (PIL), since it's nice and fast.
Here's a simple and running example:
import pygame
from PIL import Image, ImageFilter
pygame.init()
screen = pygame.display.set_mode((512, 512))
clock = pygame.time.Clock()
compressed = 'eJztnV2LHEUUhv0J+w+cf5CAELxQGT+IqNGsicYQds3kc4nEZDBiYhKTNmaFEBeyagzEiIMiKCIOKiiSi0HEGxU2F+qFKIMXGhTMXBiE3LT9jt1Db6c/qnqq6pyqroaXZGene2fqOXVO1anT1WEY3hIy0OLcpnak2UhBrF6kQY76qfd04vNa1J/fS4r1TMwOjFcihYoE+zgXX9vbBCPFzLuKeVdpJf6b3hbouLfifm6KeZF63g6Ms4cfHjFgn9Y56nZpguI4TM26LC7MULeRq1r8fxxPzdjbAB3/AQO+IupSt5WLitp1yICtiALqtnJRDLh6/p6/iAbUbeWiGHD1/D1/z7+B/F/atys8EQThseUL4dGL74RH3v1oIrz2wpmzYXD42fB0Z6vn7xB/MD36xqVVvKt0fGn5avRvi7q9XFLEYq3R/r5nftyvZbjnqOftQBn/tsk+//zb70/LPtEoEmk+KPr77UidSEFKXbxOzZUbf7BXxD2rIRgY4j0T8+4Lf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eNvAH8L80NIcgZPsTfNPjwssus/PWfYx/75p/umYUPVceIwf6zw7XpF6LrOP+QdU/Cf+IJorYowIzonwM15P+4xj59+8Zog79vKzel3XJv4SGsR9Uif3wPU+bzN/fOaITzdmpYr7sGncbeYf28BMzGw4BfNeU/y8a/zTihiujX0CeA4K1I/tZfaI5TU7nr+X5+/l+Xt5/l6ev9cU/GcZcPX8aW2gx4CtiKy+146zFvnHgVEkZ+quOCpq39Yig/1gcgT/1LjcLKEdtJnEBNhim7o9mir0uUhdwz4Bfv4cfBH19/e6yRYwVwhiexgp7ueB7+v2KY4V7Zhfon7MNKte6j04x4/nJPUfe7dmpQ=='
size, image_mode, raw = (128, 128), 'RGBA', compressed.decode("base64").decode("zlib")
# create the original pygame surface
surf = pygame.image.fromstring(raw, size, image_mode)
# create a PIL image and blur it
pil_blured = Image.fromstring("RGBA", size, raw).filter(ImageFilter.GaussianBlur(radius=6))
# convert it back to a pygame surface
other = pygame.image.fromstring(pil_blured.tostring("raw", image_mode), size, image_mode)
pygame.time.set_timer(pygame.USEREVENT, 1000)
while True:
for e in pygame.event.get():
if e.type == pygame.USEREVENT: surf, other = other, surf
if e.type == pygame.QUIT: break
else:
screen.fill((255, 255, 255))
screen.blit(surf, (192, 192))
pygame.display.flip()
clock.tick(60)
continue
break
Hint: If you're on Windows and want to install PIL, download it from here and install it via
pip install Pillow-2.8.2-cp27-none-win32.whl
or whatever version you downloaded (installing PIL on Windows can be a great PITA otherwise).

Pygame screen size not matching window size [duplicate]

I'm starting to explore Python and Pygame, however I am running into a problem. Everything I draw to the screen is only displayed in the top left quarter of the window. I thought it was my code but any demo programs I tried also display the same way.
Demo code from thenewboston on youtube
import pygame, sys
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((640,360),0,32)
background = pygame.image.load("Background.jpg").convert()
mouse_c = pygame.image.load("ball.png").convert_alpha()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
screen.blit(background, (0,0))
x,y = pygame.mouse.get_pos()
x -= mouse_c.get_width()/2
y -= mouse_c.get_height()/2
screen.blit(mouse_c, (x,y))
pygame.display.update()
In the video his displays correctly and mine looks like this
Using:
Python 2.7.4 32 bit
pygame 1.9.1 32 bit
mac 10.8.3 64 bit on macbook pro retina
I think either it has to do with the retina display or I installed something wrong. Any help would be appreciated.
The problem is, in fact, with the resolution of the retina screen. In order to get pygame to work correctly, download setresx and use it to set your resolution to any setting without HiDPI.

Categories

Resources