1

Тема: Програма для масового редагування світлин

Доброго дня. Пропоную вашій увазі програму яку я написав для масового редагування світлин.

Програма дає змогу змінити розширення та орієнтацію для всіх світлин у вказаній папці. Програма не замінює оригінальні файли, а сторює нову папку з відредагованими світлинами.

Використані бібліотеки: gtk, gobject, threading, os, shutil, PIL та інші
Програма тестувалася на Linux Mint 15

Інтерфейс:
http://clip2net.com/clip/m40453/1402833667-2014-06-15-150105_274x105_scrot-7kb.png?nocache=1

http://clip2net.com/clip/m40453/1402834553-2014-06-15-151551_276x110_scrot-9kb.png?nocache=1

Програма може бути цікава також тим хто цікавиться ООП, редагуванням фото за допоможою Python, ниттєвим програмуванням, використанням gtk для графічного ітенфейса.

Іконка додаються, лінк на відео з роботою програми - https://dl.dropboxusercontent.com/u/620 … 0%3A57.mkv

Код:

#!/usr/bin/env python
# This Python file uses the following encoding: utf-8
import gtk
import gobject
import threading
import os
import shutil
from time import time
from PIL import Image

resolution = 1600
orientacion = 0

class MainWin:
    def destroy(self, widget, data=None):
        print "{0:.3f}".format(time()-start_time), ":", "destroy signal occurred"
        gtk.main_quit()
        
    def change_resolution(self, widget):
        global resolution
        resolution = int(self.combo1.get_active_text());
        
    def change_orientacion(self, widget):
        global orientacion
        orientacion = int(self.combo2.get_active_text());
        
    def on_folder_clicked(self, widget):
        dialog = gtk.FileChooserDialog("Please choose a folder",
            None,
            gtk.FILE_CHOOSER_ACTION_OPEN,
            (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
            gtk.STOCK_OPEN, gtk.RESPONSE_OK))
        dialog.set_default_size(800, 400)
        dialog.set_action(gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER)
        response = dialog.run()
        if response == gtk.RESPONSE_OK:
            folder = dialog.get_filename()
            print "{0:.3f}".format(time()-start_time), "Select clicked"
            print "{0:.3f}".format(time()-start_time), ":", "Folder selected: " + folder
            os.chdir(folder)
            dialog.destroy()
            directory = folder + '/new'
            if not os.path.exists(directory):
                os.makedirs(directory)
                print "{0:.3f}".format(time()-start_time), ":", "Create folder: " + directory
            thread = threading.Thread(target=self.processing_images, args=(os.listdir("."), directory,))
            thread.setDaemon(True)
            thread.start()
        elif response == gtk.RESPONSE_CANCEL:
            print "{0:.3f}".format(time()-start_time), ":", "Cancel clicked"
            
    def processing_images(self, filess, directory):
        i = 0
        for files in filess:
            if files.endswith(".jpg") or files.endswith(".JPG"):
                try:
                    img = Image.open(files)
                    ratio = 1.0*img.size[0]/img.size[1]
                    if ratio >= 1:
                        img = img.resize((resolution,int(resolution/ratio)), Image.ANTIALIAS)
                    else:
                        img = img.resize((int(resolution*ratio),resolution), Image.ANTIALIAS)
                    img = img.rotate(orientacion, expand=True)
                    img.save(directory + '/' + files)
                    i += 1
                    gobject.idle_add(self.set_progress_bar_fraction, i, len(filess)-1)
                    print "{0:.3f}".format(time()-start_time), 'resize', files, 'to', resolution, 'and rotate to', orientacion        
                except Exception,e:
                    print "{0:.3f}".format(time()-start_time), ":", str(e), 'with file:', files
            
    def set_progress_bar_fraction(self, i, n):
        self.progressbar.set_text(str(i) + ' from ' + str(n))
        self.progressbar.set_fraction(1.0*i/n)

    def __init__(self):
        global start_time
        gtk.gdk.threads_init()        
        start_time = time()
        
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_title('Photo processing')
        self.window.set_size_request(270,80)
        self.window.set_border_width(10)
        color = gtk.gdk.color_parse('#777')
        self.window.modify_bg(gtk.STATE_NORMAL, color)
        try:
            self.window.set_icon_from_file("test.png")
        except Exception,e:
            print "{0:.3f}".format(time()-start_time), ":", str(e)
            
        self.window.connect("destroy", self.destroy)
        
        self.b_open = gtk.Button("Select folder")
        self.b_open.connect("clicked", self.on_folder_clicked)
        
        self.combo1 = gtk.combo_box_new_text()
        self.combo1.append_text("800")
        self.combo1.append_text("1024")
        self.combo1.append_text("1600")
        self.combo1.append_text("2400")
        self.combo1.set_active(2)
        self.combo1.set_size_request(40,30)
        self.combo1.connect('changed', self.change_resolution)
        
        self.combo2 = gtk.combo_box_new_text()
        self.combo2.append_text("0")
        self.combo2.append_text("90")
        self.combo2.append_text("180")
        self.combo2.append_text("270")
        self.combo2.set_active(0)
        self.combo2.set_size_request(40,30)
        self.combo2.connect('changed', self.change_orientacion)
        
        self.progressbar = gtk.ProgressBar(adjustment=None)
        
        self.vbox = gtk.VBox(spacing=6)
        self.hbox = gtk.HBox()
        self.hbox.pack_start(self.combo1)
        self.hbox.pack_start(self.combo2)
        self.hbox.pack_start(self.b_open)
        self.vbox.pack_start(self.hbox, False)
        self.vbox.pack_start(self.progressbar, False)
        self.window.add(self.vbox)
        self.window.show_all()

    def main(self):
        gtk.main()

if __name__ == "__main__":
    MainWin().main()
Post's attachments

test.png 9.73 kb, 204 downloads since 2014-06-15 

Подякували: 0xDADA11C71