Zoom to mouse position - pyside6 - zooming

Hello I am tring to implement simple image widget with zoom to mouse position. I combined the example at Zooming in/out on a mouser point ? and https://doc.qt.io/qt-5/qtwidgets-widgets-imageviewer-example.html. However the image do not scale as expected and the scale bars do not update appropriately either. Here is my code:
import sys
from PySide6 import QtWidgets
from PySide6.QtCore import Qt
from PIL.ImageQt import ImageQt
from PySide6.QtGui import QPixmap
from PySide6.QtWidgets import QDialog, QVBoxLayout, QLabel, QScrollArea
class MyScrollArea(QScrollArea):
def __init__(self, imageWidget):
# initialize widget
super().__init__()
self.setWidget(imageWidget)
self.myImageWidget = imageWidget
self.oldScale = 1
self.newScale = 1
def wheelEvent(self, event) -> None:
if event.angleDelta().y() < 0:
# zoom out
self.newScale = 0.8
else:
# zoom in
self.newScale = 1.25
# compute scrollbar positions
scrollBarPosHorizontal = self.horizontalScrollBar().value()
scrollBarPosVertical = self.verticalScrollBar().value()
deltaToPos = (event.position() / self.oldScale) - (self.myImageWidget.pos() / self.oldScale)
delta = deltaToPos * self.newScale - deltaToPos * self.oldScale
# resize image
self.myImageWidget.resize(self.myImageWidget.size() * self.newScale)
# set scrollbars
self.horizontalScrollBar().setValue(scrollBarPosHorizontal+delta.x())
self.verticalScrollBar().setValue(scrollBarPosVertical+delta.y())
# save old scale
self.oldScale = self.newScale
class ImageViewer(QDialog):
def __init__(self, img):
# initialize widget
super().__init__()
self.setWindowTitle('Zoom example')
self.imageWidget = QLabel()
self.imageWidget.installEventFilter(self)
self.imageWidget.setAlignment(Qt.AlignCenter)
self.pixmap = QPixmap.fromImage(img)
self.imageWidget.setPixmap(self.pixmap)
# create scroll area
self.scrollArea = MyScrollArea(self.imageWidget)
# insert to layout
self.layout = QVBoxLayout()
self.layout.addWidget(self.scrollArea)
self.setLayout(self.layout)
if __name__ == '__main__':
# prepare app
app = QtWidgets.QApplication(sys.argv)
# prepare image
image = ImageQt("test.png")
# create viewer widget
MyWidget = ImageViewer(image)
MyWidget.show()
# close app
sys.exit(app.exec())
The image do not scale to mouse point at all. What am I doing wrong?

The main problem is that by default the pixmap is not resized when QLabel is resized, so setScaledContents(True) must be used.
Note that the algorithm used for the zoom and translation doesn't work very well, as it doesn't consider the change in the range of the scroll bars correctly.
I propose an alternate version that actually zooms on the mouse similarly to what happens in common image viewers/editors and map viewers. The trick is to map the mouse position to the label, and get the delta based on the scaled position:
class MyScrollArea(QScrollArea):
def __init__(self, imageWidget):
# ...
imageWidget.setScaledContents(True)
# ...
def wheelEvent(self, event) -> None:
if event.angleDelta().y() < 0:
# zoom out
self.newScale = 0.8
else:
# zoom in
self.newScale = 1.25
widgetPos = self.myImageWidget.mapFrom(self, event.position())
# resize image
self.myImageWidget.resize(self.myImageWidget.size() * self.newScale)
delta = widgetPos * self.newScale - widgetPos
self.horizontalScrollBar().setValue(
self.horizontalScrollBar().value() + delta.x())
self.verticalScrollBar().setValue(
self.verticalScrollBar().value() + delta.y())
self.oldScale = self.newScale
Note that QLabel is not well suited for such purposes (especially for big images and high zoom values). I strongly suggest you to consider switching to the Graphics View Framework.

Related

Move a sprite, but cannot delete the precedent image of it [duplicate]

I'm building a pong game trying to get better at programming but Im having trouble moving the ball. When the move_right method is called the ellipse stretches to the right instead of moving to the right. I've tried putting the ball variable in the init method but that just makes it not move at all even though the variables should be changing on account of the move_right method. I have also tried setting the x and y positions as parameters in the Ball class,but that just stretches it also.
I don't understand why when I run the following code the ball I'm trying to move stretches to the right instead of moves to the right. Can someone explain why this is happening? I have tried everything I can think of but i can't get it to do what I want.
import pygame,sys
import random
class Ball:
def __init__(self):
self.size = 30
self.color = light_grey
self.x_pos = width/2 -15
self.y_pos = height/2 -15
self.speed = 1
#self.ball = pygame.Rect(self.x_pos, self.y_pos,self.size,self.size)
def draw_ball(self):
ball = pygame.Rect(self.x_pos, self.y_pos,self.size,self.size)
pygame.draw.ellipse(screen,self.color,ball)
def move_right(self):
self.x_pos += self.speed
class Player:
def __init__(self,x_pos,y_pos,width,height):
self.x_pos = x_pos
self.y_pos = y_pos
self.width = width
self.height = height
self.color = light_grey
def draw_player(self):
player = pygame.Rect(self.x_pos,self.y_pos,self.width,self.height)
pygame.draw.rect(screen,self.color,player)
class Main:
def __init__(self):
self.ball=Ball()
self.player=Player(width-20,height/2 -70,10,140)
self.opponent= Player(10,height/2-70,10,140)
def draw_elements(self):
self.ball.draw_ball()
self.player.draw_player()
self.opponent.draw_player()
def move_ball(self):
self.ball.move_right()
pygame.init()
size = 30
clock = pygame.time.Clock()
pygame.display.set_caption("Pong")
width = 1000
height = 600
screen = pygame.display.set_mode((width,height))
bg_color = pygame.Color('grey12')
light_grey = (200,200,200)
main = Main()
#ball = pygame.Rect(main.ball.x_pos, main.ball.y_pos,main.ball.size,main.ball.size)
#player = pygame.Rect(width-20,height/2 -70,10,140)
#opponent = pygame.Rect(10,height/2-70,10,140)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
#ball = pygame.Rect(main.ball.x_pos, main.ball.y_pos,main.ball.size,main.ball.size)
#pygame.draw.rect(screen,light_grey,player)
#pygame.draw.rect(screen,light_grey,opponent)
#pygame.draw.ellipse(screen,light_grey,ball)
main.draw_elements()
main.move_ball()
main.ball.x_pos += main.ball.speed
pygame.display.flip()
clock.tick(60)
You have to clear the display in every frame with pygame.Surface.fill:
while True:
# [...]
screen.fill(0) # <---
main.draw_elements()
main.move_ball()
main.ball.x_pos += main.ball.speed
pygame.display.flip()
# [...]
Everything that is drawn is drawn on the target surface. The entire scene is redraw in each frame. Therefore the display needs to be cleared at the begin of every frame in the application loop. The typical PyGame application loop has to:
handle the events by either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (blit all the objects)
update the display by either pygame.display.update() or pygame.display.flip()

Use mask function (for collision detection) with multiple sprites

I am brand new to Pygame and am making a game for my A-level course. I am trying to have multiple bats that I can spawn and collide with. I am using Pygames mask function for 'pixel perfect collision' but I cannot get multiple bats to spawn at the same time with the collision system also working. I tried using groups but I haven't been able to get this to work. Does anyone know how to fix my code/ a better way around this problem? Thanks! The relevant code is below...
class Bat(pygame.sprite.Sprite):
def __init__(self, bat_x, bat_y):
pygame.sprite.Sprite.__init__(self)
self.bat1 = pygame.image.load("Sprites\Bat_enemy\Bat-1.png").convert_alpha() # For hit registration for bat
self.bat1 = pygame.transform.scale(self.bat1, (80, 70))
self.bat_mask = pygame.mask.from_surface(self.bat1)
self.bat_rect = self.bat1.get_rect()
self.bat_x = bat_x
self.bat_y = bat_y
bats = pygame.sprite.Group()
Then in main loop:
num_of_bats = [1]
#Bat#
for i in num_of_bats:
bat_x = (random.randint(0, 600))
bat_y = (random.randint(0, 600))
bat = Bat(bat_x, bat_y, i)
bats.add(bat)
for bat in bats:
offsetP2B = (int(x - batx), int(y - self.baty)) #Player to Bat
resultP2B = bat_mask.overlap(player_mask, offsetP2B)
First get rectangle based collision working, then worry about the bitmask accuracy!
There's a couple of problems with your Sprite. The big one is that PyGame uses sprite.image to draw the bitmap. Your sprite code is using bat1 instead. It also needs to position the sprite.rect to the co-ordinate of the Bat. Furthermore, the collision mask must be called mask, and the sprite's collision/position pygame.Rect must be called rect.
I'm not sure if it's just a paste-o, but the sprite group definition shouldn't be inside the Sprite class.
So ... with a few minor fixups:
class Bat(pygame.sprite.Sprite):
def __init__(self, bat_x, bat_y, bat_image):
pygame.sprite.Sprite.__init__(self)
self.image = bat_image
self.rect = self.image.get_rect()
self.mask = pygame.mask.from_surface( self.image )
self.rect.centre = ( bat_x, bat_y )
def update( self ):
# TODO: code to make this bat move/flat whatever
pass
There's only minor differences here. It's better to load the image in once, outside the sprite class, than loading it in hundreds(?) of times - once for each bat.
Now it's pretty easy to make a colony of bats:
import os.path
START_BAT_COUNT = 30
BAT_IMAGE_PATH = os.path.join( 'Sprites', 'Bat_enemy', 'Bat-1.png' )
# group to hold all the bat sprites
all_bats = pygame.sprite.Group()
# Going Batty!
bat_image = pygame.image.load( BAT_IMAGE_PATH ).convert_alpha()
for i in range( START_BAT_COUNT ):
bat_x = (random.randint(0, 600))
bat_y = (random.randint(0, 600))
new_bat = Bat( bat_x, bat_y, bat_image )
all_bats.add( new_bat )
The in your main loop:
# move every bat
all_bats.update()
...
# paint every bat
all_bats.draw( screen )

Pygame blit part of an image(background image)

I have a pygame menu where i have drawn some buttons, which represent the level difficulty of my game. For user convenience, i have made a sprite which indicates which level-button is selected(think of it as a light green frame around the button). Now, if i have a solid color as my background, i can just fill the frame with the bg color. But i wanna have a custom image. However i am not sure how to do the deleting stuff with this image. I dont want to have to do a surface.blit(bgImage, surface.get_rect())
in every while-loop. Is there any way to tell pygame to blit just part of the image? So the end-result is still fine-looking. Here is my code when i have a color as the background
(please note that my question does not apply only to this scenario, its more of a general way as to blitting part of an image, without having to rely on cropping the image using 3rd party software like paint(net), photoshop etc.):
#class for the highlight sprite that appears when a level button is clicked
class HighLightImage(Sprite):
def __init__(self, spriteX, spriteY, width = 180, height = 60):
Sprite.__init__(self)
self.rect = pygame.Rect(spriteX, spriteY, width, height)
self.image = pygame.image.load("highlight.png")
#function to draw the highlight sprite, after deleting its older position.
def draw(self, newSpriteX, newSpriteY):
#due to technical issues the following method is using 4 dirty sprite deletions.
surface.fill(bgCol, (self.rect.x, self.rect.y, self.rect.width, 10))
surface.fill(bgCol, (self.rect.x, self.rect.y + self.rect.height-10, self.rect.width, 10))
surface.fill(bgCol, (self.rect.x, self.rect.y, 10, self.rect.height))
surface.fill(bgCol, ( self.rect.x + self.rect.width-10, self.rect.y, 10, self.rect.height))
self.rect.x = newSpriteX
self.rect.y = newSpriteY
surface.blit(self.image, self.rect)
And here is the main while-loop
def mainIntro():
#snake image
snakeImg = pygame.image.load("snakeB.png")
snakeImg = pygame.transform.scale(snakeImg, (150,200))
#highlight obj
hlObj = HighLightImage(0, 0)
#starting level = 1
levels = 1
#initial fill
surface.fill(bgCol)
intro = True
#start button
startButton = StartButton(WIDTH/2-330, HEIGHT - 150)
startButton.draw("Start")
#Exit button
exitButton = ExitButton(WIDTH/2+110, HEIGHT - 150)
exitButton.draw("Exit")
#level buttons
easyLvl = EasyLevelButton( 65, HEIGHT/2 )
easyLvl.draw("Easy")
midLvl = MediumLevelButton( 320, HEIGHT/2 )
midLvl.draw("Medium")
hardLvl = HardLevelButton( 570, HEIGHT/2 )
hardLvl.draw("Hard")
instructions()
surface.blit(snakeImg, (WIDTH/2-75, HEIGHT - 250))
while intro:
for ev in pygame.event.get():
# X exit event
if ev.type == QUIT:
pygame.quit()
sys.exit()
if ev.type == MOUSEMOTION:
startButton.hover()
exitButton.hover()
easyLvl.hover()
midLvl.hover()
hardLvl.hover()
if ev.type == MOUSEBUTTONDOWN:
if easyLvl.clicked():
levels = 1
if midLvl.clicked():
levels = 2
if hardLvl.clicked():
levels = 4
#button exit event
elif exitButton.clicked():
pygame.quit()
sys.exit()
elif startButton.clicked():
intro = False
#highlight frame, according to level-button chosen
if levels == 1:
hlObj.draw(easyLvl.x-10, easyLvl.y-10)
elif levels == 2:
hlObj.draw(midLvl.x-10, midLvl.y-10)
elif levels == 4:
hlObj.draw(hardLvl.x-10, hardLvl.y-10)
update()
return levels
Finally here is an image of the end result :
P.s In the above code snippets i have not included the button classes, and the global variables like colors, width, height etc., since i dont think they are relevant with what i want to accomplice. Feel free to correct my code, and/or suggest improvements.
As #cmd said above, the area param would be a good option, but for more information, try the pygame docs or have a look at this question or try pygame.transform.chop()
Try the code below:
pygame.init()
size = width, height = 1200, 800
screen = pygame.display.set_mode(size)
image = pygame.image.load("example.png")
rect = image.get_rect()
cropx,cropy = 100,10 #Change value to crop different rect areas
cropRect = (cropx, cropy, rect.w,rect.h)
while(True):
for event in pygame.event.get():
if(event.type == pygame.QUIT):
pygame.quit()
sys.exit()
screen.blit(image,cropRect,cropRect)
pygame.display.update()

pygame and sprite blur movement

I am struggling to move the sprite correctly. Instead of smooth move I can see blur move and I do not know how to solve it.
Is there any chance you can point what I do incorrectly ?
My target with it to drop the pizza so it hits the bottom and bounce back and bounc back if it hits the top and again the bottom -> bounce -> top -> bounce etc. etc.
import pygame
gravity = 0.5
class PizzaSprite:
def __init__(self, image, spritepos):
self.image = image
self.spos = spritepos
(x, y) = spritepos
self.posx = x
self.posy = y
self.xvel = 1
self.yvel = 1
print "x ", x
print "y ", y
def draw(self, target_surface):
target_surface.blit(self.image, self.spos)
def update(self):
self.posy -= self.yvel
self.spos = (self.posx, self.posy)
return
def main():
pygame.init()
screen_width = 800
screen_height = 600
x = screen_width
y = screen_height
screen = pygame.display.set_mode((screen_width, screen_height))
wall_image = pygame.image.load("wall.png")
sky_image = pygame.image.load("sky.png")
pizza_image = pygame.image.load("pizza.png")
screen.blit(wall_image,(0,200))
screen.blit(sky_image,(0,0))
all_sprites = []
pizza1 = PizzaSprite(pizza_image, (x/2, y/2))
all_sprites.append(pizza1)
while True:
ev = pygame.event.poll()
if ev.type == pygame.QUIT:
break
for sprite in all_sprites:
sprite.update()
for sprite in all_sprites:
sprite.draw(screen)
pygame.display.flip()
pygame.quit()
main()
in the beginning of your main game while loop add
white = (255,255,255)
screen.fill(white)
let me give you a small analogy of what is happening now,
you have paper and a lot of pizza stickers with the intent to make a flip book. To make the illusion of movement on each piece of paper you place a sticker a little bit lower. The screen.fill command essentially clears the screen with the rgb color value you give it. When you dont fill the screen essentially what you are doing is trying to make that flipbook on one piece of paper. You just keep placing more and more stickers a little bit lower making a blur when what you want is one on each page.
and place
pygame.init()
screen_width = 800
screen_height = 600
x = screen_width
y = screen_height
screen = pygame.display.set_mode((screen_width, screen_height))
wall_image = pygame.image.load("wall.png")
sky_image = pygame.image.load("sky.png")
all outside of your main game loop assuming you wont be making changes to these variables ever in your program it is tedious and inefficient to redefine screen,,x,y,and your two images over and over again if they dont change.
so to sum it all up:
use the screen.fill(white) command to reset the color of your screen
You only need to import pngs and define variables once if they are never going to change and don't need them in your main loop
hope this helps clear things up.

Resizing a video player

I have pygame based video player, it has berkelium browser used for GUI,
under that are VLC libraries for playing streams. I did not make the player, but I would
like to add a "resize" option, which is not currently present.
The application is made for OS X.
On initialization of the player, pygame.set_mode(screensize,OPENGL|DOUBLEBUF|RESIZABLE) is called, problem is when I resize the window more than the screen size, these parts are not visible(shown bugged), if I try pygame.set_mode again, it causes the app to crash because of dependencies (browser and VLC).
So I decided to initialize screen using:
pygame.set_mode(screenresolution, OPENGL|DOUBLEBUF|RESIZABLE)
Which should set it to the max and then switch back to original, smaller resolution (like resize with the mouse).
How to do that? how to simulate the RESIZABLE flag and mouse move action?
The player works nice resizing to smaller sizes, it is only bigger sizes that are a problem.
class InputAndDisplay:
def init(self, fullscreen=False):
global pygame, sys
# if 'darwin' in sys.platform:
# import sys
# sys.argv = ['Core.py']
# import pygame.macosx
# pygame.macosx.init()
pygame.init()
global screenSize
self.screenSize = screenSize
if fullscreen:
try: self.screenSize = pygame.display.list_modes()[0]
except Exception, e: print e
flags = OPENGL|DOUBLEBUF|FULLSCREEN
else:
flags = RESIZABLE|OPENGL|DOUBLEBUF
print 'screenSize:', self.screenSize
try:
povrsina = pygame.display.set_mode(self.screenSize, flags)
except Exception, e:
print e
self.screenSize = screenSize
povrsina = pygame.display.set_mode(self.screenSize, RESIZABLE|OPENGL|DOUBLEBUF)
pygame.display.set_caption('NetTVBox-WebPlayer')
pygame.mouse.set_pos(list(self.screenSize))
if 'darwin' not in sys.platform:
pygame.mouse.set_visible(False)
#pygame.key.set_repeat(300, 100)
if 'darwin' not in sys.platform:
pygame.mixer.quit()
if 'linux' in sys.platform: # try to turn off vsyncing
import ctypes
libgl = ctypes.cdll.LoadLibrary('libGL.so.1')
proc = libgl.glXGetProcAddressARB('glXSwapIntervalMESA')
_glXSwapIntervalMESA = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_int)(proc)
_glXSwapIntervalMESA(0)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
#gluPerspective(45.0, 640/480.0, 0.1, 100.0)
glOrtho(-0.5, 0.5, -0.5, 0.5, -5, 5)
#glTranslatef(0.0, 0.0, -3.0)
#glRotatef(25, 1, 0, 0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glEnable(GL_TEXTURE_2D)
glShadeModel(GL_FLAT)
glClearColor(0.0, 0.0, 0.0, 0.0)
glClearDepth(1.0)
glEnable(GL_DEPTH_TEST)
glDepthFunc(GL_LEQUAL)
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
self.textures = glGenTextures(2)
self.threeDmode = MODE_3D_OFF
that's init in inputAndDisplay.py
And main file is Core.py which calls it, and a lot of other stuff as well. Thats why I didnt put all the code here, there is a lot of it and not all important
Here is part of Core.py:
input_and_display = InputAndDisplay()
input_and_display.init(self.fullscreen)
rc = RemoteControl()
rc.init()
media = Media()
if 1: # !
media.config = {
'buffering': state.mediaplayer_buffering,
'contrast': state.mediaplayer_contrast,
'saturation': state.mediaplayer_saturation,
}
media.init()
browser = Browser()
browser.init()
Thanks in advance for the help
It sounds like you're missing the VIDEORESIZE event to detect resizing.
( See related events : VIDEOEXPOSE, ACTIVEEVENT http://www.pygame.org/docs/ref/event.html )
http://www.pygame.org/docs/ref/display.html#pygame.display.set_mode
" Then the display mode is set, several events are placed on the pygame event queue. pygame.QUIT is sent when the user has requested the program to shutdown. The window will receive pygame.ACTIVEEVENT events as the display gains and loses input focus. If the display is set with the pygame.RESIZABLE flag, pygame.VIDEORESIZE events will be sent when the user adjusts the window dimensions. Hardware displays that draw direct to the screen will get pygame.VIDEOEXPOSE events when portions of the window must be redrawn. "