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)):
...
Related
What module would there be that allows my program to run IF it detects a certain color
for example, if I want my program to type red when #FF0000 pops up on the screen--how can i achieve that. I know how to make python type, i know my conditional statements... i just need to know how to let python grab color.
I think I know what you are looking for
from PIL import ImageGrab
while True:
#gets current image
image = ImageGrab.grab()
#checks which colour a specific spot of your screen has, by coordinates
color = image.getpixel((660, 300))
if color == (255, 0, 0):
print("the color is red")
else:
print("its not red")
Pops up on the screen? The entire screen? A small snippet? A single pixel? It really depends. A basic implementation could be screenshotting on some interval, and parsing each image pixel by pixel to detect the hex code you're looking for.
I'm trying to find a way to convert pixels into a real coordinates. I have an image with known (GPS) edges values.
Top left = 43.51281, -70.46223
Top right = 43.51279, -70.46213
Bottom left = 43.51272, -70.46226
Bottom right = 43.51270, -70.46215
Image with known edges values
I have another script that prints the coordinates in pixels of an image. Is there any way that the value of each corner is declared, and that it prints the real coordinates of where I clicked?
For example: The next image shape is [460, 573] and when I click somewhere on it, the pixels of that click are shown, I want it to be real coordinates.
Example
An option is to use OpenCV's getPerspectiveTransform() function, see this for an intuitive explanation of how the function maps real world coordinates to coordinates on another image (which in your case would be mapping the GPS values to the pixel values within the image):
https://towardsdatascience.com/how-to-track-football-players-using-yolo-sort-and-opencv-6c58f71120b8
And these for an example of the function being used:
Python Open CV perspectiveTransform()
https://www.geeksforgeeks.org/perspective-transformation-python-opencv/
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 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.
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))