Automatic import of images from a directory using a script

scribalbum.py and scribalbuma4.py
Link to: Automatic import of images: Versions not requiring Tkinter

These are similar Python scripts. Both create a new document and as many pages as needed. scribalbum.py places 4 staggered images on a US Letter page, allowing for annotations in the remaining white space. scribalbuma4.py places 8 images per A4 page with little remaining white space. Both versions will make a text frame in the upper left corner of each page indicating the directory the images came from, and each image has a label showing the image's filename.

Here are some features that both share:


 * When you run the script from within Scribus, you will see two Tk windows, one of which is blank, the other of which asks for the name of the directory and has a number of checkboxes. You can use a relative path, but an absolute one is more precise, and the script can be far away from your image directory.


 * You may check one or as many image formats as you like. ALL images of that type will be included -- you cannot pick and choose individual images. As written, they will appear in alphanumeric order in your document, left column filled first. You could go left-right-left right down the page by changing the logic.


 * PDFs will import, but only the first page.


 * Thus far, SVGs will not import with this script. What you get is a blank frame. One of those "to do" things.


 * All images are adjusted to fit the frame size, and the frame size is the same within each script version. Adjusting the frame for individual images is a job best done in Scribus after you have made and loaded the file. These scripts are just meant to do the grunt work, let Scribus do the tweaking.


 * Once you have entered the directory, selected the image formats, then click OK. Expect some time to go by, during which Scribus frames and icons may blink in and out.


 * When the script is done, the formerly blank Tk window now says, "Now click the X in the upper right corner of this window", meaning the X in the corner of the Tk window to delete this window. This ends the script, and Scribus takes over.


 * At this point, Scribus is, in essence, loading this file for display, so you may have some considerable time (minutes) go by, depending on the number and size of the images and the speed of your processor.


 * If at this point Scribus crashes, you most likely have used up all your RAM and swap memory, and at this time there is no protection built into the script. If you're lucky, Scribus may have saved an emergency file starting with "DocumentN...", but if it's too big (the file itself may not be very large, it's the memory requirements of all the images that's the source of the problem), Scribus still won't be able to load it, since it will still bump up against the memory problem.

scribalbum.py

 * 1) !/usr/bin/env python


 * 1) File: scribalbum.py
 * 2) originally 2005.02.13  Gregory Pittman
 * 3) modified   2005.02.27 - from scribalbuma4.py
 * 4) This version for US Letter paper
 * 5) Also, puts only 4 pictures per page, to allow for text frame notes in between
 * 6) Uses Tkinter to give you an Entry box for the name of a directory
 * 7) then filters out files ending with .jpg, .png, and so on (user selectable)
 * 8) Note: at this time will not pick up uppercase extension equivalents
 * 9) Makes a new document
 * 10) Some things to be fixed: needs a more elegant closure; right now you click
 * 11) on the X in the upper right corner of the root frame, but at least it tells
 * 12) you when.
 * 13) Eventually want to be able to browse for a directory, perhaps show a
 * 14) Listbox for selecting a choice.
 * 1) Eventually want to be able to browse for a directory, perhaps show a
 * 2) Listbox for selecting a choice.

from scribus import * import Tkinter import os

class ImageDialog(Tkinter.Toplevel): def __init__(self, parent): global jpg, tif, png, pdf, svg, gif Tkinter.Toplevel.__init__(self, parent, bg="#bbbbff")

Tkinter.Label(self, text='Enter the Image Directory, Click OK',bg="#bbbbff").grid(row=0,columnspan=6)

self.e = Tkinter.Entry(self) self.e.grid(row=1,columnspan=6) self.jpg = Tkinter.StringVar self.tif = Tkinter.StringVar self.png = Tkinter.StringVar self.pdf = Tkinter.StringVar self.svg = Tkinter.StringVar self.gif = Tkinter.StringVar b = Tkinter.Button(self, text='OK', bg="#55ff88", command=self.ok) b.grid(row=2,columnspan=6) self.protocol("WM_DELETE_WINDOW", self.quit) Tkinter.Label(self, text='Filters',bg="#bbbbff").grid(row=3) j = Tkinter.Checkbutton(self, text = '.jpg', variable = self.jpg,onvalue='.jpg') j.grid(row=4,column=0) t = Tkinter.Checkbutton(self, text = '.tif', variable = self.tif,onvalue='.tif') t.grid(row=4,column=2) p = Tkinter.Checkbutton(self, text = '.png', variable = self.png,onvalue='.png') p.grid(row=4, column=4) P = Tkinter.Checkbutton(self, text = '.pdf', variable = self.pdf,onvalue='.pdf') P.grid(row=5, column=0) s = Tkinter.Checkbutton(self, text = '.svg', variable = self.svg,onvalue='.svg') s.grid(row=5, column=2) g = Tkinter.Checkbutton(self, text = '.gif', variable = self.gif,onvalue='.gif') g.grid(row=5, column=4) j.select # uncomment these to have default on settings in Checkbuttons
 * 1)        t.select # j = .jpg, t = .tif, etc.
 * 2)        s.select
 * 3)        p.select
 * 4)        P.select
 * 5)        g.select

def ok(self): m = Tkinter.Message(root, text="Now click the X in the upper right corner of this window") m.grid(row=0, columnspan=4) try: D=[] imagecount = 0 framecount = 0 filetype = [] if len(self.jpg.get) == 4: filetype.append(self.jpg.get) if len(self.tif.get) == 4: filetype.append(self.tif.get) if len(self.png.get) == 4: filetype.append(self.png.get) if len(self.pdf.get) == 4: filetype.append(self.pdf.get) if len(self.svg.get) == 4: filetype.append(self.svg.get) if len(self.gif.get) == 4: filetype.append(self.gif.get) d = os.listdir(self.e.get) for file in d:               for format in filetype: if file.endswith(format): D.append(file) D.sort xpos = (15, 310) ypos = (42, 388, 187, 533) pwidth = 288 pheight = 217
 * 1) There are 4 pictures per page; xpos and ypos are the x,y coord of the upper left corners
 * 2) Coords are: (15, 42),(15, 388), (310, 187), (310, 533)
 * 1) This proportion is right for photographs (at least for my digital Olympus)

if newDoc(PAPER_LETTER, (10,10,20,20),PORTRAIT, 1, UNIT_POINTS, NOFACINGPAGES, FIRSTPAGERIGHT): while D[imagecount]: if imagecount > 0: newPage(-1) framecount = 0 L = createText(15, 20, 200, 20) setText("Dir: " + self.e.get, L)                   setTextAlignment(ALIGN_LEFT, L)                    setFont("Luxi Sans Regular", L)                    setFontSize(10, L)                    for xframe in xpos: if D[imagecount]: f = createImage(xframe, ypos[framecount], pwidth, pheight) loadImage(self.e.get + '/' + D[imagecount], f)                               setScaleImageToFrame(scaletoframe=1, proportional=1, name=f) lenfilename = len(D[imagecount]) Lpiclen = int(5.3 * lenfilename) Lpic = createText(xframe, ypos[framecount] + 217, Lpiclen, 15) setText(D[imagecount], Lpic) setTextAlignment(ALIGN_RIGHT, Lpic) setFont("Luxi Mono Regular", Lpic) setFontSize(8, Lpic) setFillColor("White", Lpic) imagecount = imagecount + 1 framecount = framecount + 1 if D[imagecount]: f = createImage(xframe, ypos[framecount], pwidth, pheight) loadImage(self.e.get + '/' + D[imagecount], f)                               setScaleImageToFrame(scaletoframe=1, proportional=1, name=f) lenfilename = len(D[imagecount]) Lpiclen = int(5.3 * lenfilename) Lpic = createText(xframe, ypos[framecount] + 217, Lpiclen, 15) setText(D[imagecount], Lpic) setTextAlignment(ALIGN_RIGHT, Lpic) setFont("Luxi Mono Regular", Lpic) setFontSize(8, Lpic) setFillColor("White", Lpic) imagecount = imagecount + 1 framecount = framecount + 1 setRedraw(1) redrawAll self.Tkinter.Toplevel.destroy except os.error, value: Tkinter.Button(root, text=value[1], bg="#ffff55").grid(row=3, columnspan = 4)
 * 1) L is the frame at the top of each page showing the directory name
 * 1) Here is where we're loading images into the page, four at a time, then go back up for a newPage
 * 1) Lpic is the label for each picture, with position and length adjusted
 * 2) according to the text length.
 * 3) So if you change the font or its size, you may need to adjust
 * 4) this only approximate calculation.

root = Tkinter.Tk root.update

d = ImageDialog(root)

root.wait_window(d)

scribalbuma4.py

 * 1) !/usr/bin/env python


 * 1) File: scribalbuma4.py
 * 2) 2005.02.13  Gregory Pittman
 * 3) Uses Tkinter to give you an Entry box for the name of a directory
 * 4) then filters out files ending with .jpg, .png, and so on (user selectable)
 * 5) Note: at this time will not pick up uppercase extension equivalents
 * 6) Makes a new document
 * 7) Some things to be fixed: needs a more elegant closure; right now you click
 * 8) on the X in the upper right corner of the root frame, but at least it tells
 * 9) you when.
 * 10) Eventually want to be able to browse for a directory, perhaps show a
 * 11) Listbox for selecting a choice.
 * 1) Listbox for selecting a choice.

from scribus import * import Tkinter import os

class ImageDialog(Tkinter.Toplevel): def __init__(self, parent): global jpg, tif, png, pdf, svg, gif Tkinter.Toplevel.__init__(self, parent, bg="#bbbbff")

Tkinter.Label(self, text='Enter the Image Directory, Click OK',bg="#bbbbff").grid(row=0,columnspan=6)

self.e = Tkinter.Entry(self) self.e.grid(row=1,columnspan=6) self.jpg = Tkinter.StringVar self.tif = Tkinter.StringVar self.png = Tkinter.StringVar self.pdf = Tkinter.StringVar self.svg = Tkinter.StringVar self.gif = Tkinter.StringVar b = Tkinter.Button(self, text='OK', bg="#55ff88", command=self.ok) b.grid(row=2,columnspan=6) self.protocol("WM_DELETE_WINDOW", self.quit) Tkinter.Label(self, text='Filters',bg="#bbbbff").grid(row=3) j = Tkinter.Checkbutton(self, text = '.jpg', variable = self.jpg,onvalue='.jpg') j.grid(row=4,column=0) t = Tkinter.Checkbutton(self, text = '.tif', variable = self.tif,onvalue='.tif') t.grid(row=4,column=2) p = Tkinter.Checkbutton(self, text = '.png', variable = self.png,onvalue='.png') p.grid(row=4, column=4) P = Tkinter.Checkbutton(self, text = '.pdf', variable = self.pdf,onvalue='.pdf') P.grid(row=5, column=0) s = Tkinter.Checkbutton(self, text = '.svg', variable = self.svg,onvalue='.svg') s.grid(row=5, column=2) g = Tkinter.Checkbutton(self, text = '.gif', variable = self.gif,onvalue='.gif') g.grid(row=5, column=4) j.select # uncomment these to have default on settings in Checkbuttons
 * 1)        t.select # j = .jpg, t = .tif, etc.
 * 2)        s.select
 * 3)        p.select
 * 4)        P.select
 * 5)        g.select

def ok(self): m = Tkinter.Message(root, text="Now click the X in the upper right corner of this window") m.grid(row=0, columnspan=4) try: D=[] imagecount = 0 filetype = [] if len(self.jpg.get) == 4: filetype.append(self.jpg.get) if len(self.tif.get) == 4: filetype.append(self.tif.get) if len(self.png.get) == 4: filetype.append(self.png.get) if len(self.pdf.get) == 4: filetype.append(self.pdf.get) if len(self.svg.get) == 4: filetype.append(self.svg.get) if len(self.gif.get) == 4: filetype.append(self.gif.get) d = os.listdir(self.e.get) for file in d:               for format in filetype: if file.endswith(format): D.append(file) D.sort xpos = (25, 321) ypos = (42, 236, 430, 624) pwidth = 250 pheight = 187
 * 1) There are 8 pictures per page; xpos and ypos are the x,y coord of the upper left corners
 * 1) This proportion is right for photographs (at least for my digital Olympus)

if newDoc(PAPER_A4, (10,10,20,20),PORTRAIT, 1, UNIT_POINTS, NOFACINGPAGES, FIRSTPAGERIGHT): while D[imagecount]: if imagecount > 0: newPage(-1) L = createText(15, 20, 200, 20) setText("Dir: " + self.e.get, L)                   setTextAlignment(ALIGN_LEFT, L)                    setFont("Luxi Sans Regular", L)                    setFontSize(10, L)                    for yframe in ypos: for xframe in xpos: if D[imagecount]: f = createImage(xframe, yframe, pwidth, pheight) loadImage(self.e.get + '/' + D[imagecount], f)                               setScaleImageToFrame(scaletoframe=1, proportional=1, name=f) lenfilename = len(D[imagecount]) Lpiclen = int(5.3 * lenfilename) Lpic = createText(xframe + (250 - Lpiclen), yframe + 172, Lpiclen, 15) setText(D[imagecount], Lpic) setTextAlignment(ALIGN_RIGHT, Lpic) setFont("Luxi Mono Regular", Lpic) setFontSize(8, Lpic) setFillColor("White", Lpic) imagecount = imagecount + 1 setRedraw(1) redrawAll self.Tkinter.Toplevel.destroy except os.error, value: Tkinter.Button(root, text=value[1], bg="#ffff55").grid(row=3, columnspan = 4)
 * 1) L is the frame at the top of each page showing the directory name
 * 1) Here is where we're loading images into the page, eight at a time, then go back up for a newPage
 * 1) Lpic is the label for each picture, with position and length adjusted according to the text length
 * 2) so if you change the font or its size, you may need to adjust this only approximate calculation

root = Tkinter.Tk root.update

d = ImageDialog(root)

root.wait_window(d)