Automatic import of images: Versions not requiring Tkinter: Difference between revisions

From Scribus Wiki
Jump to navigation Jump to search
Line 9: Line 9:
This latest version (as of 2008.07.23) includes a GUI requester for the directory name. In addition, you now will have a choice of either 4 or 6 pictures per page. I also cleaned up the code a bit to make it less repetitive, and changed the way pictures load into the page -- instead of loading the left column, then the right, images are loaded L-R-L-R... down the page.
This latest version (as of 2008.07.23) includes a GUI requester for the directory name. In addition, you now will have a choice of either 4 or 6 pictures per page. I also cleaned up the code a bit to make it less repetitive, and changed the way pictures load into the page -- instead of loading the left column, then the right, images are loaded L-R-L-R... down the page.


Some rewriting was done so that this works with Scribus version 1.3.5svn, and the default font was changed to DejaVu Sans &ndash; if you want to use a different one, look for the <tt>labelFont</tt> variable assignment.
Some rewriting was done so that this works with Scribus version 1.3.5svn, and the default font was changed to DejaVu Sans &ndash; if you want to use a different one, look for the <tt>labelFont</tt> variable assignment. This version of scribalbum_letter also works with Scribus 1.3.3.x.


<pre>
<pre>

Revision as of 02:52, 25 July 2008

This article is part of the Scripts series.

scribalbum_letter.py

Link to: Automatic import of images from a directory using a script

I've reworked scribalbum.py to avoid the need for Tkinter, instead using valueDialog() for entries. You will see two of these, the first for entry of the directory to be searched, the second to choose image file types. Since the Scripter API does not yet have something like checkboxes, the most intuitive way I could think of was to use letter mnemonics, j = jpg, t = tif and so on, in Python using a dictionary approach. In this version I have deleted SVGs, since these do not import into image frames. Uppercase equivalents (.JPG, .TIF, etc) will now be detected.

This latest version (as of 2008.07.23) includes a GUI requester for the directory name. In addition, you now will have a choice of either 4 or 6 pictures per page. I also cleaned up the code a bit to make it less repetitive, and changed the way pictures load into the page -- instead of loading the left column, then the right, images are loaded L-R-L-R... down the page.

Some rewriting was done so that this works with Scribus version 1.3.5svn, and the default font was changed to DejaVu Sans – if you want to use a different one, look for the labelFont variable assignment. This version of scribalbum_letter also works with Scribus 1.3.3.x.

#!/usr/bin/env python

"""
This is scribalbum_letter.py. This script will load the images from a
directory, creating a new document of US Letter size.

USAGE: It is probably better not to already have any document in Scribus,
since memory usage can become intense when a large number of images is
used. 
You are first presented with a dialog to choose either 4 or 6 images per page.
Next select a directory where your images are located. Finally, select the 
types of image files, using the code you see in the last dialog. 
NOTE: If you choose PDF, only the first page will be imported, and the PDF 
will be rasterized.

AUTHOR: Gregory Pittman Original version: 2005.02.13, this version: 2008.07.23

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.

"""

import scribus
import os

filetype = []
dicttype = {'j':'.jpg','p':'.png','t':'.tif','g':'.gif','P':'.pdf'}
Dicttype = {'j':'.JPG','p':'.PNG','t':'.TIF','g':'.GIF','P':'.PDF'}
nrimages = '0'
while (nrimages != '4') and (nrimages != '6'):
    nrimages = scribus.valueDialog('Pictures','- US Letter Paper -\n Four or Six Images per Page?\nChange to 6 as desired','4')
imagedir = scribus.fileDialog('Select Image Directory','Directories',isdir=True)
imagetype = scribus.valueDialog('Image Types','Enter the Image Types, where\n j=jpg,p=png,t=tif,g=gif,P=pdf\n "jptgP" selects all','jptgP')
for t in imagetype[0:]:
    filetype.append(dicttype[t])
    filetype.append(Dicttype[t])
d = os.listdir(imagedir)
D = []
for file in d:
    for format in filetype:
        if file.endswith(format):
            D.append(file)
D.sort()
labelFont="DejaVu Sans Book"

# When 4 pics per page, coords are: (15, 42),(310, 187), (15, 388), (310, 533)
# When 6 pics per page: (15, 42),(310, 42), (15, 290), (310, 290),(15,533),(310,533)
if nrimages == '4':
    xpos = [15, 310, 15, 310]    
    ypos = [42, 187, 388, 533]
if nrimages == '6':
    xpos = [15, 310, 15, 310, 15, 310]
    ypos = [42, 42, 290, 290, 533, 533]
# This proportion is right for photographs from my digital camera
pwidth = 288
pheight = 193
imagecount = 0
#framecount = 0
if len(D) > 0:
    if scribus.newDoc(scribus.PAPER_LETTER, (10,10,20,20),scribus.PORTRAIT, 1, scribus.UNIT_POINTS, scribus.NOFACINGPAGES, scribus.FIRSTPAGERIGHT):
        while imagecount < len(D):
            if imagecount > 0:
                scribus.newPage(-1)
                framecount = 0
# L is the frame at the top of each page showing the directory name
            L = scribus.createText(15, 20, 200, 20)
            scribus.setText("Dir: " + imagedir, L)
            scribus.setTextAlignment(scribus.ALIGN_LEFT, L)
            scribus.setFont(labelFont, L)
            scribus.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 x,y in zip(xpos,ypos):
                if imagecount < len(D):
                    f = scribus.createImage(x, y, pwidth, pheight)
                    scribus.loadImage(imagedir + '/' + D[imagecount], f)
                    scribus.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 = scribus.createText(x, y + 193, Lpiclen, 15)
                    scribus.setText(D[imagecount], Lpic)
                    scribus.setTextAlignment(scribus.ALIGN_RIGHT, Lpic)
                    scribus.setFont(labelFont, Lpic)
                    scribus.setFontSize(8, Lpic)
                    scribus.setFillColor("White", Lpic)
                    imagecount += 1
                
    scribus.setRedraw(1)
    scribus.redrawAll()

else:
    result = scribus.messageBox ('Not Found','No Images found with\n this search selection',scribus.BUTTON_OK)

scribalbum_a4.py

This is the version that puts 8 images on A4 paper, with little extra space. Usage is as above.


#!/usr/bin/env python

# File: scribalbum_a4.py
# originally 2005.02.13  Gregory Pittman
# this version 2006.03.17 does without Tkinter
# Uses valueDialog() for data entry of directory name
# and also mnemonics for image types, j=jpg, and so on.
# Also searches for uppercase equivalents.
# Makes a new document

from scribus import *
import os

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

imagedir = valueDialog('Images','Enter the Image Directory')
imagetype = valueDialog('Image Types','Enter the Image Types, where\n j=jpg,J=jpeg,p=png,t=tif,g=gif,P=pdf\n "jJptgP" selects all','jJptgP')
for t in imagetype[0:]:
    filetype.append(dicttype[t])
    filetype.append(Dicttype[t])

D=[]
d = os.listdir(imagedir)
for file in d:
    for format in filetype:
        if file.endswith(format):
            D.append(file)
D.sort()
labelFont = "DejaVu Sans"
# 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
imagecount = 0
if len(D) > 0:
    if newDoc(PAPER_A4, (10,10,20,20),PORTRAIT, 1, UNIT_POINTS, NOFACINGPAGES, FIRSTPAGERIGHT):
        while imagecount < len(D):
            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: " + imagedir, L)
            setTextAlignment(ALIGN_LEFT, L)
            setFont(labelFont, 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 imagecount < len(D):
                        f = createImage(xframe, yframe, pwidth, pheight)
                        loadImage(imagedir + '/' + 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 in 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(labelFont, Lpic)
                        setFontSize(8, Lpic)
                        setFillColor("White", Lpic)
                        imagecount = imagecount + 1
                    
    setRedraw(1)
    redrawAll()

else:
    result = messageBox ('Not Found','No Images found with\n this search selection',BUTTON_OK)