What file does a icon have to be for the pygame.display.set_icon() function? [duplicate] - pygame

Does anyone know how to change the pygame icon?
I found a thing on the pygame website that lets you do this, but when I try it, it just makes the pygame window very small.

First load the icon image as a surface, then use pygame.display.set_icon(surface) to change the icon.
EDIT: Since the asker doesn't know what a surface is
From the docs at http://www.pygame.org/docs/ref/surface.html
"A pygame Surface is used to represent any image. The Surface has a fixed resolution and pixel format. Call pygame.Surface() to create a new image object."
For example, if you used screen = pygame.display.set_mode, screen is a surface.
So when using pygame.display.set_icon(surface) you must first import an image as a pygame.Surface by using a = pygame.image.load('image') where a is the variable the surface will be stored and 'image' is the directory to that image. Then you can set a to the icon by using pygame.display.set_icon(surface). You can pass any surface, but it is desirable that it is 32x32.
More information here: http://www.pygame.org/docs/ref/display.html#pygame.display.set_icon

programIcon = pygame.image.load('icon.png')
pygame.display.set_icon(programIcon)
This will set the icon of the Pygame window.

If you are using pgzero you can simply set global variable ICON to path to desired PNG file, like:
ICON = 'images/icon.png'
and pgzero will automatically replace default icon.
For more information see: https://github.com/lordmauve/pgzero/pull/128/files

You simply have to make an object, say an icon, and in that variable you have to give the path of the image you want to set as icon.
icon = 'your image path'
(Make sure you put double backslashes when writing path because writing single might give you an error)
After that you have to write:
pygame.display.set_icon(icon)
Make sure you have imported and installed pygame on your system
installation of pygame:
(if you are on windows):
hit windows+r on your keyboard... This will open a dialogue box and then hit enter. your cmd will open.. after that simply run this command:
pip install pygame
Then in your folder import it
In order to import it simply write:
import pygame
If your pip command shows you an error check the installation of python and try uninstalling python and then reinstalling it

Just make sure the display has been set and the icon is set before the caption:
icon = pygame.image.load('icon.png')
pygame.display.set_icon(icon)

You must be careful when setting a window icon. See pygame.display.set_icon():
[...] Some systems do not allow the window icon to change after it has been shown. This function can be called before pygame.display.set_mode() to create the icon before the display mode is set.
Set the icon before initializing the window pygame.display.set_mode(). e.g.:
icon = pygame.image.load('my_icon.png')
pygame.display.set_icon(icon)
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption(‘my window’)
In addition, the size of the icon is limited:
[...] You can pass any surface, but most systems want a smaller image around 32x32.
If the icon is not displayed, try a smaller icon.
Make sure that the resource (image) path and the working directory is correct.
The image file path has to be relative to the current working directory. The working directory is possibly different to the directory of the python file.
The name and path of the file can be get by __file__. The current working directory can be get by os.getcwd() and can be changed by os.chdir(path).
import os
sourceFileDir = os.path.dirname(os.path.abspath(__file__))
os.chdir(sourceFileDir)
An alternative solution is to find the absolute path.
If the image is relative to the folder of the python file (or even in the same folder), then you can get the directory of the file and join (os.path.join()) the image filename. e.g.:
import pygame
import os
# get the directory of this file
sourceFileDir = os.path.dirname(os.path.abspath(__file__))
iconPath = os.path.join(sourceFileDir, 'my_icon.png')
icon = pygame.image.load(iconPath)
pygame.display.set_icon(icon)

Related

How to remove background color in Pygame (make_surface)?

I have used opencv to read png files (with no background) and convert them into pygame surface. Here are the two examples of how I did it:
Method 1:
character=pygame.image.load("character.png")
Method 2:
CharacterImage=cv2.imread("character.png")
CharacterImage=convertToRGB(CharacterImage,CharSize,CharSize)
#charSize is desired character size (for example: 50)
#I have created a user defined function as pygame renders in RGB whereas cv2 renders in BGR image format.
CharacterActions.append(rotate(pygame.surfarray.make_surface(CharacterImage),charrot))
#charrot is rotation angle
I understand that I could manually resize images and then use the first method to get the transparent background image. But I want to understand if its possible to obtain the same via second method? I don't want to manually edit so many images resizing them and that's why I want to know if there's a way for the same.
Thanks in Advance
On the images you load using your Method 1 (pygame.image.load()), you should use pygame.transform.scale() and pygame.transform.rotate() to manipulate the loaded image.
To maintain the transparency, you need to keep the alpa of the image you are loading. To do that you need to use .convert_alpha() on the resulting image. You can do that after or before the transform.
For each of these commands I have linked them to the documentation, just click on them so you can read it.

Set region icon tint color without defining scope

So I'm currently working on a Sublime Text plugin [my firt] that displays pips in the gutter that match colors in lines of css. Similar to what JetBrains does:
I have a bit of a problem though. As far as I can tell when adding a region I can only give the region a scope which the template then themes. Now I could write out a template file that defines every hex code as a scope and a theme to match but that sounds ghastly. Is there a better way to do this? Is it possible to color a region separately from the theme? I'm very new to ST plugins so if there's a crucial piece of the docs I've missed let me know :)
Here's a very stripped down version of my plugin to show how I'm achieving this currently:
import sublime, sublime_plugin
class FooCommand(sublime_plugin.TextCommand):
def run(self, edit):
regions = [s for s in self.view.sel()]
for region in regions:
lines = self.view.split_by_newlines(region)
for index, line in enumerate(lines):
self.view.add_regions("csspip-{0}".format(index), [line], "csspip", "dot",
sublime.HIDDEN | sublime.PERSISTENT)
Make a selection and run view.run_command('foo') from the console to see what it does currently [not much].
So it turns out someone has already done what I was trying to do, and I was searching for the wrong things. It's called Gutter Color.
They're actually calling imagemagick to create a custom icon file for every color sublime sees. Which sounds insane, but is the only way to do it [apparently]. I wont quote the code because context is required, but if you got the following line you can work out what they did to make it work:
https://github.com/ggordan/GutterColor/blob/master/line.py#L88

how to Load captured image in sikuli?

How to load captured image from a location into sikuli script and make it clickable.
here is what I am doing:
open pp
capture desired image
save it to some location
Now I want to load that captured image and make it clickable in code, so that functions in that carpeted image can be used as needed.
please assist... Or any work around will be much appreciated.
My Code
openApp('Safari') App.focus("Safari")
wait(10)
import shutil
import os
dir = "imgpath" wait(5)
image = capture()
name = input("abc")
newimg = os.path.join(dir, name + ".png")
shutil.move(image, newimg)

What's the command to take a picture in Sikuli

I'm using Sikuli IDE. I'd like to know what the command to take a screenshot is, so I can capture the screen at the end of a test.
Something like this
try :
if bla bla bla:
print("blablabla")
else:
TAKESCREENSHOT() #------------------> What command do I put here?
print("TEST_FAILED")
The function is capture, as in
screen = Screen()
file = screen.capture(screen.getBounds())
print("Saved screen as "+file)
It takes a screen-shot, saves it in a file, and gives you the path to that file back.
See the Sikuli documentation on it for full details.
Cheap Sikuli trick for screencaps is to have a defined region, then capture the region.
So if you've got a Chrome browser you want to cap, just set it up something like this:
App.focus('Chrome.app')
ChromeWindow = App('Chrome.app').window()
That will both focus the computer to the target application, and define a region composed of the window parameters of the application. Then run this:
capture(ChromeWindow)
Then use shutil (import shutil) to move the file around to wherever you need it in your local directories. I usually put that code pile into a function I can call when needed TakePicture(Name) where Name is what I want to call the screencap when called in a particular test. Sikuli is both powerful and easy!
To make a screenshot of the window that has focus you simple can use:
focusWindow = App.focusedWindow()
regionImage = capture(focusWindow)
shutil.move(regionImage, os.path.join(r'C:\Screenshots', 'Dummy1.png'))

JDialog - how to change icon

I want to change icon of the JDialog (to replace standard java cup)
I am able to do that this way:
ImageIcon img = new ImageIcon(OuterClass.class.getResource("fileThatWorks.jpg"));
myJDialog.setIconImage(img.getImage());
Howerver when I replaced fileThatWorks.jpg with image.ico code stopped working.
I've tried to convert my image.ico to image.jpg but it didn't nelped.
What's wrong with my approach? Why it works for some *jpg files but doesn't work for *ico files?
The ImageIcon API states that the supported types are JPEG & GIF. Not too sure of a workaround for that.