#!/usr/bin/env python
"""
Use ImageMagick's mogrify command to resize and reformat image files.

2016 . Arno Klein (arno@binarybottle.com) . Apache v2.0 License
"""
import os
import sys
from subprocess import call

raw_files_path = '/home/pupating/culturalentomology.org/raw_files'
converted_images_path = '/home/pupating/culturalentomology.org/converted_images'
image_extensions = ['bmp','gif','jpg','jpeg','pjpeg','png','tif','tiff',
                    'BMP','GIF','JPG','JPEG','PJPEG','PNG','TIF','TIFF']
new_extension = 'jpg'
resize_dims = '800x'
quality = '100'
cmd = 'convert'

raw_files = os.listdir(raw_files_path)
converted_images = os.listdir(converted_images_path)

# Loop through raw files:
for raw_file in raw_files:

    # If raw file has an acceptable image file extension:
    exploded_filename = raw_file.split('.')
    filestem = '.'.join(exploded_filename[:-1])
    extension = exploded_filename[-1]
    if extension in image_extensions:

        # If the file has not already been converted:
        converted_file = os.path.join('.'.join([filestem, new_extension]))
        if converted_file not in converted_images:

            input_file = '"{0}"'.format(os.path.join(raw_files_path,
                                                     raw_file))
            output_file = '"{0}"'.format(os.path.join(converted_images_path,
                                                      converted_file))
            arg_resize = " -adaptive-resize " + resize_dims
            args = [cmd, input_file, arg_resize, output_file]
            command = " ".join(args)
            print(command)
            try:
                retcode = call(command, shell=True)
                if retcode < 0:
                    print("Child was terminated by signal", -retcode)
                elif retcode == 0:
                    pass
                else:
                    print("Child returned {0}".format(retcode))
            except OSError as e:
                print("Execution failed: {0}".format(e))

            # Change permissions:
            command2 = "chmod 755 " + output_file
            print(command2)
            try:
                retcode = call(command2, shell=True)
                if retcode < 0:
                    print("Child was terminated by signal", -retcode)
                elif retcode == 0:
                    pass
                else:
                    print("Child returned {0}".format(retcode))
            except OSError as e:
                print("Execution failed: {0}".format(e))

@@ -538,16 +538,25 @@
 _dims = 
+'1200x' #
 '800x'%0Aq
@@ -1040,29 +1040,16 @@
 _file = 
-os.path.join(
 '.'.join
@@ -1075,17 +1075,16 @@
 ension%5D)
-)
 %0A       
@@ -2003,16 +2003,419 @@
 at(e))%0A%0A
+            # Update list; if .tif file converted to FILE-0.jpg instead of FILE.jpg:%0A            converted_file0 = '.'.join(%5Bfilestem + '-0', new_extension%5D)%0A            output_file0 = os.path.join(converted_images_path, converted_file0)%0A            converted_images = os.listdir(converted_images_path)%0A            if converted_file not in converted_images and converted_file0 in converted_images:%0A%0A    
         
@@ -2452,24 +2452,28 @@
         
+  
+  
 command
-2
+3
  = %22chmo
@@ -2493,17 +2493,19 @@
 put_file
-%0A
+0%0A 
         
@@ -2500,32 +2500,35 @@
 e0%0A             
+   
 print(command2)%0A
@@ -2524,17 +2524,17 @@
 (command
-2
+3
 )%0A      
@@ -2531,32 +2531,36 @@
 d3)%0A            
+ 
+   
 try:%0A           
@@ -2556,32 +2556,36 @@
                 
+   
+ 
 retcode = call(c
@@ -2590,17 +2590,1135 @@
 (command
-2
+3, shell=True)%0A                    if retcode %3C 0:%0A                        print(%22Child was terminated by signal%22, -retcode)%0A                    elif retcode == 0:%0A                        pass%0A                    else:%0A                        print(%22Child returned %7B0%7D%22.format(retcode))%0A                except OSError as e:%0A                    print(%22Execution failed: %7B0%7D%22.format(e))%0A                %0A                command4 = %22mv %22 + output_file0 + %22 %22 + output_file%0A                print(command4)%0A                try:%0A                    retcode = call(command4, shell=True)%0A                    if retcode %3C 0:%0A                        print(%22Child was terminated by signal%22, -retcode)%0A                    elif retcode == 0:%0A                        pass%0A                    else:%0A                        print(%22Child returned %7B0%7D%22.format(retcode))%0A                except OSError as e:%0A                    print(%22Execution failed: %7B0%7D%22.format(e))%0A%0A            # Change permissions:%0A            command5 = %22chmod 755 %22 + output_file%0A            print(command5)%0A            try:%0A                retcode = call(command5
 , shell=

Revise convert_images.py to account for when files get a '-0.jpg' extension