Automatic import of images from a directory using a script
For simpler coding, I would advise using the Automatic import of images: Versions not requiring Tkinter, since these are more compact and Scribus-friendly.
Some newer features are that the new version scribalbum2.py has a GUI interface for selecting the directory. You can also choose either a 4-images-per-page or 6-images-per-page layout (US Letter). |
The reasons I made these scripts was, first of all, to learn some Python, but also to be able to create a picture file or album in Scribus. What I generally use this for is to organize the digital pictures I may take from a trip. One advantage of Scribus is that its native .sla files do not include image data, but only references to them, so this cataloguing file is relatively small. For some collections I will add annotations, make a PDF, and then print out an actual album.
Initially, I had created a stand-alone Perl program to do this outside of Scribus. I've deleted this, so now only the Python scripts remain. |
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
#!/usr/bin/env python # File: scribalbum.py # originally 2005.02.13 Gregory Pittman # modified 2005.02.27 - from scribalbuma4.py # This version for US Letter paper # Also, puts only 4 pictures per page, to allow for text frame notes in between # # Uses Tkinter to give you an Entry box for the name of a directory # then filters out files ending with .jpg, .png, and so on (user selectable) # Note: at this time will not pick up uppercase extension equivalents # Makes a new document # # Some things to be fixed: needs a more elegant closure; right now you click # on the X in the upper right corner of the root frame, but at least it tells # you when. # Eventually want to be able to browse for a directory, perhaps show a # 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 # t.select() # j = .jpg, t = .tif, etc. # s.select() # p.select() # P.select() # 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() # There are 4 pictures per page; xpos and ypos are the x,y coord of the upper left corners # Coords are: (15, 42),(15, 388), (310, 187), (310, 533) xpos = (15, 310) ypos = (42, 388, 187, 533) # This proportion is right for photographs (at least for my digital Olympus) pwidth = 288 pheight = 217 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 is the frame at the top of each page showing the directory name L = createText(15, 20, 200, 20) setText("Dir: " + self.e.get(), L) setTextAlignment(ALIGN_LEFT, L) setFont("Luxi Sans Regular", L) setFontSize(10, L) # Here is where we're loading images into the page, four at a time, then go back up for a newPage 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 is the label for each picture, with position and length adjusted # according to the text length. # So if you change the font or its size, you may need to adjust # this only approximate calculation. 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) root = Tkinter.Tk() root.update() d = ImageDialog(root) root.wait_window(d)
scribalbuma4.py
#!/usr/bin/env python # File: scribalbuma4.py # 2005.02.13 Gregory Pittman # Uses Tkinter to give you an Entry box for the name of a directory # then filters out files ending with .jpg, .png, and so on (user selectable) # Note: at this time will not pick up uppercase extension equivalents # Makes a new document # # Some things to be fixed: needs a more elegant closure; right now you click # on the X in the upper right corner of the root frame, but at least it tells # you when. # Eventually want to be able to browse for a directory, perhaps show a # 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 # t.select() # j = .jpg, t = .tif, etc. # s.select() # p.select() # P.select() # 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() # There are 8 pictures per page; xpos and ypos are the x,y coord of the upper left corners xpos = (25, 321) ypos = (42, 236, 430, 624) # This proportion is right for photographs (at least for my digital Olympus) pwidth = 250 pheight = 187 if newDoc(PAPER_A4, (10,10,20,20),PORTRAIT, 1, UNIT_POINTS, NOFACINGPAGES, FIRSTPAGERIGHT): while D[imagecount]: if imagecount > 0: newPage(-1) # L is the frame at the top of each page showing the directory name L = createText(15, 20, 200, 20) setText("Dir: " + self.e.get(), L) setTextAlignment(ALIGN_LEFT, L) setFont("Luxi Sans Regular", L) setFontSize(10, L) # Here is where we're loading images into the page, eight at a time, then go back up for a newPage 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 is the label for each picture, with position and length adjusted according to the text length # so if you change the font or its size, you may need to adjust this only approximate calculation 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) root = Tkinter.Tk() root.update() d = ImageDialog(root) root.wait_window(d)