views:

60

answers:

3

Regards to all. I'm developing a Image compression system using Python Image Library. The basic workflow is:

  • Read all images of a certain directory with : find /opt/images -name *.jpg > /opt/rs/images.txt
  • Read this file and storage the result in a Python list
  • Iterate the list, create a Image object and passing like a argument to the compress function
  • and, copy the resulting image on a certain directory which is buit depending of the name of the image.

A example: /opt/buzon/17_499999_1_00000000_00000999_1.jpg This is the real name of the image: The final result is: 17_499999.jpg The ouput directory is: /opt/ftp and should be storaged on this way: 1- first partition 00000000 - second partition 00000999 - third partition 1- this flag if to decide if we have to compress this image or not (1 is False, 0 is True)

For that reason the final path of the image is: /opt/ftp/1/00000000/00000999/17_499999.jpg for the original copy /opt/ftp/1/00000000/00000999/17_499999_tumb.jpg

Now, Where is the problem. When I read the file where I storage the result of the find command, each line of the file has the \n character.

How I can replace with regular expressions this?

The completed source code is this. Any suggests is welcome.


import Image, os ,sys, re, subprocess, shlex
import ConfigParser
from symbol import except_clause

CONFIG_FILE = "/opt/scripts/config.txt"
config = ConfigParser.ConfigParser()   
config.read(CONFIG_FILE)

entry_dir = config.get('directories', 'entry_dir')
out_dir = config.get('directories', 'out_dir')

def resize(img, box, fit, out):
    '''Downsample the image.

    @param img: Un objeto de la clase Imagen
    @param box: tuple(x, y) - El recuadro de la imagen resultante
    @param fix: boolean - el corte de la imagen para llenar el rectangulo
    @param out: objeto de tipo fichero -  guarda la imagen hacia la salida estandar
    '''
    # prepara la imagen con un factor de 2, 4, 8 y el algoritmo mas rapido        
    factor = 1
    while img.size[0]/factor > 2*box[0] and img.size[1]*2/factor > 2*box[1]:
      factor *=2
      if factor > 1:
          img.thumbnail((img.size[0]/factor, img.size[1]/factor), Image.NEAREST)
          # Aqui se calcula el rectangulo optimo para la imagen

      if fit:
          x1 = y1 = 0
          x2, y2 = img.size
          wRatio = 1.0 * x2/box[0]
          hRatio = 1.0 * y2/box[1]
          if hRatio > wRatio:
              y1 = y2/2-box[1]*wRatio/2
              y2 = y2/2+box[1]*wRatio/2
          else:
              x1 = x2/2-box[0]*hRatio/2
              x2 = x2/2+box[0]*hRatio/2
              # Este metodo es para manipular rectangulos de una determinada imagen
              # definido por 4 valores que representan las coordenadas: izquierda,
              # arriba, derecha y abajo
              img = img.crop((x1,y1,x2,y2))
            # Le damos el nuevo tamanno a la imagen con el algoritmo de mejor calidad(ANTI-ALIAS)
              img.thumbnail(box, Image.ANTIALIAS)
    return img

def removeFiles(directory):
    """
    Funcion para borrar las imagenes luego de ser procesadas
    """
    for f in os.listdir(directory):
     path = os.path.abspath(f)
     if re.match("*.jpg", path):
         try:
             print "Erasing %s ..." % (path)
             os.remove(path)
         except os.error:
             pass

def case_partition(img):
    """
    @desc: Esta funcion es la que realmente guarda la imagen en la
    particion 0
    @param: imagen a guardar
    @output_dir: Directorio a guardar las imagenes
    """
    nombre_imagen = img
    nombre_import = os.path.splitext(nombre_imagen) 
    temp = nombre_import[0]
    nombre_real = temp.split('_')

    if nombre_real[4] == 0:
         ouput_file = nombre_real[0] + nombre_real[1] + ".jpg"
         output_dir = out_dir + "/%d/%d/%d/" % (nombre_real[2], nombre_real[3], nombre_real[4])
         if os.path.isdir(output_dir):
              os.chdir(output_dir)
              img.save(output_file, "JPEG", quality=75)
         else:
              create_out_dir(output_dir)
              os.chdir(output_dir)
              img.save(output_file)
    else:    
         print "Esta imagen sera comprimida"
         img = resize(img, 200, 200, 200) ## FIXME Definir el tamano de la imagen
         # Salvamos la imagen hacia un objeto de tipo file
         # con la calidad en 75% en JPEG y el nombre definido por los especialistas
         # Este nombre no esta definido......
        # FIXME
         output_file = nombre_real[0] + nombre_path[1] + "_.jpg"
         output_dir = out_dir + "/%d/%d" % (nombre_real[2], nombre_real[3], nombre_real[4]) 
         if os.path.isdir(output_dir):
              os.chdir(out)
              img.save(output_file, "JPEG", quality=75)
         else:
             create_out_dir(output_dir)
             os.chdir(output_dir)
             img.save(output_file, "JPEG", quality=75)

if __name__ == "__main__":
   find = "find %s -name *.jpg > /opt/scripts/images.txt" % entry_dir
   args = shlex.split(find)
   p = subprocess.Popen(args)
   f = open('/opt/scripts/images.txt', "r")   
   images = []

   for line in f:
     images.append(line)

   f.close()
   for i in images:
       img = Image.open(filename) # Here is the error when I try to open a file
       case_partition(img)        # with the format '/opt/buzon/15_498_3_00000000_00000000_1.jpg\n'
                                  # and the \n character is which I want to replace with nothing
   removeFiles(entry_dir)         #
                                  #

Regards

+2  A: 

Assuming s is the string with the carriage return, you may use s.strip("\n") to remove carriage returns at the corners of the string. No need for regular expressions there.

toaster
+1  A: 

I guess the relevant code lines are these:

for line in f:
    images.append(line)

to remove the \n, you can simply call strip() on the string:

for line in f:
    images.append(line.strip())
piquadrat
A: 

there are many ways to do that without using regexp simpliest and correct one is use images.append(line.rstrip("\n")), also you can use images.append(line[:-1])

Also you can use glob() module instead call 'find' command through shell. It will return result as python list without having to use the files. ex: images=glob.glob("*.jpg"). http://docs.python.org/library/glob.html?highlight=glob

seriyPS