Automatic import of images from a directory using a script

Link to: Automatic import of images: Versions not requiring Tkinter

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 (scribalbum.pl) to do this outside of Scribus. It still works even though I made it according to the 1.2.x series of file formats. One advantage it has is that it is quite fast and will generate a many-paged document in a few seconds. Its disadvantage is that it clumsily does a rote spitting out of this particular format, and there is a danger that it may not work with some future versions of Scribus. "Fixing" it to generate a 1.3.x format is more work than I would like. Best to load into 1.3.x Scribus then save from there.

The Python implementations have the advantage of using Scribus itself to make the document, so as long as the script works, the resulting file will be up-to-date. Another advantage is more easy modification to fit other needs.

You will find the programs at the bottom.

scribalbum.pl
I'm not going to say anything about this program. If you know something about Perl, you will see that it's not elegant, but it gets the job done. Especially tedious were all the backslashes to get the quotation marks printed to the file. It operates on the command line.

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.

Additional Notes
I have tried this on the brand-new (Égalité) Windows version of Scribus, and it doesn't work, and unfortunately no great clues as to why. You get the initial requestors. After you type in the name of a directory, it almost instantaneously tells you it's done, except all that you have is a one page document with the name of the directory in the upper left hand corner.

I went to www.python.org and downloaded the latest version of Python, still works the same. I also have a smaller program called showfiles.py, which simply lists the image files in a directory. It works fine, and since this set of commands is also in scribalbum.py, it should be finding the files without problems. Therefore, it must be something about loading the images.

If I get some specific questions (send them to scribus-list), I can add notes (or others can add them) about how to modify scribalbum.py and scribalbuma4.py to fit other needs.

scribalbum.pl
print "This program will create a picture album as a Scribus .sla file from all the pictures in a directory.\n"; print "Enter the name of the directory:\t"; chomp ($folder =); opendir (DIR, $folder) || die "Do you have the drive mounted?"; @files = grep { !/^\./} readdir(DIR); @files = grep { ( /\Z jpeg|jpg|tif|pdf|png|gif|svg/i ) } @files ; closedir (DIR);
 * 1) !/usr/bin/perl -w
 * 2)       Program scribalbum.pl        #
 * 3)       Created 2004.09.12           #
 * 4)       Last modified 2004.09.14     #
 * 5)       Gregory Pittman              #
 * 1)       Last modified 2004.09.14     #
 * 2)       Gregory Pittman              #
 * 1) First we'll eliminate anything starting with a period
 * 1) Then sift out the image files; Scribus can show at least some PDFs
 * 2) Not having so much luck with SVGs, but at least you'll know they are there

$numfiles = scalar(@files); $anzpages = int $numfiles/4; if ($numfiles/4 gt ($anzpages)){ ++$anzpages; } print "There are $numfiles pictures in this directory.\nThis will make $anzpages pages.\n"; print "What do you want to name this album? (\".sla\" will be appended)\t"; chomp ($album =); $album = "$album.sla";

$utf = ""; $document = " <DOCUMENT MAJGRID=\"100\" ABSTSPALTEN=\"11\" PAGEWITH=\"612\" AUTOCHECK=\"0\" LANGUAGE=\"English\" DPIn2=\"\" DPgam=\"0\" HCMS=\"1\" DGAP=\"0\" ORIENTATION=\"0\" MINWORDLEN=\"3\" VTIEFSC=\"100\" DOCLANGINFO=\"\" COMMENTS=\"\" TITLE=\"\" KEYWORDS=\"\" DSIZE=\"12\" AUTOSPALTEN=\"1\" GROUPC=\"7\" DOCFORMAT=\"\" DOCDATE=\"\" BORDERTOP=\"9\" GUIDELOCK=\"0\" DIMo=\"1\" DIPr=\"0\" DPuse=\"0\" DPSo=\"0\" DOCSOURCE=\"\" FIRSTNUM=\"1\" DPbla=\"1\" VHOCHSC=\"100\" DOCTYPE=\"\" BORDERBOTTOM=\"40\" BASEGRID=\"12\" VTIEF=\"33\" DOCCONTRIB=\"\" DOCRELATION=\"\" BASEO=\"0\" DOCIDENT=\"\" PUBLISHER=\"\" ANZPAGES=\"$anzpages\" HYCOUNT=\"2\" DIMo2=\"1\" AUTHOR=\"\" UNITS=\"0\" BORDERRIGHT=\"9\" DPPr=\"\" DPMo=\"\" PAGEHEIGHT=\"792\" MINGRID=\"20\" VHOCH=\"33\" DOCCOVER=\"\" DCOL=\"1\" AUTOMATIC=\"1\" ALAYER=\"0\" DOCRIGHTS=\"\" DPIn=\"\" VKAPIT=\"75\" DFONT=\"Bitstream Vera Sans Mono Roman\" BORDERLEFT=\"9\" >";
 * 1) The only variable in $document is $anzpages

$colorlayer = " \n  \n  ";

$pdf = " \n   \n   \n   \n   \n  ";

$docend = " \n \n\n"; open (FILE, ">$album"); print FILE "$utf\n$document\n$colorlayer\n$pdf\n";
 * 1) This goes at the end when we're done

$pagecount = 0; $image = 0; $piccount = 0; # will use this to place each picture on the page @xpos = qw( 16 310  16  310 ); # $xpos[$piccount] will give this value @ypos = qw( 14 187  360  533 );# ditto for $ypos @xpostxt = qw( 16 450  16  450 ); # $xpostxt[$piccount] label for image @ypostxt = qw( 232 165  578  510 );# ditto for $ypostxt @cab = qw( 0 2 0 2 );# alternates left and right justification for image labels foreach $file (@files) { if ($piccount gt 3) { $piccount = 0; print FILE "\n"; ++$pagecount; }   if ($piccount == 0) { print FILE " <PAGE NUM=\"$pagecount\" NAM=\"\" LEFT=\"0\" NumVGuides=\"0\" HorizontalGuides=\"\" MNAM=\"Normal\" NumHGuides=\"0\" VerticalGuides=\"\" >\n"; print FILE "  <PAGEOBJECT BottomLine=\"0\" REXTRA=\"1\" ALIGN=\"2\" LANGUAGE=\"English\" NUMPO=\"16\" PLINEART=\"1\" TXTSCALE=\"100\" RightLine=\"0\" LOCALSCX=\"1\" LINESP=\"12\" ROT=\"0\" TXTSTROKE=\"Black\" WIDTH=\"195\" BACKPAGE=\"-1\" GROUPS=\"\" LOCKR=\"0\" IFONT=\"Luxi Sans Regular\" LOCALSCY=\"1\" NAMEDLST=\"\" AUTOTEXT=\"0\" FLIPPEDV=\"0\" EXTRAV=\"0\" PCOLOR=\"None\" RADRECT=\"0\" REVERS=\"0\" PRINTABLE=\"1\" RATIO=\"1\" FLIPPEDH=\"0\" COLGAP=\"0\" PCOLOR2=\"None\" NEXTITEM=\"-1\" NUMGROUP=\"0\" TransValue=\"0\" INVERS=\"0\" PLINEEND=\"0\" FRTYPE=\"0\" PTYPE=\"4\" isTableItem=\"0\" TEXTFLOW2=\"0\" SHADE2=\"100\" PWIDTH=\"1\" HEIGHT=\"25\" DASHOFF=\"0\" PFILE2=\"\" PFILE=\"\" TEXTFLOW3=\"0\" ISIZE=\"10\" PLTSHOW=\"0\" TXTSTYLE=\"0\" CLIPEDIT=\"0\" BACKITEM=\"-1\" BookNr=\"0\" TransValueS=\"0\" EMBEDDED=\"1\" PFILE3=\"\" ANNAME=\"\" SHADE=\"100\" COCOOR=\"0 0 0 0 195 0 195 0 195 0 195 0 195 25 195 25 195 25 195 25 0 25 0 25 0 25 0 25 0 0 0 0 \" BASEOF=\"0\" PICART=\"1\" COLUMNS=\"1\" LAYER=\"0\" BOOKMARK=\"0\" TopLine=\"0\" LOCK=\"0\" EPROF=\"\" BBOXX=\"0\" DASHS=\"\" IRENDER=\"0\" TEXTFLOW=\"0\" BBOXH=\"0\" TXTFILLSH=\"100\" YPOS=\"11\" ANNOTATION=\"0\" LOCALX=\"0\" GRTYP=\"0\" XPOS=\"400\" NUMCO=\"16\" POCOOR=\"0 0 0 0 195 0 195 0 195 0 195 0 195 25 195 25 195 25 195 25 0 25 0 25 0 25 0 25 0 0 0 0 \" EXTRA=\"1\" LOCALY=\"0\" NEXTPAGE=\"-1\" TABS=\"\" NUMTAB=\"0\" NUMDASH=\"0\" LeftLine=\"0\" PRFILE=\"\" TEXTRA=\"1\" SCALETYPE=\"1\" TXTFILL=\"Black\" BEXTRA=\"1\" PLINEJOIN=\"0\" TXTSTRSH=\"100\" >\n    <ITEXT CAB=\"2\" CCOLOR=\"Black\" CSIZE=\"10\" CH=\"$folder/\" CSHADE2=\"100\" CSCALE=\"100\" CSHADE=\"100\" CSTYLE=\"0\" CFONT=\"Luxi Sans Regular\" CEXTRA=\"0\" CSTROKE=\"Black\" />\n   </PAGEOBJECT>"; }   print FILE "   <PAGEOBJECT BottomLine=\"0\" REXTRA=\"1\" ALIGN=\"0\" LANGUAGE=\"English\" NUMPO=\"16\" PLINEART=\"1\" TXTSCALE=\"100\" RightLine=\"0\" LOCALSCX=\"0.18\" LINESP=\"14.4\" ROT=\"0\" TXTSTROKE=\"Black\" WIDTH=\"288\" BACKPAGE=\"-1\" GROUPS=\"\" LOCKR=\"0\" IFONT=\"Bitstream Vera Sans Mono Roman\" LOCALSCY=\"0.18\" NAMEDLST=\"\" AUTOTEXT=\"0\" FLIPPEDV=\"0\" EXTRAV=\"0\" PCOLOR=\"White\" RADRECT=\"0\" REVERS=\"0\" PRINTABLE=\"1\" RATIO=\"1\" FLIPPEDH=\"0\" COLGAP=\"0\" PCOLOR2=\"None\" NEXTITEM=\"-1\" NUMGROUP=\"0\" TransValue=\"0\" INVERS=\"0\" PLINEEND=\"0\" FRTYPE=\"0\" PTYPE=\"2\" isTableItem=\"0\" TEXTFLOW2=\"0\" SHADE2=\"100\" PWIDTH=\"1\" HEIGHT=\"217\" DASHOFF=\"0\" PFILE2=\"\" PFILE=\"$folder/$file\" TEXTFLOW3=\"0\" ISIZE=\"12\" PLTSHOW=\"0\" TXTSTYLE=\"0\" CLIPEDIT=\"0\" BACKITEM=\"-1\" BookNr=\"0\" TransValueS=\"0\" EMBEDDED=\"1\" PFILE3=\"\" ANNAME=\"Image$image\" SHADE=\"100\" COCOOR=\"0 0 0 0 288 0 288 0 288 0 288 0 288 217 288 217 288 217 288 217 0 217 0 217 0 217 0 217 0 0 0 0 \" BASEOF=\"0\" PICART=\"1\" COLUMNS=\"1\" LAYER=\"0\" BOOKMARK=\"0\" TopLine=\"0\" LOCK=\"0\" EPROF=\"\" BBOXX=\"0\" DASHS=\"\" IRENDER=\"1\" TEXTFLOW=\"0\" BBOXH=\"1200\" TXTFILLSH=\"100\" YPOS=\"$ypos[$piccount]\" ANNOTATION=\"0\" LOCALX=\"0\" GRTYP=\"0\" XPOS=\"$xpos[$piccount]\" NUMCO=\"16\" POCOOR=\"0 0 0 0 288 0 288 0 288 0 288 0 288 217 288 217 288 217 288 217 0 217 0 217 0 217 0 217 0 0 0 0 \" EXTRA=\"1\" LOCALY=\"0\" NEXTPAGE=\"-1\" TABS=\"\" NUMTAB=\"0\" NUMDASH=\"0\" LeftLine=\"0\" PRFILE=\"\" TEXTRA=\"1\" SCALETYPE=\"0\" TXTFILL=\"Black\" BEXTRA=\"1\" PLINEJOIN=\"0\" TXTSTRSH=\"100\" />\n"; print FILE "  <PAGEOBJECT BottomLine=\"0\" REXTRA=\"1\" ALIGN=\"0\" LANGUAGE=\"English\" NUMPO=\"16\" PLINEART=\"1\" TXTSCALE=\"100\" RightLine=\"0\" LOCALSCX=\"1\" LINESP=\"12\" ROT=\"0\" TXTSTROKE=\"Black\" WIDTH=\"148\" BACKPAGE=\"-1\" GROUPS=\"\" LOCKR=\"0\" IFONT=\"Luxi Sans Regular\" LOCALSCY=\"1\" NAMEDLST=\"\" AUTOTEXT=\"0\" FLIPPEDV=\"0\" EXTRAV=\"0\" PCOLOR=\"None\" RADRECT=\"0\" REVERS=\"0\" PRINTABLE=\"1\" RATIO=\"1\" FLIPPEDH=\"0\" COLGAP=\"0\" PCOLOR2=\"None\" NEXTITEM=\"-1\" NUMGROUP=\"0\" TransValue=\"0\" INVERS=\"0\" PLINEEND=\"0\" FRTYPE=\"0\" PTYPE=\"4\" isTableItem=\"0\" TEXTFLOW2=\"0\" SHADE2=\"100\" PWIDTH=\"1\" HEIGHT=\"22\" DASHOFF=\"0\" PFILE2=\"\" PFILE=\"\" TEXTFLOW3=\"0\" ISIZE=\"10\" PLTSHOW=\"0\" TXTSTYLE=\"0\" CLIPEDIT=\"0\" BACKITEM=\"-1\" BookNr=\"0\" TransValueS=\"0\" EMBEDDED=\"1\" PFILE3=\"\" ANNAME=\"\" SHADE=\"100\" COCOOR=\"0 0 0 0 148 0 148 0 148 0 148 0 148 22 148 22 148 22 148 22 0 22 0 22 0 22 0 22 0 0 0 0 \" BASEOF=\"0\" PICART=\"1\" COLUMNS=\"1\" LAYER=\"0\" BOOKMARK=\"0\" TopLine=\"0\" LOCK=\"0\" EPROF=\"\" BBOXX=\"0\" DASHS=\"\" IRENDER=\"-279058628\" TEXTFLOW=\"0\" BBOXH=\"0\" TXTFILLSH=\"100\" YPOS=\"$ypostxt[$piccount]\" ANNOTATION=\"0\" LOCALX=\"0\" GRTYP=\"0\" XPOS=\"$xpostxt[$piccount]\" NUMCO=\"16\" POCOOR=\"0 0 0 0 148 0 148 0 148 0 148 0 148 22 148 22 148 22 148 22 0 22 0 22 0 22 0 22 0 0 0 0 \" EXTRA=\"1\" LOCALY=\"0\" NEXTPAGE=\"-1\" TABS=\"\" NUMTAB=\"0\" NUMDASH=\"0\" LeftLine=\"0\" PRFILE=\"\" TEXTRA=\"1\" SCALETYPE=\"1\" TXTFILL=\"Black\" BEXTRA=\"1\" PLINEJOIN=\"0\" TXTSTRSH=\"100\" >\n    <ITEXT CAB=\"$cab[$piccount]\" CCOLOR=\"Black\" CSIZE=\"10\" CH=\"$file\" CSHADE2=\"100\" CSCALE=\"100\" CSHADE=\"100\" CSTYLE=\"0\" CFONT=\"Luxi Sans Regular\" CEXTRA=\"0\" CSTROKE=\"Black\" />\n   </PAGEOBJECT>\n "; ++$piccount; ++$image; } print FILE "</PAGE>\n"; print FILE "$docend\n"; close (FILE); print "All finished printing to $album\n";
 * 1) Now let's build some pages
 * 1) This will print the directory name in the upper right hand corner of the page
 * 1) The individual image files
 * 1) The image file name in a text frame
 * 1) All the pics should be in now...finish up

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)