p5
p5 copied to clipboard
Unexpected Resizing behavior on with skia rendere
Rendering the image normally without attempting to resize, with image(img, 10,20)
I get this:
However when trying to dynamically resize the width to half its size I get the image below
from p5 import *
from components.shuffler import Shuffler
from components.debugger import Debugger
from game_logic.constants import ITEMS
# Intellisense can't find these on its own for some reason
global mouse_is_pressed, mouse_x, mouse_y
global shuffler, debugger
global width, height
SHUFFLED_ITEMS = list(ITEMS.keys())
img = None
img_width = None
img_height = None
def start():
"""
Start the UI. This function takes over the main thread (never returns)!
"""
run(renderer="skia", sketch_draw=draw, sketch_setup=setup)
def setup():
size(1024, 600) #touchscreen size
background(200)
global shuffler, debugger, img, img_width, img_height
shuffler = Shuffler()
debugger = Debugger()
img = loadImage("kart_ui/images/no-item.png")
print("original size", img.width(), img.height())
img_width = int(img.width()/2)
img_height = int(img.height()/2)
# no_tint()
def draw():
background(255)
fill(255)
# print(type(img))
# print(img_width, img_height)
image(img, 10,20, img_width, img_height)
def mouse_pressed():
if mouse_x < width/2:
shuffler.shuffle(SHUFFLED_ITEMS[int(random_uniform(0, len(SHUFFLED_ITEMS)))])
else:
shuffler.use_item()
if __name__ == "__main__":
start()
Hi,
Looking at the lib source code, image() doesn't appear to do a resize. Although the docs suggest it should if you give image() a width and height
Until that is fixed or clarified as to what the behaviour should be, this ugly bit of code will work without editing the library,
from p5 import *
import skia
from p5.sketch.Skia2DRenderer.image import SkiaPImage
pimg1 = None
pimg2 = None
w = None
h = None
def setup():
size(1024, 600)
background(200)
global pimg1, pimg2, w, h
simg = skia.Image.open("lena.png")
w = simg.width()
h = simg.height()
simg_scaled = simg.resize(int(w/2), int(h/2))
pimg1 = SkiaPImage(w, h, pixels=simg.toarray())
pimg2 = SkiaPImage(int(w/2),int(h/2), pixels=simg_scaled.toarray())
def draw():
background(255)
fill(255)
image(pimg1, 0, 0)
image(pimg2, w, 0)
image(pimg2, w, int(h/2))
if __name__ == "__main__":
run(renderer="skia")
Loading the image via skia directly and using skia to resize. Then converting the resized image from a skia image to a PImage.
#The loading via skia and converting to a PImage is also what loadImage() does.