Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Png extracting workflow for ComfyUI (Better version )

requires PIL 
 so maybe do a 
 $pip install PIL 
 beforehand

import argparse import PIL.Image import PIL.PngImagePlugin def extract_text(filename): """Extracts text from a PNG file.""" img = PIL.Image.open(filename) info = img.text["workflow"] return info ## usage python readpng.py ~/Downloads/example.png > comfy_workflow.json def main(): """Parses arguments and extracts text from a PNG file.""" parser = argparse.ArgumentParser(description="Extracts text from a PNG file.") parser.add_argument("filename", help="The PNG file to extract text from.") args = parser.parse_args() text = extract_text(args.filename) print(text) if __name__ == "__main__": main()

Extracting metadata from a png file in Python

 A very simple program using PIL to extract the png text embedded as metadata by comfyUI 


import PIL.Image
import argparse

def extract_text(filename):
    png = PIL.Image.open(filename)
    return png.text

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='png metadata infor')
    parser.add_argument('filename', help='png File to check')
    args = parser.parse_args()
    text = extract_text(args.filename)
    print(text)



Error downloading audio: get_throttling_function_name: could not find match for multiple

 In the past I had posted this https://alashazam.blogspot.com/2023/05/python-program-to-download-music.html explaining how to download video using python library pytube

If you are still using this program, you will notice that it is recently not functional anymore 

As of end of June 2023, this program will crash with this error:

Error downloading audio: get_throttling_function_name: could not find match for multiple

and if you are investigating further you will also discover this error

pytube.exceptions.RegexMatchError: get_throttling_function_name: could not find match for multiple
During handling of the above exception, another exception occurred:

To fix the error - you will have to edit the file /lib/python3.10/site-packages/pytube/cipher.py
on line +287 and remove the ; character

so the new file will be like this


Once that change is made the pytube library will work again.

How to prevent outside updates to break a Python program

There are several ways to freeze a Python environment to prevent it from being impacted by updates to the operating system or other software.

One common way to do this is to use a virtual environment, which is a self-contained environment that isolates the Python interpreter and any installed packages from the system-wide installation.
This allows you to install and manage specific versions of packages and dependencies without affecting other Python applications on the system.

To create a virtual environment, you can use the built-in venv module in Python 3:

$ python3 -m venv myenv

This will create a new virtual environment in the myenv directory. You can activate the virtual environment by running the activate script in the bin directory:

$ source myenv/bin/activate

Once the virtual environment is activated, you can install packages using pip without affecting other Python applications on the system:

$ pip install mypackage

To freeze the environment, you can use the pip freeze command to generate a list of installed packages and their versions:

$ pip freeze > requirements.txt

This will create a file called requirements.txt that contains a list of all installed packages and their versions.
You can then use this file to recreate the environment on another system or to restore the environment if it becomes corrupted or outdated:

$ pip install -r requirements.txt

By using a virtual environment and freezing the requirements,
you can ensure that your Python environment remains stable and consistent,
even as the underlying system and software changes.
Like a time capsule



Linux script for youtube-dl


 #!/bin/bash
function usage
{
    echo "usage: yl [youtube-link]"
}
if [ "$1" != "" ]; then
 echo "Downloading $1"
 cd ~/Music
 echo "/usr/local/bin/youtube-dl --extract-audio --audio-quality 128K  --audio-format mp3 -l $1"
 /usr/local/bin/youtube-dl --extract-audio --audio-quality 128K  --audio-format mp3 -l $1
 echo "Done!"
fi



Downloading songs as mp3

Fetching mp3 from youtube video on Ubuntu Linux

the selected tool is 'youtube-dl'

from the terminal prompt write:

$pip install youtube-dl

but then you get this error (because we are March 2023 and things are broken)

ERROR: Unable to extract uploader id; please report this issue on https://yt-dl.org/bug . Make sure you are using the latest version; see  https://yt-dl.org/update  on how to update. Be sure to call youtube-dl with the --verbose flag and include its complete output.

for the long version of this error:

ERROR: Unable to extract uploader id; please report this issue on https://yt-dl.org/bug . Make sure you are using the latest version; see  https://yt-dl.org/update  on how to update. Be sure to call youtube-dl with the --verbose flag and include its complete output.

    'uploader_id': self._search_regex(r'/(?:channel|user)/([^/?&#]+)', owner_profile_url, 'uploader id') if owner_profile_url else None,

  File "/home/_dl/extractor/common.py", line 1012, in _search_regex

    raise RegexNotFoundError('Unable to extract %s' % _name)

youtube_dl.utils.RegexNotFoundError: Unable to extract uploader id; please report this issue on https://yt-dl.org/bug . Make sure you are using the latest version; see  https://yt-dl.org/update  on how to update. Be sure to call youtube-dl with the --verbose flag and include its complete output.


To fix this error you will have to type this:

$ pip uninstall youtube-dl

then type this:
$pip install git+https://github.com/ytdl-org/youtube-dl.git@master#egg=youtube_dl

but that means you need to install git
$ apt install git 
will do

while you're installing new package don't forget to get ffmpeg as well
$ apt install ffmpeg

Now you're ready to get the song you want




Training a lora (stable diffusion) [part 1]

Step by step Lora Training

Training a model is very demanding, and the result is a minimum 2GB file ; while a Lora (Low-Rank Adaptation) will be less than 150MB.

Where to start?

I would recommend this google colab link

Linaqruf/kohya-trainer/blob/main/kohya-LoRA-dreambooth.ipynb 

Step 1:

Requirements: The preparation of a set of images, and texts (dataset)

Another requirements is a google account (to use the colab link and google drive)

Each image and text's name must match - the size of the images should be 512x512 pixels 

It is convenient to use https://www.birme.net/ to resize the dataset.

For the text, best is to give a little description on each file, or to use the img2txt feature and get the terms directly from the AI.

 Only 5 images (and texts) are going to suffice, but 15 or more will make the model more reliable.

Once the dataset is complete, put these files into a folder with the number of repeats (5 or 10) and the concept (something like exp, or person, or Bcloth, etc ...)

Once ready add the datasets into the google drive

Step 2: 

Open the colab trainer

Go to github.com/linaqruf/kohya-trainer

and pick Kohya Lora Dreambooth


To be continued ...



First step with Python



Why learning a programming language is a good thing?

With the raise of the personal computer back in the 90s, users had only one thing to do most of the time and that was programming - because back in the old days of gw-basic, ms-dos, borland C, and Turbo Pascal  - there was not many games, and the internet was not yet a real thing.

The big thing at that time was to develop programs, these could be ms-dos batch files, because users wanted to automate some tasks, or to tweak their autoexec.bat to optimize their system.
Or was it Pascal file, because that's what was taught in school, where IT class were all about learning algorithms, and how to draw fractals, and solve the Tower of Hanoi kind of things.

Back in the days, one page of code could get you a game (might it be Tetris, or Snake, or Arkanoid) - the game were simple and there was more fun in the programming than in the gaming itself.

Nowadays, programs are back to the programmers - most users won't even try to read a source code.

There might be an exception to this with Linux Users - because sometime they have to look under the hood and understand what is going astray with their system after an update...

Back to the question - why is it a good thing to learn a programming language?

Because it sets you free ... with as little as 4 or 5 days of learning you would have started to understand your computer from the inside, you would learn what a compiler is, how and why sometime your computer seems to freeze - and other very useful troubleshooting technique.

For quick little thing, Python can be used as a more advanced scripting language than will take the user further than what the Linux shell will usually get them.

Another cool thing is when you don't have a calculator close at hand - you open a terminal window and type "python" and do your calculation ... it's very handy.


Why Python as a first language?

Educators recommend Python as the beginner language of choice because it is simple and yet has great capabilities. Python code is easy to read and setup good programming style (like indenting), without being too strict on the syntax (compare to c++ or Java who requires to add a semicolon at the end of each line).

Python is included on Linux OS - usually Python 2.7 ; it is include on mac system too.

Without any addition you can have access to many library - to manage text, math, time, and many more. And after you have mastered all these you can then access additional libraries that will open your code to graphical interface (if you're running Linux - these additional libraries are easily installed using synaptic - for example for games e.g: sudo apt-get install pyGames )

Another good thing with Python is the overwhelming list of free documentations, resources, video tutorials and help you will find on the internet.

Highly recommended :

Related Posts with Thumbnails