Les membres ayant 30 points peuvent parler sur les canaux annonces, projets et hs du chat.

Forum Casio - Projets de programmation


Index du Forum » Projets de programmation » img2py and pyimage for MicroPython
Calamari Hors ligne Membre Points: 229 Défis: 0 Message

img2py and pyimage for MicroPython

Posté le 21/08/2022 13:35

I wanted to load and display images from files in Python on my fx-9750GIII, rather than creating them from a bunch of set_pixel() calls. There's no PIL, of course. And the open() command doesn't seem to work in MicroPython. However, imports do! So, I decided to abuse use them.

I wrote a small Python script that ASCII encodes and RLE compresses a black and white image as an importable Casio MicroPython module. The pyimage.py module provides draw_pyimage(), which a script can use to then decode and draw the image at a given (x, y). If you'd like to try it, the source is here: GitHub: img2py. It works, but it's in a very early state. Please let me know if you have any ideas for improving it.




Lephenixnoir En ligne Administrateur Points: 22872 Défis: 149 Message

Citer : Posté le 27/08/2022 14:13 | #


I forgot to follow up on this: I was able to compile the v1.9.4 tag of the micropython repo to act like the calculator (at least for NUL's).

Thanks for mentioning the version and option, we can never have too much notes on building MicroPython!

How can that be done? I couldn't seem to get Shift-8 CLIP to work in input() or in the SHELL.

I was really convinced you could... now I'm disappointed you can't!

Right, an add-in could access the captures (although only libfxcg has the APIs for it right now) so it's easier than the PC. But then if you have an add-in we might as well use a different version of Python. You see, when dealing with these kinds of problem in the old BASIC days, using add-ins wasn't an option because many models didn't support them. But now every officially-Python-supporting CASIO calculator also has add-ins, so we can just hammer away at our problems with C. x)

Ultimately I agree with you it's much simpler to save data from an add-in, it's just that not everyone knows how to write them.

Here's what's currently implemented. It's not much, but honestly a lot of stuff really isn't needed, as the compressed fs is read only and there is no directory tree or file attributes other than size:
* builtin stuff: open, with, iteration (such as for loops)
* os/os.path: listdir, remove, exists, getsize. These methods are available once a filesystem has been imported, such as: from filesystem_module_name import MPFS
* io: read, readall, readline, readlines, seek, tell, close. These methods may be called on the Mpfs object returned from open()

That's quite a bit actually! It also seems enough to implement robust asset loading with some sweet automation... and compression too, dang. :o
Calamari Hors ligne Membre Points: 229 Défis: 0 Message

Citer : Posté le 28/08/2022 01:10 | #


mpfs is now available on GitHub: mpfs: MicroPython File System
Calamari Hors ligne Membre Points: 229 Défis: 0 Message

Citer : Posté le 28/08/2022 22:02 | #


Made another image converter, here. This one doesn't try to compress. Instead, it creates a Python string for a B&W image that is valid in both Python 3 and MicroPython and remains intact after being saved in Visual Studio Code. It saves the image 7 bits at a time. Then, it attempts to find an offset it can add to each byte (mod 128) that will result in no restricted characters being used (NUL, CR, LF, ", \). For most small and non-random images this will probably be successful, however it's not guaranteed.

The first byte of the string is the (offset + 1), the 2nd is the image width, and 3rd is image height. Then, the image bytes follow.

Here's a minified function to draw such an image:
def draw_image(i,x,y):
  o,w,h=i[0:3];o-=1;w=(w-o)%128;h=(h-o)%128;z=0;u=0
  for j in range(3,len(i)):
    v=(i[j]-o)%128
    for _ in range(7):
      p=v&1;v>>=1
      if p:set_pixel(x+z,y+u)
      z+=1
      if z>=w:z=0;u+=1
      if u>=h:return

Note: The offset, width and height can also just be hardcoded in the program using the image, rather than being stored in the Python string, making draw_image() smaller.

LienAjouter une imageAjouter une vidéoAjouter un lien vers un profilAjouter du codeCiterAjouter un spoiler(texte affichable/masquable par un clic)Ajouter une barre de progressionItaliqueGrasSoulignéAfficher du texte barréCentréJustifiéPlus petitPlus grandPlus de smileys !
Cliquez pour épingler Cliquez pour détacher Cliquez pour fermer
Alignement de l'image: Redimensionnement de l'image (en pixel):
Afficher la liste des membres
:bow: :cool: :good: :love: ^^
:omg: :fusil: :aie: :argh: :mdr:
:boulet2: :thx: :champ: :whistle: :bounce:
valider
 :)  ;)  :D  :p
 :lol:  8)  :(  :@
 0_0  :oops:  :grr:  :E
 :O  :sry:  :mmm:  :waza:
 :'(  :here:  ^^  >:)

Σ π θ ± α β γ δ Δ σ λ
Veuillez donner la réponse en chiffre
Vous devez activer le Javascript dans votre navigateur pour pouvoir valider ce formulaire.

Si vous n'avez pas volontairement désactivé cette fonctionnalité de votre navigateur, il s'agit probablement d'un bug : contactez l'équipe de Planète Casio.

Planète Casio v42 © créé par Neuronix et Muelsaco 2004 - 2023 | Il y a 69 connectés | Nous contacter | Qui sommes-nous ? | Licences et remerciements

Planète Casio est un site communautaire non affilié à Casio. Toute reproduction de Planète Casio, même partielle, est interdite.
Les programmes et autres publications présentes sur Planète Casio restent la propriété de leurs auteurs et peuvent être soumis à des licences ou copyrights.
CASIO est une marque déposée par CASIO Computer Co., Ltd