Muddy coloured outline when drawing PIL rectangles? - python

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.

Related

in kivy, changing the color does not work the way it's supposed to

i realized that for using RGB numbers in Kivy, we should divide them by 255,but that doesn't result the color it should.
for instance:
self.btn5 = Button(text='click me!', background_color=(255/255,255/255,25/255))
self.add_widget(self.btn5)
and the result:
it's like brown or sth similar
enter image description here
what should i do? did i do something wrong during the installation process?
It does work as it is supposed to. Have a look at the documentation for background_color:
This acts as a multiplier to the texture colour. The default texture
is grey, so just setting the background color will give a darker
result. To set a plain color, set the background_normal to ''.

wxPython is there a way to draw half circle

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.

pixel image to tile map

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)):
...

PIL - Identifying an object with a virtual box

I have an image (sorry cannot link it for copyright purposes) that has a character outlined in a black line. The black line that outlines the character is the darkest thing on the picture (planned on using this fact to help find it). What I need to do is obtain four coordinates that draw a virtual box around the character. The box should be as small as possible while still keeping the outlined character inside its contents. I intend on using the box to help pinpoint what would be the central point of the character's figure by using the center point of the box.
I started with trying to identify parts of the outline. Since it's the darkest line on the image, I used getextrema() to obtain at least one point on the outline, but I can't figure out how to get more points and then combine those points to make a box.
Any insight into this problem is greatly appreciated. Cheers!
EDIT *
This is what I have now:
im = Image.open("pic.jpg")
im = im.convert("L")
lo, hi = im.getextrema()
im = im.point(lambda p: p == lo)
rect = im.getbbox()
x = 0.5 * (rect[0] + rect[2])
y = 0.5 * (rect[1] + rect[3])
It seems to be pretty consistent to getting inside the figure, but it's really not that close to the center. Any idea why?
Find an appropriate threshold that separates the outline from the rest of the image, perhaps using the extrema you already have. If the contrast is big enough this shouldn't be too hard, just add some value to the minimum.
Threshold the image with the value you found, see this question. You want the dark part to become white in the binary thresholded image, so use a smaller-than threshold (lambda p: p < T).
Use thresholdedImage.getbbox() to get the bounding box of the outline

set_alpha() for partially transparent surface

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.

Categories