lundi 28 juillet 2014

ClamAV - Un anti-virus pour mon Linux

Hé oui, ce n'est pas parce que Linux est plus sécurisé qu'il n'est pas totalement immunisé.

En cas de doute, sachez qu'il existe aussi des anti-virus pour Linux

jeudi 24 juillet 2014

Ecrire un script Python pour Gimp

En plein traitement d'image pour standardiser les images introduites dans notre base d'information (en vue de constituer un catalogue), il y a un certain nombre d'opération identiques que je réalise systématiquement.

Au bout de 1700 images traitée (et encore autant à faire), certaines routines de traitement se dégagent clairement. Gimp disposant de bon raccourcis claviers, les opérations sont assez rapide... mais il y a encore moyen de faire mieux à l'aide d'un script.

Sur base des informations trouvées dans l'article "Python-Fu #2 - Yout first Python-Fu plugin" écrit par Frédéric Jaume, je me suis lancé dans le test et la réalisation d'un script pour automatiser le cadrage final de nos image.

Un log pour Python-Fu sous Gimp
Le plus compliqué ne fut pas de créer le script (un peu d'expérience Python cela aide), mais d'avoir un système de log suffisamment efficace pour tracer le script.
La méthode la plus simple est de rediriger  stdout et stderr dans un fichier (écrasé à chaque exécution du script)
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# GIMP Python plug-in template.

from gimpfu import *
import sys

__DEBUG = True

# Redirect the print statement to file (usefull for debugging)
if __DEBUG:
  sys.stderr = open( 'c:\\temp\\gimpstderr.txt', 'w')
  sys.stdout = open( 'c:\\temp\\gimpstdout.txt', 'w')

def gimp_log( sMsg ):
  """ Log to StdOut (or nothing) depending on the __DEGUG flag """
  if __DEBUG:
    print( sMsg )
  else:
    pass


Script final
Voici la version finale du script gg_crop_resize.py, qu'il faut placer dans le répertoire plug-ins.


#!/usr/bin/env python
# -*- coding: utf-8 -*-

# GIMP Python plug-in template.

from gimpfu import *
import sys

__DEBUG = True

# Redirect the print statement to file (usefull for debugging)
if __DEBUG:
  sys.stderr = open( 'c:\\temp\\gimpstderr.txt', 'w')
  sys.stdout = open( 'c:\\temp\\gimpstdout.txt', 'w')

def gimp_log( sMsg ):
  """ Log to StdOut (or nothing) depending on the __DEGUG flag """
  if __DEBUG:
    print( sMsg )
  else:
    pass
    
# Display a message in the status bar
#    pdb.gimp_message("test")

# Some functions about progress 
#
#    pdb.gimp_progress_init( 'Test2', None )
#    pdb.gimp_progress_set_text('This goes to the status bar')
#    pdb.gimp_progress_pulse()
#    pdb.python_fu_console()
#    pdb.gimp_progress_end()

def do_gg_crop_resize():
    """ Crop image to selection THEN scale image to fit properly a 2848 x 2848 
        picture THEN resize Canvas to fit photo format 4282 x 2848 (image 
        centered)"""

    # Diplay message in the bottom of the console
    pdb.gimp_message("GG Crop Resize...")
  
    # Print to the standard output (which is a file)    
    gimp_log( "do_gg_crop_resize:")
     
    # Set up an undo group, so the operation will be undone in one step.
    #pdb.gimp_undo_push_group_start(img)

    # NB: The last opened file is always at index 0, previous opened file 
    #     at index 1 and so on.
    count = len( gimp.image_list() )
    img = gimp.image_list()[count-1]
    sel = pdb.gimp_selection_bounds(img)
    if sel[0] == 0:
      pdb.gimp_message("GG Crop Resize Stopped - No selection!")
      gimp_log( "Stopped! No selection" )
      return
      
    # Crop the image 
    pdb.gimp_image_crop( img, sel[3]-sel[1], sel[4]-sel[2], sel[1], sel[2] )
    gimp_log( "Current Size (width,height)=(%i,%i)" % (img.width,img.height) )
    
    # Calculate Image Ratio
    ratio = img.height / float(img.width)
    gimp_log( "Current Ratio Height/width = %f" % (ratio) )
    
    if img.width > img.height:
      gimp_log( "Image is Larger THEN scale width " )
      pdb.gimp_image_scale( img, 2848, 2848 * ratio ) # New Width, New Height
      pdb.gimp_image_resize( img, 4282, 2848, (4282-img.width)/2, (2848-img.height)/2 )
      
      
    else:
      gimp_log( "Image is Higher THEN scale height ")
      pdb.gimp_image_scale( img, 2848 / ratio, 2848 )
      pdb.gimp_image_resize( img, 4282, 2848, (4282-img.width)/2, (2848-img.height)/2 )
      
    pdb.gimp_message( "GG Crop Resize completed" ) 
    gimp_log( "Treatment complete" )
    # Close the undo group.
    #pdb.gimp_undo_push_group_end(img)

register(
    "gg_crop_resize",
    "Crop and resize to photo",
    "Crop to selection, resize and center to Photo dimensions",
    "Meurisse D.",
    "Guy Gerard SPRL",
    "2014",
    "GG Crop Resize", # Menu item Name
    "*",      # Alternately use RGB, RGB*, GRAY*, INDEXED etc.
    [],
    [],
    do_gg_crop_resize, 
    menu="<Image>/GG"  # Host menu for the MenuItem
    
)

main()

Une fois Gimp redémarré, le point de menu apparait dans le menu principal de gimp (version 2.8 dans mon cas)