venerdì 11 novembre 2016

Python Vs Livecode

In giro ho trovato il seguente script python per aggiungere la filigrana (watermark) al centro di un'immagine:
import sys
import os
import Image

def modifica(filename, watermarkname):
        photo = Image.open(filename)
        watermark = Image.open(watermarkname)

        Pwidth = photo.size[0]
        Pheight = photo.size[1]

        Wwidth = watermark.size[0]
        Wheight = watermark.size[1]

        x = (Pwidth/2)-(Wwidth/2)
        y =  (Pheight/2)-(Wheight/2)

        photo.paste(watermark, (x, y), watermark)
        photo.save(filename.split(".")[0]+"_watermark.jpg")



if len(sys.argv) < 3:
    print "Esempio d'uso: python watermark.py foto.jpg watermark.png"
    sys.exit()

foto = os.path.abspath(sys.argv[1])
watermark = os.path.abspath(sys.argv[2])

if os.path.isdir(foto):
    for file in os.listdir(foto):
        modifica(foto+"/"+file, watermark)
else:
    modifica(foto, watermark)

Sono 28 righe di codice abbastanza semplice da capire.
In livecode si può fare come puro script con:

on OpenCard
   if $# <> 3 then
      put "Esempio d'uso: livecode -ui watermark.livecode foto.jpg   watermark.png"
      quit 64 #command line usage error
   end if
   import paint from file $2
   set the name of last image to watermark
   if last char of $1 is "/" then
      set the defaultFolder to $1
      put files() into lista      
   else
      put $1 into lista
   end if   
   repeat for each line tFile in lista
      applicaW tFile
   end repeat
   quit
end openCard

on applicaW tFile
   import paint from file tFile
   set the name of last image to foto
   set the loc of image watermark to the loc of image foto   
   set itemdel to "."
   put item 1 of tFile into tFile
   export snapshot from rect (the rect of image foto) of this card to file ( tFile & "_watermark.jpg") as JPEG
   delete image foto
end applicaW

Circa 27 righe. La gestione della posizione è più semplice in livecode, perchè la proprietà loc è il baricentro dell'oggetto e basta farli coincidere per mettere al centro di un'immagine la filigrana.
A sfavore del Python è il sistema di gestione dei blocchi di codice solo su indentazione, è un attimo fare degli errori. Al contrario in Livecode è l'indentazione è automatica, e i cicli sono racchiusi da delimitatori ben chiari tipo ON/END.
Voi che ne pensate?

Nessun commento:

Posta un commento