Automatic import of images: Multiple image directories: Difference between revisions

From Scribus Wiki
Jump to navigation Jump to search
(Initial post)
 
(Script now supports image titles from accompanying text file)
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
As an amateur photographer, I take a lot of pictures and in general I post them to a web site with one or more galleries being generated most days. For general purposes, that's enough. Occasionally, however, I've found it useful to have a printed version of my images, to be used more as like the contact sheet of old than as publication quality images. I'd like these documents to have small versions of all the images in each gallery, labeled with the file name and an option title that identified the subject of the photograph. Since the display of images on the web site is driven by text files with file names, titles, and descriptions, I it made sense to reuse that text file for labeling the pictures on the contact sheets.
Starting with the script found on the page [[Automatic import of images: Versions not requiring Tkinter#album_letter_2.py]], I have created a new script that will take a collection of image directories and create 12-images-per-page documents from each of them in turn, save the documents and generate PDF files from each.
Starting with the script found on the page [[Automatic import of images: Versions not requiring Tkinter#album_letter_2.py]], I have created a new script that will take a collection of image directories and create 12-images-per-page documents from each of them in turn, save the documents and generate PDF files from each.


The initial input and output directories are specified in the code and will need to be changed based on your own configuration. They are defined on lines 6 and 7. There are many other settings that may be adjusted but this should work well enough as a starting point.
The initial input and output directories are specified in the code and will need to be changed based on your own configuration. They are defined on lines 27 and 31. The file that I use to list the images is index.txt but this file name may also be changed on line 42. Finally, the font to use is set on line 45. While there are many other settings that may be adjusted, the script should work well enough with just those four items set. Note that the script will not create the output directory so you'll need to make sure it exists before you start.
 
The basic idea here is to go through the source directory and gather all the sub-directory names. Then, go through each of those sub-directories, open the index.txt file, and generate a document with the images listed there, 12 per page. The document places the sub-directory name at the top of each page, arranges the images in four rows of three images with the file names centered under each. Page numbers are added. Since I print these documents double sided, the page numbers move from side to side and the entire page contents are shifted a little to the right (odd numbered pages) or the left (even numbered pages). Each gallery gets its own file.
 
Once generated, an SLA file is saved in the outputDirectory specified on line 31. Then, a PDF is generated with all pages and saved to the same location. Both files are named the same as the image sub-directory but with .sla and .pdf extensions respectively.
 
'''CAUTION''': if you have directories with too many or very large image files, this can take a long while to run. Documents for directories with a couple dozen images of the appropriate size are created, saved, and exported to PDF in a matter of seconds. Documents for directories with images much larger than necessary or with more than a couple hundred images can take many minutes to be created. Since the images are printed at just over 1.4 by 2.1 inches, even at 300 dpi they will only need to be 420 by 633 pixels and you can probably get away with less. I would be cautious running this script on directories with lots of images or those copied directly from your digital camera. I've successfully produced documents with 640 images this way but it took a good while.
 
For publication to my web site I use a script that creates thumbnail, small, and large versions of each photo. Since the small size images are 467 by 700 pixels, I use those when generating contact sheets with this script. That works quite well and I've created documents with 1,500 images without any problem. Note that images are the same size whether they are vertical or horizontal. Even with this precaution, when I run this on a large number of directories, I often, eventually, get a Scribus crash with the message:


The basic idea here is to go through the source directory and gather all the sub-directory names. Then, go through each of those sub-directories and generate a document with the images found there, 12 per page. The document places the sub-directory name at the top of each page, arranges the images in four rows of three images with the file names centered under each. Page numbers are added. Since I print these documents double sided, the page numbers move from side to side and the entire page contents are shifted a little to the right (odd numbered pages) or the left (even numbered pages). Each gallery gets its own file.
:Scribus crashes due to the following exception : EXCEPTION_ACCESS_VIOLATION


Once generated, an SLA file is saved in the outputDirectory specified on line 7. Then, a PDF is generated with all pages and saved to the same location. Both files are named the same as the image sub-directory but with .sla and .pdf extensions respectively.
I just close Scribus, move the image directories that have already been processed and start again. If anyone knows what's causing that and can fix it, that would be wonderful.


Considerable credit goes to Gregory Pittman, who wrote the original script on which this is based.
Considerable credit and thanks to Gregory Pittman, who wrote the original script on which this is based.


<pre><nowiki>
<pre><nowiki>
Line 16: Line 26:
of directories, creating a new document of US Letter size from each directory.
of directories, creating a new document of US Letter size from each directory.


AUTHOR: Henry Hartley, December 2, 2010, with thanks to Gregory Pittman
AUTHOR: Henry Hartley
DATE: December 2, 2010 (original version)
MODIFICATIONS:
    June 28, 2011 - Added support for reading file information from index.txt


LICENSE: This program is free software; you can redistribute it and/or modify  
LICENSE: This program is free software; you can redistribute it and/or modify  
Line 24: Line 37:


http://wiki.scribus.net/index.php/Automatic_import_of_images:_Multiple_image_directories
http://wiki.scribus.net/index.php/Automatic_import_of_images:_Multiple_image_directories
You will probably need to edit the following:


"""
"""
# The directory that contains images or subdirectories with images.
# All subdirectories under this location will be used to create
# separate output files
sourceDirectory = "C:/temp/photo/"


import scribus
# The directory, which should already exist, where you want your output files
import os
# to be written
outputDirectory = "C:/temp/photo/"
 
# The name of a file that contains one line per image. Each line should consist of
# a tab delimited string with the following file information:
#
#  0 - count      -- Actually, anything can be here except a blank space or a hash
#                      mark (#), which will cause the line to be ignored.
#  1 - file name  -- The name of the file without the .jpg extension
#  2 - image title -- The title to use in labeling the file (optional)
#  3 - description -- A longer description (optional) (currently not used)
#
sourceFileList = 'index.txt'


sourceDirectory = "C:/temp/photographs/images/"
# The font to use for documents
outputDirectory = "C:/temp/photographs/contact_sheets/"
labelFont = 'Garamond Regular'


filetype = []
dicttype = {'j':'.jpg','p':'.png','t':'.tif','g':'.gif','P':'.pdf'}
Dicttype = {'j':'.JPG','p':'.PNG','t':'.TIF','g':'.GIF','P':'.PDF'}


firstPageNumber = '1'
####################################################################################
imagetype = 'jptgP'
## While you might want to edit setting below here, you
labelFont = 'Garamond Regular'
## shouldn't need to in order to run the script
####################################################################################


for t in imagetype[0:]:
import scribus
filetype.append(dicttype[t])
import os
filetype.append(Dicttype[t])


# All of these numbers can be adjusted but what is here seems to work pretty well
# All of these numbers can be adjusted but what is here seems to work pretty well
firstPageNumber = '1'


marginX  = 36
marginX  = 36
Line 72: Line 104:
# Height is enough to allow a second line of text if desired.
# Height is enough to allow a second line of text if desired.
labelGap  = 0
labelGap  = 0
labelFS  = 7
labelFS  = 6
labelH    = labelFS * 3
labelH    = labelFS * 4


# Page number text frame
# Page number text frame
Line 87: Line 119:
# It's quite likely that these longs lists could be replaced by a formula.
# It's quite likely that these longs lists could be replaced by a formula.
# If anyone has the time to do that, it would probably make this script easier
# If anyone has the time to do that, it would probably make this script easier
# to maintain.
# to maintain. Still, this works.
xPosH = [marginX, marginX + iHorW + gapW, marginX + (2 * iHorW) + (2 * gapW), marginX, marginX + iHorW + gapW, marginX + (2 * iHorW) + (2 * gapW), marginX, marginX + iHorW + gapW, marginX + (2 * iHorW) + (2 * gapW), marginX, marginX + iHorW + gapW, marginX + (2 * iHorW) + (2 * gapW)]
xPosH = [marginX, marginX + iHorW + gapW, marginX + (2 * iHorW) + (2 * gapW), marginX, marginX + iHorW + gapW, marginX + (2 * iHorW) + (2 * gapW), marginX, marginX + iHorW + gapW, marginX + (2 * iHorW) + (2 * gapW), marginX, marginX + iHorW + gapW, marginX + (2 * iHorW) + (2 * gapW)]
yPosH = [marginY + yOffset + V2H, marginY + yOffset + V2H, marginY + yOffset + V2H, marginY + yOffset + iVerH + gapH + V2H, marginY + yOffset + iVerH + gapH + V2H, marginY + yOffset + iVerH + gapH + V2H, marginY + yOffset + (2 * iVerH) + (2 * gapH) + V2H, marginY + yOffset + (2 * iVerH) + (2 * gapH) + V2H, marginY + yOffset + (2 * iVerH) + (2 * gapH) + V2H, marginY + yOffset + (3 * iVerH) + (3 * gapH) + V2H, marginY + yOffset + (3 * iVerH) + (3 * gapH) + V2H, marginY + yOffset + (3 * iVerH) + (3 * gapH) + V2H]
yPosH = [marginY + yOffset + V2H, marginY + yOffset + V2H, marginY + yOffset + V2H, marginY + yOffset + iVerH + gapH + V2H, marginY + yOffset + iVerH + gapH + V2H, marginY + yOffset + iVerH + gapH + V2H, marginY + yOffset + (2 * iVerH) + (2 * gapH) + V2H, marginY + yOffset + (2 * iVerH) + (2 * gapH) + V2H, marginY + yOffset + (2 * iVerH) + (2 * gapH) + V2H, marginY + yOffset + (3 * iVerH) + (3 * gapH) + V2H, marginY + yOffset + (3 * iVerH) + (3 * gapH) + V2H, marginY + yOffset + (3 * iVerH) + (3 * gapH) + V2H]
Line 100: Line 132:
DIR = []
DIR = []
for file in dir:
for file in dir:
#result = scribus.messageBox("Directory", "File: "+file)
    #result = scribus.messageBox("Directory", "File: "+file)
if os.path.isdir(sourceDirectory+file):
    if os.path.isdir(sourceDirectory+file):
DIR.append(file)
        DIR.append(file)
dirCount += 1
        dirCount += 1
DIR.sort()
DIR.sort()


Line 109: Line 141:


for imagedir in DIR:
for imagedir in DIR:
#result = scribus.messageBox("Directory", "The directory about to be processed is: "+imagedir)
   
d = os.listdir(sourceDirectory+imagedir)
    # Open the index.txt file
D = []
    f = open(sourceDirectory+imagedir+'/'+sourceFileList, 'r')
for file in d:
    D = []
for format in filetype:
    D = f.readlines()
if file.endswith(format):
   
D.append(file)
    gallerytitle = imagedir
D.sort()
   
    imageCount = 0
gallerytitle = imagedir
    pageNumber = 1
    scribus.progressTotal(len(D))
imageCount = 0
    if len(D) > 0:
pageNumber = 1
        if scribus.newDoc(scribus.PAPER_LETTER, (marginX, marginX, marginY, marginY),scribus.PORTRAIT, 1, scribus.UNIT_POINTS, scribus.NOFACINGPAGES, scribus.FIRSTPAGERIGHT):
scribus.progressTotal(len(D))
            while imageCount < len(D):
if len(D) > 0:
                if imageCount > 0:
if scribus.newDoc(scribus.PAPER_LETTER, (marginX, marginX, marginY, marginY),scribus.PORTRAIT, 1, scribus.UNIT_POINTS, scribus.NOFACINGPAGES, scribus.FIRSTPAGERIGHT):
                    scribus.newPage(-1)
while imageCount < len(D):
               
if imageCount > 0:
                # Here is where we're loading images into the page, four, six, or twelve at a time, then go back up for a newPage
scribus.newPage(-1)
                if pageNumber % 2 == 0:
                    pOffset = 0
# Here is where we're loading images into the page, four, six, or twelve at a time, then go back up for a newPage
                    pNumX = xPosH[0]
if pageNumber % 2 == 0:
                else:
pOffset = 0
                    pOffset = xOffset
pNumX = xPosH[0]
                    pNumX = xPosH[pNumXt] + iHorW + pOffset - pNumW
else:
   
pOffset = xOffset
                # L is the frame at the top of each page showing the gallery title (read in above)
pNumX = xPosH[pNumXt] + iHorW + pOffset - pNumW
                L = scribus.createText(titleX + pOffset, titleY, titleW, titleH)
                scribus.setText(gallerytitle, L)
# L is the frame at the top of each page showing the gallery title (read in above)
                scribus.setTextAlignment(scribus.ALIGN_CENTERED, L)
L = scribus.createText(titleX + pOffset, titleY, titleW, titleH)
                scribus.setFont(labelFont, L)
scribus.setText(gallerytitle, L)
                scribus.setFontSize(titleFS, L)
scribus.setTextAlignment(scribus.ALIGN_CENTERED, L)
               
scribus.setFont(labelFont, L)
                for i in imgID:
scribus.setFontSize(titleFS, L)
                    if imageCount < len(D):
                       
for i in imgID:
                        if not D[imageCount].startswith("#") and not D[imageCount].startswith(" "):
if imageCount < len(D):
                            Img = []
                            Img = D[imageCount].split("\t")
scribus.statusMessage("Placing image " + D[imageCount] + " in document (" + `imageCount` + " of " + `len(D)` + ")")
                           
scribus.progressSet(imageCount)
                            scribus.statusMessage("Placing image " + Img[1] + " in document (" + `imageCount` + " of " + `len(D)` + ")")
                            scribus.progressSet(imageCount)
# Create a temporary image object. Size and location are irrelivant
       
tempImage = scribus.createImage(10, 10, 10, 10)
                            # Create a temporary image object. Size and location are irrelivant
                            tempImage = scribus.createImage(10, 10, 10, 10)
# Populate that object with your image
       
scribus.loadImage(sourceDirectory+imagedir + '/' + D[imageCount], tempImage)
                            # Populate that object with your image
                            scribus.loadImage(sourceDirectory+imagedir + '/' + Img[1]+'.jpg', tempImage)
# Scale the image but NOT proportionally
       
scribus.setScaleImageToFrame(scaletoframe=1, proportional=0, name=tempImage)
                            # Scale the image but NOT proportionally
                            scribus.setScaleImageToFrame(scaletoframe=1, proportional=0, name=tempImage)
# Use getImageScale to get the relative scaling in the x and y directions
       
imgx,imgy = scribus.getImageScale(tempImage)
                            # Use getImageScale to get the relative scaling in the x and y directions
                            imgx,imgy = scribus.getImageScale(tempImage)
# If imgx < imgy, you have an horizontal image, otherwise,  
       
# a vertical (or square) image.  
                            # If imgx < imgy, you have an horizontal image, otherwise,  
if imgx < imgy:
                            # a vertical (or square) image.  
x = xPosH[i] + pOffset
                            if imgx < imgy:
y = yPosH[i]
                                x = xPosH[i] + pOffset
pWidth = iHorW
                                y = yPosH[i]
pHeight = iHorH
                                pWidth = iHorW
else:
                                pHeight = iHorH
x = xPosV[i] + pOffset
                            else:
y = yPosV[i]
                                x = xPosV[i] + pOffset
pWidth = iVerW
                                y = yPosV[i]
pHeight = iVerH
                                pWidth = iVerW
                                pHeight = iVerH
# Can't have these cluttering up the page
       
scribus.deleteObject(tempImage)
                            # Can't have these cluttering up the page
                            scribus.deleteObject(tempImage)
# Now we create the REAL image object placed appropriately
                           
f = scribus.createImage(x, y, pWidth, pHeight)
                            # Now we create the REAL image object placed appropriately
                            f = scribus.createImage(x, y, pWidth, pHeight)
# and load the image
                           
scribus.loadImage(sourceDirectory+imagedir + '/' + D[imageCount], f)
                            # and load the image
                            scribus.loadImage(sourceDirectory+imagedir + '/' + Img[1]+'.jpg', f)
# this time when we scale the image, we do it proportionally
                           
scribus.setScaleImageToFrame(scaletoframe=1, proportional=1, name=f)
                            # this time when we scale the image, we do it proportionally
                            scribus.setScaleImageToFrame(scaletoframe=1, proportional=1, name=f)
# Lpic is the label for each picture, the position is the same for all  
                           
# image orientations and the labels are centered under the pictures.
                            # Lpic is the label for each picture, the position is the same for all  
                            #   image orientations and the labels are centered under the pictures.
# Create the text frame.
                           
Lpic = scribus.createText(xPosH[i] + pOffset, yPosV[i] + iVerH + labelGap, iHorW, labelH)
                            # Create a text frame for the picture caption
                            Lpic = scribus.createText(xPosH[i] + pOffset - int(gapW/2)-1, yPosV[i] + iVerH + labelGap, iHorW + gapW-2, labelH)
# populate with the file name
                           
scribus.setText(D[imageCount], Lpic)
                            # populate with the file name and title (Img[2])
                            if len(Img[2]) > 0:
# and format as desired
                                scribus.setText('File: '+Img[1]+', '+Img[2], Lpic)
scribus.setTextAlignment(scribus.ALIGN_CENTERED, Lpic)
                            else:
scribus.setFont(labelFont, Lpic)
                                scribus.setText('File: '+Img[1], Lpic)
scribus.setFontSize(labelFS, Lpic)
                           
#scribus.setFillColor("White", Lpic)
                            # and format as desired
                            scribus.setTextAlignment(scribus.ALIGN_CENTERED, Lpic)
# increment the image count
                            scribus.setFont(labelFont, Lpic)
imageCount += 1
                            scribus.setFontSize(labelFS, Lpic)
                            scribus.setLineSpacing(labelFS, Lpic)
# Now the page number
                            #scribus.setFillColor("White", Lpic)
                           
# Create a text frame
                            # End of If/Then -- startswith()
pNum = scribus.createText(pNumX, pNumY, pNumW, pNumH)
                           
                        # increment the image count
# Put the page number in it and format
                        imageCount += 1
pageNumberPrint = pageNumber + (int(firstPageNumber)-1)
               
scribus.setText(`pageNumberPrint`, pNum)
                # Now the page number
if imageCount < len(D):
               
scribus.setTextAlignment(scribus.ALIGN_RIGHT, pNum)
                # Create a text frame
else:
                pNum = scribus.createText(pNumX, pNumY, pNumW, pNumH)
scribus.setTextAlignment(scribus.ALIGN_LEFT, pNum)
               
scribus.setFont(labelFont, pNum)
                # Put the page number in it and format
scribus.setFontSize(pNumFS, pNum)
                pageNumberPrint = pageNumber + (int(firstPageNumber)-1)
#scribus.setFillColor("White", pNum)
                scribus.setText(`pageNumberPrint`, pNum)
                if imageCount < len(D):
# Finally, we increment the page number
                    scribus.setTextAlignment(scribus.ALIGN_RIGHT, pNum)
pageNumber += 1
                else:
scribus.setRedraw(1)
                    scribus.setTextAlignment(scribus.ALIGN_LEFT, pNum)
scribus.redrawAll()
                scribus.setFont(labelFont, pNum)
                scribus.setFontSize(pNumFS, pNum)
else:
                #scribus.setFillColor("White", pNum)
result = scribus.messageBox ('Not Found','No Images found with\n this search selection',scribus.BUTTON_OK)
               
                # Finally, we increment the page number
scribus.setRedraw(1)
                pageNumber += 1
scribus.redrawAll()
        scribus.setRedraw(1)
        scribus.redrawAll()
scribus.statusMessage("Saving File as "+outputDirectory+imagedir)
   
#result = scribus.messageBox("Save", "Saving SLA File as: "+outputDirectory+imagedir+".sla")
    else:
scribus.saveDocAs(outputDirectory+imagedir+".sla")
        result = scribus.messageBox ('Not Found','No Images found with\n this search selection',scribus.BUTTON_OK)
   
scribus.statusMessage("Saving PDF File as "+outputDirectory+imagedir)
    scribus.setRedraw(1)
pdfExport = scribus.PDFfile()
    scribus.redrawAll()
pdfExport.info = outputDirectory+imagedir+".pdf"
   
pNum = 1
    scribus.statusMessage("Saving File as "+outputDirectory+imagedir+".sla")
PDFPages = []
    #result = scribus.messageBox("Save", "Saving SLA File as: "+outputDirectory+imagedir+".sla")
while pNum < pageNumber:
    scribus.saveDocAs(outputDirectory+imagedir+".sla")
PDFPages.append(pNum)
   
pNum += 1
    scribus.statusMessage("Saving PDF File as "+outputDirectory+imagedir+".pdf")
pdfExport.pages = PDFPages
    pdfExport = scribus.PDFfile()
pdfExport.file = outputDirectory+imagedir+".pdf"
    pdfExport.info = outputDirectory+imagedir+".pdf"
pdfExport.save()
    pNum = 1
    PDFPages = []
scribus.closeDoc()
    while pNum < pageNumber:
        PDFPages.append(pNum)
scribus.statusMessage("")
        pNum += 1
scribus.progressReset()
    pdfExport.pages = PDFPages
    pdfExport.file = outputDirectory+imagedir+".pdf"
    pdfExport.save()
   
    scribus.closeDoc()
   
    scribus.statusMessage("")
    scribus.progressReset()
 


</nowiki></pre>
</nowiki></pre>

Latest revision as of 16:47, 28 June 2011

As an amateur photographer, I take a lot of pictures and in general I post them to a web site with one or more galleries being generated most days. For general purposes, that's enough. Occasionally, however, I've found it useful to have a printed version of my images, to be used more as like the contact sheet of old than as publication quality images. I'd like these documents to have small versions of all the images in each gallery, labeled with the file name and an option title that identified the subject of the photograph. Since the display of images on the web site is driven by text files with file names, titles, and descriptions, I it made sense to reuse that text file for labeling the pictures on the contact sheets.

Starting with the script found on the page Automatic import of images: Versions not requiring Tkinter#album_letter_2.py, I have created a new script that will take a collection of image directories and create 12-images-per-page documents from each of them in turn, save the documents and generate PDF files from each.

The initial input and output directories are specified in the code and will need to be changed based on your own configuration. They are defined on lines 27 and 31. The file that I use to list the images is index.txt but this file name may also be changed on line 42. Finally, the font to use is set on line 45. While there are many other settings that may be adjusted, the script should work well enough with just those four items set. Note that the script will not create the output directory so you'll need to make sure it exists before you start.

The basic idea here is to go through the source directory and gather all the sub-directory names. Then, go through each of those sub-directories, open the index.txt file, and generate a document with the images listed there, 12 per page. The document places the sub-directory name at the top of each page, arranges the images in four rows of three images with the file names centered under each. Page numbers are added. Since I print these documents double sided, the page numbers move from side to side and the entire page contents are shifted a little to the right (odd numbered pages) or the left (even numbered pages). Each gallery gets its own file.

Once generated, an SLA file is saved in the outputDirectory specified on line 31. Then, a PDF is generated with all pages and saved to the same location. Both files are named the same as the image sub-directory but with .sla and .pdf extensions respectively.

CAUTION: if you have directories with too many or very large image files, this can take a long while to run. Documents for directories with a couple dozen images of the appropriate size are created, saved, and exported to PDF in a matter of seconds. Documents for directories with images much larger than necessary or with more than a couple hundred images can take many minutes to be created. Since the images are printed at just over 1.4 by 2.1 inches, even at 300 dpi they will only need to be 420 by 633 pixels and you can probably get away with less. I would be cautious running this script on directories with lots of images or those copied directly from your digital camera. I've successfully produced documents with 640 images this way but it took a good while.

For publication to my web site I use a script that creates thumbnail, small, and large versions of each photo. Since the small size images are 467 by 700 pixels, I use those when generating contact sheets with this script. That works quite well and I've created documents with 1,500 images without any problem. Note that images are the same size whether they are vertical or horizontal. Even with this precaution, when I run this on a large number of directories, I often, eventually, get a Scribus crash with the message:

Scribus crashes due to the following exception : EXCEPTION_ACCESS_VIOLATION

I just close Scribus, move the image directories that have already been processed and start again. If anyone knows what's causing that and can fix it, that would be wonderful.

Considerable credit and thanks to Gregory Pittman, who wrote the original script on which this is based.

#!/usr/bin/env python

"""
This is contact-sheet.py. This script will load the images from any number
of directories, creating a new document of US Letter size from each directory.

AUTHOR: Henry Hartley
DATE: December 2, 2010 (original version)
MODIFICATIONS:
    June 28, 2011 - Added support for reading file information from index.txt

LICENSE: This program is free software; you can redistribute it and/or modify 
it under the terms of the GNU General Public License as published by the Free 
Software Foundation; either version 2 of the License, or (at your option) any 
later version.

http://wiki.scribus.net/index.php/Automatic_import_of_images:_Multiple_image_directories



You will probably need to edit the following:

"""
# The directory that contains images or subdirectories with images.
# All subdirectories under this location will be used to create 
# separate output files
sourceDirectory = "C:/temp/photo/"

# The directory, which should already exist, where you want your output files 
# to be written
outputDirectory = "C:/temp/photo/"

# The name of a file that contains one line per image. Each line should consist of 
# a tab delimited string with the following file information: 
# 
#   0 - count       -- Actually, anything can be here except a blank space or a hash
#                      mark (#), which will cause the line to be ignored.
#   1 - file name   -- The name of the file without the .jpg extension
#   2 - image title -- The title to use in labeling the file (optional)
#   3 - description -- A longer description (optional) (currently not used)
# 
sourceFileList = 'index.txt'

# The font to use for documents
labelFont = 'Garamond Regular'


####################################################################################
## While you might want to edit setting below here, you 
## shouldn't need to in order to run the script
####################################################################################

import scribus
import os

# All of these numbers can be adjusted but what is here seems to work pretty well

firstPageNumber = '1'

marginX   = 36
marginY   = 36

# Image height and width
iHorW   = 152
iVerW   = 101
iVerH  = iHorW
iHorH  = iVerW

# Gap sizes and page offsets
gapW      = 24
gapH      = 21
xOffset   = 36
yOffset   = 24

# Page title text frame
titleFS   = 12
titleX    = marginX
titleY    = marginY
titleW    = iHorW + iHorW + gapW + iHorW + gapW
titleH    = titleFS * 2

# Image label text frame
# Height is enough to allow a second line of text if desired.
labelGap  = 0
labelFS   = 6
labelH    = labelFS * 4

# Page number text frame
pNumFS    = 8
pNumY     = 759
pNumXt    = 2
pNumW     = 18
pNumH     = pNumFS * 1.5

H2V = int((iHorW-iHorH)/2)
V2H = int((iVerH-iVerW)/2)

# It's quite likely that these longs lists could be replaced by a formula.
# If anyone has the time to do that, it would probably make this script easier
# to maintain. Still, this works.
xPosH = [marginX, marginX + iHorW + gapW, marginX + (2 * iHorW) + (2 * gapW), marginX, marginX + iHorW + gapW, marginX + (2 * iHorW) + (2 * gapW), marginX, marginX + iHorW + gapW, marginX + (2 * iHorW) + (2 * gapW), marginX, marginX + iHorW + gapW, marginX + (2 * iHorW) + (2 * gapW)]
yPosH = [marginY + yOffset + V2H, marginY + yOffset + V2H, marginY + yOffset + V2H, marginY + yOffset + iVerH + gapH + V2H, marginY + yOffset + iVerH + gapH + V2H, marginY + yOffset + iVerH + gapH + V2H, marginY + yOffset + (2 * iVerH) + (2 * gapH) + V2H, marginY + yOffset + (2 * iVerH) + (2 * gapH) + V2H, marginY + yOffset + (2 * iVerH) + (2 * gapH) + V2H, marginY + yOffset + (3 * iVerH) + (3 * gapH) + V2H, marginY + yOffset + (3 * iVerH) + (3 * gapH) + V2H, marginY + yOffset + (3 * iVerH) + (3 * gapH) + V2H]
xPosV = [marginX + H2V, marginX + iHorW + gapW + H2V, marginX + (2 * iHorW) + (2 * gapW) + H2V, marginX + H2V, marginX + iHorW + gapW + H2V, marginX + (2 * iHorW) + (2 * gapW) + H2V, marginX + H2V, marginX + iHorW + gapW + H2V, marginX + (2 * iHorW) + (2 * gapW) + H2V, marginX + H2V, marginX + iHorW + gapW + H2V, marginX + (2 * iHorW) + (2 * gapW) + H2V]
yPosV = [marginY + yOffset, marginY + yOffset, marginY + yOffset, marginY + yOffset + iVerH + gapH, marginY + yOffset + iVerH + gapH, marginY + yOffset + iVerH + gapH, marginY + yOffset + (2 * iVerH) + (2 * gapH), marginY + yOffset + (2 * iVerH) + (2 * gapH), marginY + yOffset + (2 * iVerH) + (2 * gapH), marginY + yOffset + (3 * iVerH) + (3 * gapH), marginY + yOffset + (3 * iVerH) + (3 * gapH), marginY + yOffset + (3 * iVerH) + (3 * gapH)]
imgID = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
imgRow = 3
imagedir = ""

dirCount = 0
dir = os.listdir(sourceDirectory)
DIR = []
for file in dir:
    #result = scribus.messageBox("Directory", "File: "+file)
    if os.path.isdir(sourceDirectory+file):
        DIR.append(file)
        dirCount += 1
DIR.sort()

#result = scribus.messageBox("Directory Count", "There were "+str(dirCount)+" directories added to the array")

for imagedir in DIR:
    
    # Open the index.txt file
    f = open(sourceDirectory+imagedir+'/'+sourceFileList, 'r')
    D = []
    D = f.readlines()
    
    gallerytitle = imagedir
    
    imageCount = 0
    pageNumber = 1
    scribus.progressTotal(len(D))
    if len(D) > 0:
        if scribus.newDoc(scribus.PAPER_LETTER, (marginX, marginX, marginY, marginY),scribus.PORTRAIT, 1, scribus.UNIT_POINTS, scribus.NOFACINGPAGES, scribus.FIRSTPAGERIGHT):
            while imageCount < len(D):
                if imageCount > 0:
                    scribus.newPage(-1)
                
                # Here is where we're loading images into the page, four, six, or twelve at a time, then go back up for a newPage
                if pageNumber % 2 == 0:
                    pOffset = 0
                    pNumX = xPosH[0]
                else:
                    pOffset = xOffset
                    pNumX = xPosH[pNumXt] + iHorW + pOffset - pNumW
    
                # L is the frame at the top of each page showing the gallery title (read in above)
                L = scribus.createText(titleX + pOffset, titleY, titleW, titleH)
                scribus.setText(gallerytitle, L)
                scribus.setTextAlignment(scribus.ALIGN_CENTERED, L)
                scribus.setFont(labelFont, L)
                scribus.setFontSize(titleFS, L)
                
                for i in imgID:
                    if imageCount < len(D):
                        
                        if not D[imageCount].startswith("#") and not D[imageCount].startswith(" "):
                            Img = []
                            Img = D[imageCount].split("\t")
                            
                            scribus.statusMessage("Placing image " + Img[1] + " in document (" + `imageCount` + " of " + `len(D)` + ")")
                            scribus.progressSet(imageCount)
        
                            # Create a temporary image object. Size and location are irrelivant
                            tempImage = scribus.createImage(10, 10, 10, 10)
        
                            # Populate that object with your image
                            scribus.loadImage(sourceDirectory+imagedir + '/' + Img[1]+'.jpg', tempImage)
        
                            # Scale the image but NOT proportionally
                            scribus.setScaleImageToFrame(scaletoframe=1, proportional=0, name=tempImage)
        
                            # Use getImageScale to get the relative scaling in the x and y directions
                            imgx,imgy = scribus.getImageScale(tempImage)
        
                            # If imgx < imgy, you have an horizontal image, otherwise, 
                            # a vertical (or square) image. 
                            if imgx < imgy:
                                x = xPosH[i] + pOffset
                                y = yPosH[i]
                                pWidth = iHorW
                                pHeight = iHorH
                            else:
                                x = xPosV[i] + pOffset
                                y = yPosV[i]
                                pWidth = iVerW
                                pHeight = iVerH
        
                            # Can't have these cluttering up the page
                            scribus.deleteObject(tempImage)
                            
                            # Now we create the REAL image object placed appropriately
                            f = scribus.createImage(x, y, pWidth, pHeight)
                            
                            # and load the image
                            scribus.loadImage(sourceDirectory+imagedir + '/' + Img[1]+'.jpg', f)
                            
                            # this time when we scale the image, we do it proportionally
                            scribus.setScaleImageToFrame(scaletoframe=1, proportional=1, name=f)
                            
                            # Lpic is the label for each picture, the position is the same for all 
                            #    image orientations and the labels are centered under the pictures.
                            
                            # Create a text frame for the picture caption
                            Lpic = scribus.createText(xPosH[i] + pOffset - int(gapW/2)-1, yPosV[i] + iVerH + labelGap, iHorW + gapW-2, labelH)
                            
                            # populate with the file name and title (Img[2])
                            if len(Img[2]) > 0:
                                scribus.setText('File: '+Img[1]+', '+Img[2], Lpic)
                            else:
                                scribus.setText('File: '+Img[1], Lpic)
                            
                            # and format as desired
                            scribus.setTextAlignment(scribus.ALIGN_CENTERED, Lpic)
                            scribus.setFont(labelFont, Lpic)
                            scribus.setFontSize(labelFS, Lpic)
                            scribus.setLineSpacing(labelFS, Lpic)
                            #scribus.setFillColor("White", Lpic)
                            
                            # End of If/Then -- startswith()
                            
                        # increment the image count
                        imageCount += 1
                
                # Now the page number
                
                # Create a text frame
                pNum = scribus.createText(pNumX, pNumY, pNumW, pNumH)
                
                # Put the page number in it and format
                pageNumberPrint = pageNumber + (int(firstPageNumber)-1)
                scribus.setText(`pageNumberPrint`, pNum)
                if imageCount < len(D):
                    scribus.setTextAlignment(scribus.ALIGN_RIGHT, pNum)
                else:
                    scribus.setTextAlignment(scribus.ALIGN_LEFT, pNum)
                scribus.setFont(labelFont, pNum)
                scribus.setFontSize(pNumFS, pNum)
                #scribus.setFillColor("White", pNum)
                
                # Finally, we increment the page number
                pageNumber += 1
        scribus.setRedraw(1)
        scribus.redrawAll()
    
    else:
        result = scribus.messageBox ('Not Found','No Images found with\n this search selection',scribus.BUTTON_OK)
    
    scribus.setRedraw(1)
    scribus.redrawAll()
    
    scribus.statusMessage("Saving File as "+outputDirectory+imagedir+".sla")
    #result = scribus.messageBox("Save", "Saving SLA File as: "+outputDirectory+imagedir+".sla")
    scribus.saveDocAs(outputDirectory+imagedir+".sla")
    
    scribus.statusMessage("Saving PDF File as "+outputDirectory+imagedir+".pdf")
    pdfExport = scribus.PDFfile()
    pdfExport.info = outputDirectory+imagedir+".pdf"
    pNum = 1
    PDFPages = []
    while pNum < pageNumber:
        PDFPages.append(pNum)
        pNum += 1
    pdfExport.pages = PDFPages
    pdfExport.file = outputDirectory+imagedir+".pdf"
    pdfExport.save()
    
    scribus.closeDoc()
    
    scribus.statusMessage("")
    scribus.progressReset()