Related
How can I draw a rectangle that has a color with an alpha?
I have:
windowSurface = pygame.display.set_mode((1000, 750), pygame.DOUBLEBUF)
pygame.draw.rect(windowSurface, pygame.Color(255, 255, 255, 128), pygame.Rect(0, 0, 1000, 750))
But I want the white rectangle to be 50% transparent, but the alpha value doesn't appear to be working.
pygame.draw functions will not draw with alpha. The documentation says:
Most of the arguments accept a color argument that is an RGB triplet. These can also accept an RGBA quadruplet. The alpha value will be written directly into the Surface if it contains pixel alphas, but the draw function will not draw transparently.
What you can do is create a second surface and then blit it to the screen. Blitting will do alpha blending and color keys. Also, you can specify alpha at the surface level (faster and less memory) or at the pixel level (slower but more precise). You can do either:
s = pygame.Surface((1000,750)) # the size of your rect
s.set_alpha(128) # alpha level
s.fill((255,255,255)) # this fills the entire surface
windowSurface.blit(s, (0,0)) # (0,0) are the top-left coordinates
or,
s = pygame.Surface((1000,750), pygame.SRCALPHA) # per-pixel alpha
s.fill((255,255,255,128)) # notice the alpha value in the color
windowSurface.blit(s, (0,0))
Keep in mind in the first case, that anything else you draw to s will get blitted with the alpha value you specify. So if you're using this to draw overlay controls for example, you might be better off using the second alternative.
Also, consider using pygame.HWSURFACE to create the surface hardware-accelerated.
Check the Surface docs at the pygame site, especially the intro.
Unfortunately there is no good way to draw a transparent shape. See pygame.draw module:
A color's alpha value will be written directly into the surface [...], but the draw function will not draw transparently.
So you can't draw transparent shapes directly with the 'pygame.draw' module. The 'pygame.draw' module does not blend the shape with the target surface. You have to draw the shape on a surface (with RGBA format). Then you can blit (and thus blend) this surface. See Draw a transparent rectangles and polygons in pygame. Hence you need to do a workaround:
Create a pygame.Surface object with a per-pixel alpha format large enough to cover the shape.
Draw the shape on the _Surface.
Blend the Surface with the target Surface. blit() by default blends 2 Surfaces
For example 3 functions, which can draw transparent rectangles, circles and polygons:
def draw_rect_alpha(surface, color, rect):
shape_surf = pygame.Surface(pygame.Rect(rect).size, pygame.SRCALPHA)
pygame.draw.rect(shape_surf, color, shape_surf.get_rect())
surface.blit(shape_surf, rect)
def draw_circle_alpha(surface, color, center, radius):
target_rect = pygame.Rect(center, (0, 0)).inflate((radius * 2, radius * 2))
shape_surf = pygame.Surface(target_rect.size, pygame.SRCALPHA)
pygame.draw.circle(shape_surf, color, (radius, radius), radius)
surface.blit(shape_surf, target_rect)
def draw_polygon_alpha(surface, color, points):
lx, ly = zip(*points)
min_x, min_y, max_x, max_y = min(lx), min(ly), max(lx), max(ly)
target_rect = pygame.Rect(min_x, min_y, max_x - min_x, max_y - min_y)
shape_surf = pygame.Surface(target_rect.size, pygame.SRCALPHA)
pygame.draw.polygon(shape_surf, color, [(x - min_x, y - min_y) for x, y in points])
surface.blit(shape_surf, target_rect)
Minimal example: repl.it/#Rabbid76/PyGame-TransparentShapes
import pygame
def draw_rect_alpha(surface, color, rect):
shape_surf = pygame.Surface(pygame.Rect(rect).size, pygame.SRCALPHA)
pygame.draw.rect(shape_surf, color, shape_surf.get_rect())
surface.blit(shape_surf, rect)
def draw_circle_alpha(surface, color, center, radius):
target_rect = pygame.Rect(center, (0, 0)).inflate((radius * 2, radius * 2))
shape_surf = pygame.Surface(target_rect.size, pygame.SRCALPHA)
pygame.draw.circle(shape_surf, color, (radius, radius), radius)
surface.blit(shape_surf, target_rect)
def draw_polygon_alpha(surface, color, points):
lx, ly = zip(*points)
min_x, min_y, max_x, max_y = min(lx), min(ly), max(lx), max(ly)
target_rect = pygame.Rect(min_x, min_y, max_x - min_x, max_y - min_y)
shape_surf = pygame.Surface(target_rect.size, pygame.SRCALPHA)
pygame.draw.polygon(shape_surf, color, [(x - min_x, y - min_y) for x, y in points])
surface.blit(shape_surf, target_rect)
pygame.init()
window = pygame.display.set_mode((250, 250))
clock = pygame.time.Clock()
background = pygame.Surface(window.get_size())
ts, w, h, c1, c2 = 50, *window.get_size(), (160, 160, 160), (192, 192, 192)
tiles = [((x*ts, y*ts, ts, ts), c1 if (x+y) % 2 == 0 else c2) for x in range((w+ts-1)//ts) for y in range((h+ts-1)//ts)]
for rect, color in tiles:
pygame.draw.rect(background, color, rect)
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window.blit(background, (0, 0))
draw_rect_alpha(window, (0, 0, 255, 127), (55, 90, 140, 140))
draw_circle_alpha(window, (255, 0, 0, 127), (150, 100), 80)
draw_polygon_alpha(window, (255, 255, 0, 127),
[(100, 10), (100 + 0.8660 * 90, 145), (100 - 0.8660 * 90, 145)])
pygame.display.flip()
pygame.quit()
exit()
the most i can do to help you is to show you how to draw a rectangle that is not filled in.
the line for the rectangle is:
pygame.draw.rect(surface, [255, 0, 0], [50, 50, 90, 180], 1)
the "1" means that it is not filled in
A way to do this is to instead of drawing a rectangle with pygame, you could create an image of a transparent rectangle by using a drawing program such as paint.net or Fire Alpaca.
I need to deform an image moving one (or more) point from its position to a different destination.
The following example (generated using warp function in Photoshop) clarifies the problem:
BEFORE
AFTER
In the example point (100,100) has been stretched to the new position (200,200).
I tried using the warp solutions provided by OpenCV:
input_pts = np.float32([(100, 100), (400, 100), (400, 400), (100, 400)])
output_pts = np.float32([(200, 200), (400, 100), (400, 400), (100, 400)])
(M, _) = cv2.findHomography(input_pts, output_pts)
# M = cv2.getPerspectiveTransform(input_pts, output_pts)
out = cv2.warpPerspective(test_img, M, (500, 500), flags=cv2.INTER_AREA)
and
input_pts = np.float32([(100, 100), (400, 100), (100, 400)])
output_pts = np.float32([(200, 200), (400, 100), (100, 400)])
M = cv2.getAffineTransform(input_pts, output_pts)
out = cv2.warpAffine(test_img.copy(), M, (500, 500), flags=cv2.INTER_LINEAR)
without success.
I'm wondering what could be the best approach to solve this problem.
I have the following transparent images.
What I want to do is to paste them on an image with a background of a specific color. The color of the background is randomized like this:
rand1, rand2, rand3 = (random.randint(0, 255),
random.randint(0, 255),
random.randint(0, 255))
background = Image.new('RGBA', png.size, (rand1, rand2, rand3))
alpha_composite = Image.alpha_composite(background, png)
Unfortunately, some of the logos don't go well with their background colors. The background color sometimes comes close to color(s) inside the logo, which makes the logo either partially or completely invisible. Here is an example where the background color is almost identical to the orange color in the Ubuntu logo:
What I did was to get all of the colors from each logo and save them in a list of tuples like this. This is actually a list of lists of tuples. I've just edited it now to highlight which nested list of tuples belong to which logo:
Intel = [(0, 113, 197)]
Corsair = [(4, 7, 7)]
Google = [(66, 133, 244), (234, 67, 53), (251, 188, 5), (52, 168, 83), (0, 255, 255), (255, 128, 0), (255, 255, 0)]
Riot = [(209, 54, 57), (255, 255, 255), (226, 130, 132), (0, 0, 0)]
What I want to do is to use the above ^ information to randomly choose background colours so that no part of a logo is made invisible. I'm asking for suggestions on strategies to go about this..
This is the function that adds a background color to the logos:
def logo_background(path):
rand1, rand2, rand3 = (random.randint(0, 255),
random.randint(0, 255),
random.randint(0, 255))
png = Image.open(path).convert('RGBA')
colors = extcolors.extract_from_path(path)
background = Image.new('RGBA', png.size, (rand1, rand2, rand3))
alpha_composite = Image.alpha_composite(background, png)
return alpha_composite
>>> extcolors.extract_from_path(path)
[((0, 113, 197), 25727), 56235]
# for the intel logo, which is just blue with transparent background
Some logos are completely black. The corsair logo is an all black logo with transparent background but the code did not select the right background.
I think using a thre component vektor like rgb is difficult for randome choice. I would convert it to the hsv-color-system (hue, saturation, lightness) first. Then we only need to worry about hue and can use random.choice to choose a value from a 1D list of possible values.
Google = [(66, 133, 244), (234, 67, 53), (251, 188, 5), (52, 168, 83), (0, 255, 255), (255, 128, 0), (255, 255, 0)]
threshold = 0.1 #No hue value closer than threshold to a logo-color
# convert to hsv
GoogleHsv = [colorsys.rgb_to_hsv(r/255.0, g/255.0, b/255.0) for r,g,b in Google]
print("GoogleHsv:", GoogleHsv)
# list of possible hue-values that are at least threshold away from all logo-hue-values
choices = [x for x in np.linspace(0,1,201) if min([abs(x-h) for h,l,s in GoogleHsv]) > threshold]
print("choices:", choices)
h = random.choice(choices)
l = random.random() # random lightness
s = random.random() # random saturation
# you could also use constants for l,s to alway get a vibrant/dark color.
color = [int(x*255) for x in colorsys.hsv_to_rgb(h, l, s)] # converting back to rbg
print("color:", color)
Hope this helps. Have a nice day.
EDIT
For your function, it would look like this:
def logo_background(path):
threshold = 0.1
png = Image.open(path).convert('RGBA')
used_colors_rgb = extcolors.extract_from_path(path)[0]
used_hsv = [colorsys.rgb_to_hsv(r/255.0, g/255.0, b/255.0) for (r,g,b), _ in used_colors_rgb]
choices = [x for x in np.linspace(0,1,201) if min([abs(x-h) for h,l,s in used_hsv]) > threshold]
h, l, s = random.choice(choices), (random.random()+1) / 2, (random.random()+1) / 2
color = [int(x*255) for x in colorsys.hsv_to_rgb(h, l, s)]
background = Image.new('RGBA', png.size,tuple(color))
alpha_composite = Image.alpha_composite(background, png)
return alpha_composite
logo_background("google.png")
I have images (png) that are 128x128 pixels, how do I convert the image so that each pixel in the image is closest in color to the ones in the following array?
The array will probably get bigger with more specific colors, but in this case:
[(0, 255, 100), (100, 100, 100), (255, 255, 255), (0, 0, 0), (156, 126, 210)]
How can I draw a rectangle that has a color with an alpha?
I have:
windowSurface = pygame.display.set_mode((1000, 750), pygame.DOUBLEBUF)
pygame.draw.rect(windowSurface, pygame.Color(255, 255, 255, 128), pygame.Rect(0, 0, 1000, 750))
But I want the white rectangle to be 50% transparent, but the alpha value doesn't appear to be working.
pygame.draw functions will not draw with alpha. The documentation says:
Most of the arguments accept a color argument that is an RGB triplet. These can also accept an RGBA quadruplet. The alpha value will be written directly into the Surface if it contains pixel alphas, but the draw function will not draw transparently.
What you can do is create a second surface and then blit it to the screen. Blitting will do alpha blending and color keys. Also, you can specify alpha at the surface level (faster and less memory) or at the pixel level (slower but more precise). You can do either:
s = pygame.Surface((1000,750)) # the size of your rect
s.set_alpha(128) # alpha level
s.fill((255,255,255)) # this fills the entire surface
windowSurface.blit(s, (0,0)) # (0,0) are the top-left coordinates
or,
s = pygame.Surface((1000,750), pygame.SRCALPHA) # per-pixel alpha
s.fill((255,255,255,128)) # notice the alpha value in the color
windowSurface.blit(s, (0,0))
Keep in mind in the first case, that anything else you draw to s will get blitted with the alpha value you specify. So if you're using this to draw overlay controls for example, you might be better off using the second alternative.
Also, consider using pygame.HWSURFACE to create the surface hardware-accelerated.
Check the Surface docs at the pygame site, especially the intro.
Unfortunately there is no good way to draw a transparent shape. See pygame.draw module:
A color's alpha value will be written directly into the surface [...], but the draw function will not draw transparently.
So you can't draw transparent shapes directly with the 'pygame.draw' module. The 'pygame.draw' module does not blend the shape with the target surface. You have to draw the shape on a surface (with RGBA format). Then you can blit (and thus blend) this surface. See Draw a transparent rectangles and polygons in pygame. Hence you need to do a workaround:
Create a pygame.Surface object with a per-pixel alpha format large enough to cover the shape.
Draw the shape on the _Surface.
Blend the Surface with the target Surface. blit() by default blends 2 Surfaces
For example 3 functions, which can draw transparent rectangles, circles and polygons:
def draw_rect_alpha(surface, color, rect):
shape_surf = pygame.Surface(pygame.Rect(rect).size, pygame.SRCALPHA)
pygame.draw.rect(shape_surf, color, shape_surf.get_rect())
surface.blit(shape_surf, rect)
def draw_circle_alpha(surface, color, center, radius):
target_rect = pygame.Rect(center, (0, 0)).inflate((radius * 2, radius * 2))
shape_surf = pygame.Surface(target_rect.size, pygame.SRCALPHA)
pygame.draw.circle(shape_surf, color, (radius, radius), radius)
surface.blit(shape_surf, target_rect)
def draw_polygon_alpha(surface, color, points):
lx, ly = zip(*points)
min_x, min_y, max_x, max_y = min(lx), min(ly), max(lx), max(ly)
target_rect = pygame.Rect(min_x, min_y, max_x - min_x, max_y - min_y)
shape_surf = pygame.Surface(target_rect.size, pygame.SRCALPHA)
pygame.draw.polygon(shape_surf, color, [(x - min_x, y - min_y) for x, y in points])
surface.blit(shape_surf, target_rect)
Minimal example: repl.it/#Rabbid76/PyGame-TransparentShapes
import pygame
def draw_rect_alpha(surface, color, rect):
shape_surf = pygame.Surface(pygame.Rect(rect).size, pygame.SRCALPHA)
pygame.draw.rect(shape_surf, color, shape_surf.get_rect())
surface.blit(shape_surf, rect)
def draw_circle_alpha(surface, color, center, radius):
target_rect = pygame.Rect(center, (0, 0)).inflate((radius * 2, radius * 2))
shape_surf = pygame.Surface(target_rect.size, pygame.SRCALPHA)
pygame.draw.circle(shape_surf, color, (radius, radius), radius)
surface.blit(shape_surf, target_rect)
def draw_polygon_alpha(surface, color, points):
lx, ly = zip(*points)
min_x, min_y, max_x, max_y = min(lx), min(ly), max(lx), max(ly)
target_rect = pygame.Rect(min_x, min_y, max_x - min_x, max_y - min_y)
shape_surf = pygame.Surface(target_rect.size, pygame.SRCALPHA)
pygame.draw.polygon(shape_surf, color, [(x - min_x, y - min_y) for x, y in points])
surface.blit(shape_surf, target_rect)
pygame.init()
window = pygame.display.set_mode((250, 250))
clock = pygame.time.Clock()
background = pygame.Surface(window.get_size())
ts, w, h, c1, c2 = 50, *window.get_size(), (160, 160, 160), (192, 192, 192)
tiles = [((x*ts, y*ts, ts, ts), c1 if (x+y) % 2 == 0 else c2) for x in range((w+ts-1)//ts) for y in range((h+ts-1)//ts)]
for rect, color in tiles:
pygame.draw.rect(background, color, rect)
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window.blit(background, (0, 0))
draw_rect_alpha(window, (0, 0, 255, 127), (55, 90, 140, 140))
draw_circle_alpha(window, (255, 0, 0, 127), (150, 100), 80)
draw_polygon_alpha(window, (255, 255, 0, 127),
[(100, 10), (100 + 0.8660 * 90, 145), (100 - 0.8660 * 90, 145)])
pygame.display.flip()
pygame.quit()
exit()
the most i can do to help you is to show you how to draw a rectangle that is not filled in.
the line for the rectangle is:
pygame.draw.rect(surface, [255, 0, 0], [50, 50, 90, 180], 1)
the "1" means that it is not filled in
A way to do this is to instead of drawing a rectangle with pygame, you could create an image of a transparent rectangle by using a drawing program such as paint.net or Fire Alpaca.