I am trying to do this:
surface_with_text = pygame.font(params).render(params) # transparent bg
surface_with_text.set_alpha(100) # make it half transparent
another_surface.blit(surface_with_text) # blit onto image
But, of course, it silently fails - my text is still fully opaque... Why so? How do I find a workaround?
Of course I can blit "255 - 100"-transparent another_surface copy on the top of the text, but what fun is that, right?
The rendered font surface is already using per-pixel alpha values (otherwise, it'd have a solid background).
From the Font.render doc:
If the background is transparent a pixel alpha will be included.
From the set_alpha doc:
This value is different than the per pixel Surface alpha. If the Surface format contains per pixel alphas, then this alpha value will be ignored.
Related
I'm trying to draw a rectangle in Pillow that has no Fill, just a white outline rectangle to frame an object.
However, I've noticed that when I set width=1 then I don't actually get the colour that I set, instead I get this muddy looking grey.
To investigate I've turned the width up to 7 and I get a nice thick white box, exactly what I'm looking for albeit far too thick. But the rectangle has that muddy coloured outline on it.
I tried setting outline to None but it doesn't work. I'd be happy if instead of the outline it showed my white line on width=1. Am I missing a setting?
Also as an aside, is there any way to get width < 1? I noticed it takes an integer value but I'd love an even finer line.
My current code:-
import PIL.Image as img
import PIL.ImageDraw as imdrw
idraw=imdrw.Draw(im)
idraw.rectangle([r[0],r[1],r[2],r[3]], fill=None, outline=None, width=1)
I initially tried that last line as
idraw.rectangle([r[0],r[1],r[2],r[3]], fill=None, outline="#ffffff", width=1)
EDIT: To explain this a bit clearer... It's like there's a border around my outline. And when I set the outline width to be low enough (e.g. = 1) then it ONLY shows the border, which is a muddy grey colour. If there's a way for me to either get rid of this border, OR change the border colour to white then that works too.
Problem: I have multiple book cover images. I made a template of "book"-like template with a 3D perspective. And all I have to do now its take each of book cover images, correct a perspective (its always constant, because the template is always unchanged) and merge my perspective corrected image with the template (background/canvas).
For easier understanding - here is an example created in Adobe Photoshop:
With red arrows I tried to show vertex points of the original cover image (before perspective correction). As you can see, 2 vertex points on the right have to stay. The other two points of the left have to be corrected always the same.
Can you please show me how to achieve that?
UPDATE
What I have:
1) Cover itself
2) Template with transparent background:
I need to transform perspective of cover and merge it with template image
You don't really need to write any Python, you can just do it in the Terminal with ImageMagick using a "Perspective Transform" like this:
magick cover.png -virtual-pixel none -distort perspective "0,0 96,89 %w,0 325,63 %w,%h 326,522 0,%h 96,491" template.png +swap -flatten result.png
Looking at the parameters to the perspective transform, you can hopefully see there are 4 pairs of coordinates, one pair for each corner of the transform showing how the source location gets mapped in the output image.
So, the top-left corner of the cover (0,0) gets mapped to the top-left of the empty area in the template (96,89). The top right of the cover (width,0) gets mapped to the top-right of the empty area of the template (325,63). The bottom-right of the cover (width,height) gets mapped to the bottom-right of the empty area on the template (326,522). The bottom-left of the cover (0,height) gets mapped to the bottom-left corner of the empty area of the template (96,491).
If you are using the old v6 ImageMagick, replace magick with convert.
Note that, if you really want to do it in Python, there is a Python binding called wand here. I am not very experienced with wand but this seems to be equivalent:
#!/usr/bin/env python3
from itertools import chain
from wand.color import Color
from wand.image import Image
with Image(filename='cover.png') as cover, Image(filename='template.png') as template:
w, h = cover.size
cover.virtual_pixel = 'transparent'
source_points = (
(0, 0),
(w, 0),
(w, h),
(0, h)
)
destination_points = (
(96, 89),
(325, 63),
(326, 522),
(96, 491)
)
order = chain.from_iterable(zip(source_points, destination_points))
arguments = list(chain.from_iterable(order))
cover.distort('perspective', arguments)
# Overlay cover onto template and save
template.composite(cover,left=0,top=0)
template.save(filename='result.png')
Keywords: Python, ImageMagick, wand, image processing, perspective transform, distort.
Question is simple is there a way to draw half circle or pie using wxGraphicsContext? I need to make something look like in the picture (this gauge
is made in pyQt4).
EDIT: I did
path = gc.CreatePath()
path.AddArc(450, 100, 30, math.radians(180), math.radians(0))
pen = wx.Pen('#ffffff', 1)
#pen.SetCap(wx.CAP_BUTT)
gc.SetBrush(wx.Brush('#CC7F32', wx.SOLID))
gc.SetPen(pen)
gc.DrawPath(path)
and I got this
How can I get full outline because now there is none at the bottom of the arc?
You can do it using AddArc() or, probably better in this particular case, AddArcToPoint(). As always, remember that you can fill any path by setting the brush corresponding to the desired background colour and calling FillPath() instead of just StrokePath().
And to draw the segment, you should just call AddLineToPoint() directly: it's simple as you just need to give it the coordinates of your circle centre.
I am working on a game in pygame/python, and I am wondering who has the know how to show me to turn an image into a map.
The idea is simple. The image is colored by tile type. When the program loads the image, I want the color (example) #ff13ae to be matched to a certain grass tile, and the color (example) #ff13bd to a different tile. Now, I know that I may very well have to convert from hexcodes to rgb, but that is trivial. I just want to know the way I would go about this, mainly because all my other games don't do anything of this sort.
Use pygame.PixelArray:
The PixelArray wraps a Surface and provides direct access to the surface's pixels.
[...]
pxarray = pygame.PixelArray(surface)
# Check, if the first pixel at the topleft corner is blue
if pxarray[0, 0] == surface.map_rgb((0, 0, 255)):
...
I have an application written in python that's basically an etch-a-sketch, you move pixels around with WASD and arrow keys and it leaves a trail. However, I want to add a counter for the amount of pixels on the screen. How do I have the counter update without updating the entire surface and pwning the pixel drawings?
Use Surface.blit(source, dest, area=None, special_flags = 0): return Rect
dest can be a pair of coordinates representing the upper left corner of the source.
You probably want to erase the your old counter value, before you blit the new one. For this you can capture the background before you blit your counter value for the first time. Then blit that image everytime before you update the counter value.
In addition you should make the background of surface that your blitting transparent. Assuming you have black font on white background, you could use:
source.set_colorkey((255,255,255))