Crop image to circle shape for PyQt [duplicate] - python

This question already has answers here:
How to create circular image using pyqt4?
(3 answers)
How to crop an image using QPainterPath without saving rest of image
(2 answers)
Closed 1 year ago.
I am creating a simple application with PyQt6. I want to my code to automatically crop the selected image into circle shape. How can I do that? Please help

Using an external library like pillow or opencv makes things easy as they generally have functions that already provide similar processing, but there's no need to add a dependency for simple manipulations like this, unless performance becomes an issue.
On Qt it's pretty straightforward: create a new QPixmap based on the minimum extent (assuming the circle is going to be the biggest possible in a given rectangle), create a QPainterPath with a circle that will be used for clipping, and finally draw the source contents in it.
def circleImage(imagePath):
source = QtGui.QPixmap(imagePath)
size = min(source.width(), source.height())
target = QtGui.QPixmap(size, size)
target.fill(QtCore.Qt.transparent)
qp = QtGui.QPainter(target)
qp.setRenderHints(qp.Antialiasing)
path = QtGui.QPainterPath()
path.addEllipse(0, 0, size, size)
qp.setClipPath(path)
sourceRect = QtCore.QRect(0, 0, size, size)
sourceRect.moveCenter(source.rect().center())
qp.drawPixmap(target.rect(), source, sourceRect)
qp.end()
return target
This obviously creates an image with transparent background around the circle, you can use any of the global colors or your own QColor.
Note that, no matter the color, the fill() is mandatory, otherwise there will probably be random pixels on the unpainted portions of the image instead (usually some "RAM residue").

Related

How can I make a pygame surface object from only part of a loaded image? [duplicate]

This question already has answers here:
How can I crop an image with Pygame?
(4 answers)
How to blit from the x and y coordinates of an image in Pygame?
(1 answer)
Closed 2 years ago.
Pixel-perfect collisions from multi-sprite image
I want to do pixel-perfect collision checks. I have a sprite with 6 frames in PNG format with a transparent background, but they're all on a single spritesheet. (The 'enemy' sprites have 2 frames each.) I can animate them and move them fine.
How can I make a pygame surface object from only part of a loaded image? I suppose I could load each sprite frame individually, but this is laborious and doesn't seem like good coding.
More Info
I can turn the whole spritesheet into a mask easy enough as follows (originally in a spritesheet class but simplified here):
# Load image
sheet = pygame.image.load("spritesheet.png")
# Create mask
sheet_mask = pygame.mask.from_surface(sheet)
I could simply use the rect of the current frame to detect collisions, but this will be very unsatisfying because collisions would seem to happen without contact sometimes. An pygame has this wonderful mask object for this sort of thing.
In each loop of the main game loop I want to check the appropriate frame of one sprite against the appropriate frame of the other. I have been trying to create a mask for each frame then store them in a list.
Use the subsurface() method to get a rectangular subsurface from a pygame.Surface:
sheet = pygame.image.load("spritesheet.png")
rect_area = pygame.Rect(left, top, width, heigth)
sprite_from_sheet = sheet.subsurface(rect_area)

Keeping transparent pixels through rotozoom in pygame, Python 3

I'm currently making a game in pygame, Python 3 and a part of the code that has been giving me issues is:
for counter in range(0, 30):
particles = pygame.image.load('particles.png').convert()
particles = pygame.transform.rotozoom(particles, 36*counter, 1.1**counter).convert()
particles.set_colorkey((0, 0, 0, 0))
screen.blit(particles, particles.get_rect(centerx=480, centery=100))
pygame.display.flip()
time.sleep(0.05)
particles.png is just a few colored pixels on a transparent background. The problem is that when the image is rotated and scaled, some of those particles sort of blur out resulting in a mass of black squares around them.
How do I fix this problem? Thanks in advance!!
I've had a bad experience with pygame's rotozoom combined with transparency. Instead of loading the image and then rotozooming it, consider the following:
Using PIL library:
Convert the loaded surface to a string using pygame.image.tostring
Convert the string to an Image object
Use PIL's functions to replace rotozoom (zooming, rotating)
Convert the Image object back into a surface using the same method
Using math:
Load data about the position and color of the particles,
or infer it from the loaded surface
Use simple mathematical functions to caculate their new position,
according to the angle and zoom
Paint them using pygame.gfxdraw, pygame.draw or pygame.Surface.set_at
Good luck!

Pyglet: blit_into texture and alpha

I've been using pyglet for a while now and I really like it. I've got one thing I'd like to do but have been unable to do so far, however.
I'm working on a 2D roleplaying game and I'd like the characters to be able to look different - that is to say, I wouldn't like use completely prebuilt sprites, but instead I'd like there to be a range of, say, hairstyles and equipment, visible on characters in the game.
So to get this thing working, I thought the most sensible way to go on about it would be to create a texture with pyglet.image.Texture.create() and blit the correct sprite source images on that texture using Texture.blit_into. For example, I could blit a naked human image on the texture, then blit a hair texture on that, etc.
human_base = pyglet.image.load('x/human_base.png').get_image_data()
hair_style = pyglet.image.load('x/human_hair1.png').get_image_data()
texture = pyglet.image.Texture.create(width=human_base.width,height=human_base.height)
texture.blit_into(human_base, x=0, y=0, z=0)
texture.blit_into(hair_style, x=0, y=0, z=1)
sprite = pyglet.sprite.Sprite(img=texture, x=0, y=0, batch=my_sprite_batch)
The problem is that blitting the second image into the texture "overwrites" the texture already blitted in. Even though both of the images have an alpha channel, the image below (human_base) is not visible after hair_style is blit on top of it.
One reading this may be wondering why do it this way instead of, say, creating two different pyglet.sprite.Sprite objects, one for human_base and one for hair_style and just move them together. One thing is the draw ordering: the game is tile-based and isometric, so sorting a visible object consisting of multiple sprites with differing layers (or ordered groups, as pyglet calls them) would be a major pain.
So my question is, is there a way to retain alpha when using blit_into with pyglet. If there is no way to do it, please, any suggestions for alternative ways to go on about this would be very much appreciated!
setting the blend function correctly should fix this:
pyglet.gl.glBlendFunc(pyglet.gl.GL_SRC_ALPHA,pyglet.gl.GL_ONE_MINUS_SRC_ALPHA)
I ran into the very same problem and couldn't find a proper solution. Apparently blitting two RGBA images/textures overlapping together will remove the image beneath. Another approache I came up with was using every 'clothing image' on every character as an independent sprite attached to batches and groups, but that was far from the optimal and reduced the FPS dramatically.
I got my own solution by using PIL
import pyglet
from PIL import Image
class main(pyglet.window.Window):
def __init__ (self):
TILESIZE = 32
super(main, self).__init__(800, 600, fullscreen = False)
img1 = Image.open('under.png')
img2 = Image.open('over.png')
img1.paste(img2,(0,0),img2.convert('RGBA'))
img = img1.transpose(Image.FLIP_TOP_BOTTOM)
raw_image=img.tostring()
self.image=pyglet.image.ImageData(TILESIZE,TILESIZE,'RGBA',raw_image)
def run(self):
while not self.has_exit:
self.dispatch_events()
self.clear()
self.image.blit(0,0)
self.flip()
x = main()
x.run()
This may well not be the optimal solution, but if you do the loading in scene loading, then it won't matter, and with the result you can do almost almost anything you want to (as long as you don't blit it on another texture, heh). If you want to get just 1 tile (or a column or a row or a rectangular box) out of a tileset with PIL, you can use the crop function.

setting alpha using pixels_alpha in pygame

I'm using pygame for image editing (not displaying). (Yes, I know there are other options like PIL/pillow, but pygame should work for this.)
I want to draw and save an image where I'm individually setting the alpha values of each pixel according to a formula (I'm drawing a complicated RGBA profile). It seems that pixels_alpha is the right way to do this. But when I change pixels_alpha it's ignored, the image just stays transparent. Here's my code...
import pygame as pg
import os
def init_transparent(img_width, img_height):
"""
Create a new surface with width and height, starting with transparent
background. This part works!
"""
os.environ['SDL_VIDEODRIVER'] = 'dummy'
pg.init()
pg.display.init()
# next command enables alpha
# see http://stackoverflow.com/questions/14948711/
pg.display.set_mode((img_width, img_height), 0, 32)
# pg.SRCALPHA is a flag that turns on per-pixel alpha.
return pg.Surface((img_width, img_height), pg.SRCALPHA)
def make_semitransparent_image():
surf = init_transparent(20, 20)
# alphas should be a direct reference to the alpha data in surf
alphas = pg.surfarray.pixels_alpha(surf)
# alphas is a uint8-dtype numpy array. Right now it's all zeros.
for i in range(20):
for j in range(20):
# Since pixels_alpha gave me a uint8 array, I infer that
# alpha=255 means opaque. (Right?)
alphas[i,j] = 200
pg.image.save(surf, '/home/steve/Desktop/test.png')
make_semitransparent_image()
Again, it saves an image but the image looks completely transparent, no matter what value I set alphas to. What am I doing wrong here? (Using Python 2.7, pygame 1.9.1release.) (Thanks in advance!)
The Pygame docs say that pixels_array() locks the surface as long as the resulting array exists, and that locked surface may not be drawn correctly. It seems to be the case that they are not saved correctly, too. You need to throw away the alphas surfarray object before calling image.save(). This can be done by adding del alphas just before image.save().
(Note that this is more a workaround than a proper fix. Adding del alphas can only have a reliable effect only on CPython (and not PyPy, Jython, IronPython). Maybe you can really "close" a surfarray objects, the same way you can either close a file or merely forget about it?)

Semi-transparent 2d VTK text background

Simple question, but I've tried a few things and nothing seems to work.
I want to overlay some statistics onto a 3d VTK scene, using 2D vtkTextActors. This works fine, but the text is at times difficult to see, depending on what appears behind it in the 3D scene.
For this reason, I'd like to add a 2d, semi-transparent "box" behind my text actors to provide a darker background.
Which VTK object is appropriate for this? I've tried so far:
vtkLegendBoxActor: Not what I want, but I can use this with no text to display a semi-transparent box on screen. I cannot size it directly and I get warnings about not initialising some of the content.
vtkImageData: Tried manually creating image data and adding it to the scene; I believe it was placed within the 3d scene and not used as an overlay. If that's not the case then I couldn't get it to show at all.
vtkCornerAnnotation: Scales with window size, is fixed to a corner and the background opacity cannot be set AFAIK.
vtkTextActor: Cannot set a background color or opacity
Can anyone tell me how they might achieve what I'm after in VTK?
I've found a way to do this with vtkPolyMapper2D which seems to work okay. It seems to be a very stupid way to do this. If there is something more elegant, I'm all ears.
import vtk
extents = [[0,0],[620,0],[620,220],[0,220]]
polyPoints = vtk.vtkPoints()
for x, y in extents:
polyPoints.InsertNextPoint(x, y, 0)
num_corners = len(extents)
polyCells = vtk.vtkCellArray()
polyCells.InsertNextCell(num_corners + 1)
for i in range(0, num_corners):
polyCells.InsertCellPoint(i)
polyCells.InsertCellPoint(0) ## Rejoin at the end
poly_profile = vtk.vtkPolyData()
poly_profile.SetPoints(polyPoints)
poly_profile.SetPolys(polyCells) ## Goes solid
cut_triangles = vtk.vtkTriangleFilter()
cut_triangles.SetInput(poly_profile)
_poly_mapper = vtk.vtkPolyDataMapper2D()
_poly_mapper.SetInput(poly_profile)
_poly_mapper.SetInputConnection(cut_triangles.GetOutputPort())
_actor = vtk.vtkActor2D()
_actor.SetMapper(_poly_mapper)
_actor.GetProperty().SetColor([0.1,0.1,0.1])
_actor.GetProperty().SetOpacity(0.5)
#Add to renderer as normal
just use vtktexture, vtkimagedata & add your own image as texture background to the vtkrenderer by reducing the opacity like a watermark. thats it

Categories

Resources