Table of contents
1.
Introduction
2.
Pyglet.resource
3.
Path format
4.
Class file location(path)
5.
ClassLoader(path= None, script_home= None)
5.1.
add_font(name) 
5.2.
attributed(name)
5.3.
get_cached_animation_names()
5.4.
get_texture_bins()
5.5.
html(name)
5.6.
location(name)
5.7.
text(name)
5.8.
texture(name)
5.9.
get_cached_image_names()
5.10.
get_cached_texture_names()
6.
ClassURLLocation(base_url)
6.1.
get_script_home()
6.2.
get_settings_path(name)
7.
ClassZIPLocation(zip, dir)
7.1.
get_script_home()
7.2.
media (name, broadcast = True)
7.3.
text(name)
7.4.
get_cached_texture_names()
7.5.
html(name)
7.6.
get_cached_image_names()
7.7.
animation(name, flip_x=False, flip_y=False, rotate=0, border=1)
8.
Frequently Asked Questions
8.1.
How do I show a picture in a pyglet?
8.2.
What are the parameters used in the loader class?
8.3.
What are the parameters used in the open function of class file location?
9.
Conclusion 
Last Updated: Mar 27, 2024
Easy

pyglet.resource

Author Ashish Sharma
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this article, we will understand the pyglet.resource, path format, and some of its functions. Uploading resources by specifying compatible filenames is often a problem for Python, as the active directory is not the same as the application text files.

Pyglet is an easy-to-use but powerful library for developing visual GUI applications such as games, multimedia, etc. A window is a "heavy" object that consumes operating system resources. Windows may appear to be floating circuits or may be set to full screen (full screen). To upload a file that is an application, we use a pyglet utility module.

Pyglet.resource

Pyglet.resource module allows applications to specify application search methods. ZIP files may appear along the way and can be searched inside. The service module also works as expected with integrated applications such as PyInstaller, py2exe, py2app, etc.

In addition to providing file references (with file function ()), the utility module also contains simple parts for uploading images, formats, fonts, media, and texts.

External company modules or packages not tied to a particular application should build their own Uploading Module and provide a way to use resources in the module directory.

Path format

Application modes are a list of resources. Locations are searched in the order provided by the path. It is skipped if the path is invalid (for example, if the directory does not exist).

Locations in the path beginning with the symbol "in" (‘’ @ ’’) specify Python packages. Some locations specify a ZIP archive or directory in the file system. Incomplete spaces are considered to be related to script home.

path = ['res']
path = ['@levels.level1', 'res/images']

 

Paths are always sensitive, and forward particles are often used as path partitions, even in cases where the file system or platform does not. This avoids the typical editor error when moving applications between platforms.

Class file location(path)

It describes the location of the filesystem.

open(filename,mode=’rb’) - It Opens a file at the location.

  • Parameters
    → filename (str) - The name of the file to be open. Complete methods are not supported. Related methods are not supported for most locations (you should only specify a filename that does not have part of the path).
    Mode (str) - File mode to open. Files opened in the file system only use this parameter; others will ignore it.
     
  • Return type - file object.

ClassLoader(path= None, script_home= None)

Its main task is to load the program resource files from the disk. The loader contains a search path that can include filesystem directories, ZIP archives, and Python packages.

Variables

  • Path (list of str) –List of search locations. After repairing the path, you must call the reindex path.
  • script_home (str) –Basic resource location, automatically in the application script area.
     

add_font(name) 

It adds a font resource to the application. Unsupported fonts should be added to the pyglet before they can be used with font.load. Although the font has been added with its file name using this function, it is loaded with specifying its family name. 

For example:

resource.add_font('action_man.ttf')
action_man = font.load('Action Man')

 

animation(name, flip_x = False, flip_y = false, rotate = 0, border = 1)  - It Uploads the animation with customizations. Animations uploaded to the same source but with different conversions will use the same texture. It loads the animation with the least transformation. Animations uploaded to the same source but with different conversions will use the same texture.

  • Parameters
    → Name (str) - The file name of the animation source to be uploaded.
    → flip_x (bool) - If true, the restored image will be indexed horizontally.
    flip_y (bool) - If True, the restored image will be flipped over.
    rotate (int) - The returned image will be rotated clockwise with a given number of degrees (90 repetitions).
    boundary (int) - Leaves specific pixels of empty space next to each image in the atlas, which can help reduce bleeding formation.
     
  • Return type - Animation.
     

attributed(name)

It Loads the attributed document.

  • Parameter
    name (str)- Name of the attribute text resource should be loaded in the file.
     
  • Return type- Formatted document.
     

get_cached_animation_names()

It gets a list of animations that have been cached in the filename. It is useful for debugging and profiling only.

  • Return type: list
  • Returns: list of str
     

get_texture_bins()

Get a list of textures to use. It is only used for debugging purposes and profiling only.

  • Return type: list
  • Returns: list of texture Bin.
     

html(name)

Loads the HTML document.

  • Parameter
    → name (str) – It loads the HTML resource to filenames.
     
  • Return type: Formatted Document
     

location(name)

Find a resource location. This method is useful for opening targeted files in the application. For example, an HTML file uploaded as a resource may refer to other images. These images should be available in connection with the HTML file, not individually looking at the upload path.

  • Parameter
    name (str) – It locates the filenames of the resources.
     
  • Return type: location
     

text(name)

It loads the text document.

  • Parameter
    → name (str) – It loads the plain text of the filenames.

 

  • Return type: Unformatted Document
     

texture(name)

It loads the texture.

  • Parameter
    → name(str)- it loads the image of the filename

 

  • Return type: texture
     

get_cached_image_names()

Get a list of images that have been cached. This is useful for debugging.

  • Return type: list
  • Returns: list of str
     

get_cached_texture_names()

It gets the names of textures that are currently cached.

  • Return: list of str.
  • Return type: location.
  • location(name) - Get the location of a resource.
     

This method helps open targeted files in the application. For example, an HTML file uploaded as a resource may refer to other images. These images should be available in connection with the HTML file, not individually looking at the upload path.

ClassURLLocation(base_url)

It uses the urlparse and urllib2 modules to open files on the network given a URL.

open(filename, mode='rb') - Open a file at this location.

 

  • Parameter
    filename (str) – Absolute paths are not supported. Most locations do not support relative paths.
    Mode (str) – Only files are opened on the filesystem using this parameter; others ignore it.

 

  • Return type: file object
     

get_script_home()

Find a directory containing the system login module.

There is a directory containing the __main__ module for standard Python scripts. In the functionality created by py2exe, the result is a directory that includes a functional file. For OS X loads, created using Py2App, the result is the Resource directory within the active pile.

If no one condition is applied and __ main__ file cannot be specified then the active directory is restored.

If a Python profile runs the script, this function may restore the directory where the profile works instead of the actual script's directory. The full path to the original script can be specified in pyglet.resource.path to fixing this behavior

get_settings_path(name)

Get a directory to save user preferences. Different platforms have different settings for where to store user preferences, saved games, and settings. This function uses those arguments. Note that the restored method may not be available: applications must use os.makedirs to build it if required.

  • In Linux, the reference name in the user configuration directory is restored (usually under ~ / .config).
  • In Windows (including under Cygwin), the word list in the user application settings directory is restored.
  • On Mac OS X, the name index under ~ / Library / Application Support is restored.

ClassZIPLocation(zip, dir)

It locates within the ZIP file.

open(filename, mode='rb') -  ItOpens a file at given location.

 

  • Parameter 
    → filename (str) - Complete methods are not supported. Related methods are not supported for most of the locations (you should only specify a filename that does not have part of the path).
    Mode (str) - Files opened in the file system only use this parameter; others will ignore it.

 

  • Return type: file objects.
     

get_script_home()

Find a directory containing system login credentials. For standard Python scripts, there is a directory containing the __main__ module. In the functionality created by py2exe, the result is a directory that contains a functional file. For OS X loads created using Py2App, the result is the Resource directory within the active pile.

If we cannot find the conditions to be true and the__ main__ file cannot be specified if the active directory is restored. If the script is run by a Python profile, this function may restore the directory where the profile works instead of the actual script's directory. To fix this behavior the full path to the original script can be specified in pyglet.resource.path.

  • Return type: str
     

media (name, broadcast = True)

It Downloads audio or video resources. The definition of streaming is media.load. Compressed sources cannot be streamed (i.e., compressed video and audio cannot be streamed from the ZIP archive).

  • Parameter
    → Name (str) - file name of the media source to be uploaded.
    streaming (bool) - it will be true if the source should be streamed from disk, and false if it should be completely removed from memory immediately.

 

  • Return type: media.Source.
     

text(name)

It loads the text document.

  • Parameter
    → name (str) – It loads the plain text of the filenames.

 

  • Return type: Unformatted Document.
     

get_cached_texture_names()

Get the names of textures currently cached.

  • Return: list of str.
  • Return type: location.
  • location(name) - Get the location of a resource.
     

This method helps open targeted files in the application. For example, an HTML file uploaded as a resource may refer to other images. These images should be available in connection with the HTML file, not individually looking at the upload path.
 

html(name)

Loads the HTML document.

  • Parameter
    name (str) – It loads the HTML resource to filenames.

 

  • Return type: Formatted Document.
     

get_cached_image_names()

Get a list of images that have been cached. This is useful for debugging.

  • Return type: list
  • Returns:  list of str
     

animation(nameflip_x=Falseflip_y=Falserotate=0border=1)

It loads the animation with the least transformation. Animations uploaded to the same source but with different conversions will use the same texture.

  • Parameter
    → Name (str) - file name of the animation source to be uploaded.
    flip_x (bool) - If true, the restored image will be indexed horizontally.
    flip_y (bool) - If True, the restored image will be flipped over.
    rotate (int) - The returned image will be rotated clockwise with a given number of degrees   (90 repetitions).
    boundary (int) - Leaves specific pixels of empty space next to each image in the atlas,   which can help reduce bleeding formation.

 

  • Return type - Animation.

Frequently Asked Questions

How do I show a picture in a pyglet?

from pyglet, import image pic = image. load ('picture.png') 

Supported image file types include PNG, BMP, GIF, JPG, and more, depending on the operating system.

pic = image. load ('hint.jpg', file = file obj) 
width, height = image. width, image.
image = image. get_region (x, y, width, height)

 

What are the parameters used in the loader class?

ClassLoader(path=None,script_home=None)

Path (list of str) –List of search locations. After repairing the path, you must call the reindex path.

script_home (str) –Basic resource location, automatically in the application script area.

 

What are the parameters used in the open function of class file location?

open (filename,mode=’rb’) -Open a file at this location.

filename (str) – Absolute paths are not supported. Most locations do not support relative paths.

Mode (str) – Only files are opened on the filesystem using this parameter; others ignore it.

Conclusion 

In this article, we have extensively discussed the pyglet.resource, path format, and some of its functions. We begin with a brief introduction to the pyglet.resource, path format. After that we have discussed the classes which are used in pyglet.resource and functions fall under these classes.

After reading about the pyglet.resource, are you not feeling excited to read/explore more articles on the topic of file systems? Don't worry; Coding Ninjas has you covered. To learn, see Operating SystemUnix File SystemFile System Routing, and File Input/Output.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But suppose you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass