Reputation: 15
I am trying to make a basic Pokemon replica in Python using PyGame. I have followed a few tutorials concerning this library in the past. So, for this project, I will have a basic tile structure, either grass or water. The player is not allowed to move onto a water tile but can move freely on the grass tile. The sprite I obtained via the tutorial is of dimensions 64 x 64 and it occupies the entire space of the tile, so there are no transparent pixels around the boundaries.
However, for my purposes, I need to use my own sprites (64 x 64) which I got from a sprite sheet. However, the sprites, in this case, have the top half of the image as just transparent, unused pixels. The same applies to the quarters on each side of the sprite. So, I need to batch resize all those sprites by multiplying the visible pixel size by 2 along both axes. IE, just increase the sprite size by scale factor 2. I could easily do that, but it would still be ineffective since the new sprite size would be 128 x 128 and the ratio of unused pixels stays the same.
My question:
I need a method which I could use for all 9600 sprites I have, preferably in Python, even though I wouldn't mind a solution with some other software.
I tried looking for solutions on the web but I don't know how to properly phrase this question.
Upvotes: 1
Views: 467
Reputation: 210878
Create an empty pygame.Surface
object with transparent alpha and a size of 32x32:
new_image = pygame.Surface((32, 32), pygame.SRCALPHA)
blit
the desired region (32, 16, 32, 32) form the original 64x64 Sprite (original_image
) on to the new surface:
new_image.blit(original_image, (0, 0), (32, 16, 32, 32))
Scale the new Surface object by pygame.transform.smoothscale
new_image = pygame.transform.smoothscale(new_image, (64, 64))
Upvotes: 2
Reputation: 2790
@Rabbid76's answer of copying/blit'ing a section of the original and then enlarging/scaling it is a good option and probably the way I would go.
However, if you have already resized them to the larger size and just want a cropped down version of an existing image/surface, you can use subsurface(). That will give you a surface that is really like a virtual surface that provides a window into a subsection of the original surface. It references the same pixels as the original surface, so if one is changed it is visible in the other as well. That does not seem an issue for what you are doing though.
Upvotes: 1
Reputation: 160
If these are images you can easily do this using PIL.
from PIL import Image
Ignore this bit which just makes a 64x64 test image
from scipy import misc
img = misc.face()
img=Image.fromarray(np.uint8(img))
img=img.resize((64,64))
This should show a cute little raccoon
Next we can resize by a scale factor of two as you describe (NOTE: I'm not taking advantage of the fact that this is a square image just so this is more generalizable. You could simplify this a bit with imsz=img.size[0]
)
imsz=img.size
# resize by factor of 2
img=img.resize((imsz[0]*2,imsz[1]*2))
Which should give a 2x scaled image
Now we just crop it
# box – The crop rectangle, as a (left, upper, right, lower)-tuple
crpimg=img.crop(box=(int(0.5*imsz[0]),int(0.5*imsz[1]),int(1.5*imsz[0]),int(1.5*imsz[1])))
Which gives us a 64x64 image:
Upvotes: 1