3

I am trying to display images at a smaller size then their source and while I have achieved this, the quality is horrible on any resolution other then 1080p. Here's my code thus far:

import random
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.uix.image import Image
from kivy.logger import Logger
from kivy.metrics import Metrics
from kivy.graphics import Color, Rectangle
from kivy.uix.scatter import Scatter

Window.fullscreen = True
Window.size = (1920, 1080)

Player1Hand=['Spade', 'Spade', 'Spade',
                        'Heart', 'Heart',
                        'Heart', 'Diamond', 'Club',
                        'Club', 'Club']

random.shuffle(Player1Hand)
global Draw_Card
Draw_Card = 0

class Sprite(Image):

    def __init__(self, **kwargs):
        super(Sprite, self).__init__(allow_stretch=True, **kwargs)
        self.texture.mag_filter = 'linear'
        w, h = self.texture_size
        self.size = (params.scale * w, params.scale * h)

class CardSprite(Image):

    def __init__(self, **kwargs):
        super(CardSprite, self).__init__(allow_stretch=True, **kwargs)
        self.texture.mag_filter = 'linear'
        w = 210.9
        h = 240
        self.size = (params.scale * w, params.scale * h)


class PlayerHandArea(Widget):


    def __init__(self):
        super(PlayerHandArea, self).__init__()
        with self.canvas:
            Color(1,0,0,0.5)
            Rectangle(size=((1152 * params.scale), (240 * params.scale)),
                      pos = ((384 * params.scale), 0))

class DrawCard(Widget):

    def __init__(self):
        super(DrawCard, self).__init__()
        global Draw_Card
        while Draw_Card > 0:
            scatter = Scatter(do_rotation=False, do_scale=False,
                              size_hint=(None,None),
                              size=(210.9 * params.scale, 240 * params.scale))
            self.NextCard = (CardSprite
                (source='Images/'+(Player1Hand.pop(0))+'.png'))
            self.add_widget(scatter)
            scatter.add_widget(self.NextCard)
            Draw_Card += -1           

class Blank(Widget):

    def __init__(self, pos, size):
        super(Blank, self).__init__()
        with self.canvas:
            Color(0, 0, 0)
            Rectangle(pos=pos, size=size)
            Color(1, 1, 1)

class Game(Widget):

    def __init__(self):
        super(Game, self).__init__()
        self.background = (Sprite
                (source='Images/Background.jpg'))
        self.size = self.background.size
        self.add_widget(self.background)
        self.add_widget(PlayerHandArea())
        global Draw_Card
        Draw_Card += 5
        self.add_widget(DrawCard())
        self.add_widget(Blank(*params.blank_rect))

class GameApp(App):

    def build(self):
        params.init()
        top = Widget()
        top.add_widget(Game())
        return top

class params(object):

    def init(self):
        self.bg_width, self.bg_height = 1920, 1080
        self.width, self.height = Window.size
        self.center = Window.center
        ws = float(self.width) / self.bg_width
        hs = float(self.height) / self.bg_height
        self.scale = min(ws, hs)
        Logger.info('size=%r; dpi=%r; density=%r; SCALE=%r',
            Window.size, Metrics.dpi, Metrics.density, self.scale)
        if ws > hs:
            gap = self.width - (self.bg_width * hs)
            self.blank_rect = ((self.width - gap, 0), (gap, self.height))
        else:
            gap = self.height - (self.bg_height * ws)
            self.blank_rect = ((0, self.height - gap), (self.width, gap))
params = params()


if __name__ == '__main__':
    GameApp().run()

So I'm using Richard Jones example on setting the size of your images based on the screen size of whos using it. This works perfect when run at 1920 by 1080 but when I switch the Window.size to say 720p the downscaled images have so much noise in them it looks awful. Ivied tried using linear and nearest for the mag_filter and also setting the card's size to dp(210.9) and dp(240) but they come out the same. Does anyone know a better way to scale down with kivy using different resolutions or a document that explains how to get this to work? The original image sizes are 746 by 1037.

0 Answers0