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

From Scribus Wiki
Jump to navigation Jump to search
No edit summary
(reworking of scribalbum2.py)
Line 14: Line 14:
#!/usr/bin/env python
#!/usr/bin/env python


# File: scribalbum_2.py
# File: scribalbum2.py
# originally 2005.02.13  Gregory Pittman
# originally 2005.02.13  Gregory Pittman
# modified  2006.03.17 - from scribalbum.py
# modified  2006.06.28
# This version for US Letter paper
# This version for US Letter paper
# Puts 4 pictures per page, to allow for text frame notes in between
# Puts 4 or 6 pictures per page
# This version does without Tkinter, uses valueDialog(), to give you an
# This version uses fileDialog to choose the directory
# Entry dialog for the name of a directory, another to choose file type(s)
# Entry dialog to choose file type(s)
# Filters out files ending with .jpg, .png, .tif, .gif, .pdf, and
# Filters out files ending with .jpg, .png, .tif, .gif, .pdf, and
# uppercase equivalents.
# uppercase equivalents.
# Makes a new document, and will not fault if you already have one.
# Makes a new document, and will not fault if you already have one.


from scribus import *
import scribus
import os
import os


Line 31: Line 31:
dicttype = {'j':'.jpg','p':'.png','t':'.tif','g':'.gif','P':'.pdf'}
dicttype = {'j':'.jpg','p':'.png','t':'.tif','g':'.gif','P':'.pdf'}
Dicttype = {'j':'.JPG','p':'.PNG','t':'.TIF','g':'.GIF','P':'.PDF'}
Dicttype = {'j':'.JPG','p':'.PNG','t':'.TIF','g':'.GIF','P':'.PDF'}
 
nrimages = scribus.messageBox('Pictures','- US Letter Paper -\n Four Images per Page?\nYes = 4, No = 6',button1=scribus.BUTTON_YES,button2=scribus.BUTTON_NO)
imagedir = valueDialog('Images','Enter the Image Directory')
imagedir = scribus.fileDialog('Select Image Directory','Directories',isdir=True)
imagetype = valueDialog('Image Types','Enter the Image Types, where\n j=jpg,p=png,t=tif,g=gif,P=pdf\n "jptgP" selects all','jptgP')
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:]:
for t in imagetype[0:]:
     filetype.append(dicttype[t])
     filetype.append(dicttype[t])
Line 45: Line 45:
D.sort()
D.sort()


# There are 4 pictures per page; xpos and ypos are the x,y coord of the upper left corners
# When 4 pics per page, coords are: (15, 42),(310, 187), (15, 388), (310, 533)
# Coords are: (15, 42),(15, 388), (310, 187), (310, 533)
# When 6 pics per page: (15, 42),(310, 42), (15, 290), (310, 290),(15,533),(310,533)
xpos = (15, 310)    
if nrimages == 3:
ypos = (42, 388, 187, 533)
    xpos = [15, 310, 15, 310]    
    ypos = [42, 187, 388, 533]
if nrimages == 4:
    xpos = [15, 310, 15, 310, 15, 310]
    ypos = [42, 42, 290, 290, 533, 533]
# This proportion is right for photographs (at least for my digital Olympus)
# This proportion is right for photographs (at least for my digital Olympus)
pwidth = 288
pwidth = 288
pheight = 217
pheight = 217
imagecount = 0
imagecount = 0
framecount = 0
#framecount = 0
if len(D) > 0:
if len(D) > 0:
     if newDoc(PAPER_LETTER, (10,10,20,20),PORTRAIT, 1, UNIT_POINTS, NOFACINGPAGES, FIRSTPAGERIGHT):
     if scribus.newDoc(scribus.PAPER_LETTER, (10,10,20,20),scribus.PORTRAIT, 1, scribus.UNIT_POINTS, scribus.NOFACINGPAGES, scribus.FIRSTPAGERIGHT):
         while imagecount < len(D):
         while imagecount < len(D):
             if imagecount > 0:
             if imagecount > 0:
                 newPage(-1)
                 scribus.newPage(-1)
                 framecount = 0
                 framecount = 0
# L is the frame at the top of each page showing the directory name
# L is the frame at the top of each page showing the directory name
             L = createText(15, 20, 200, 20)
             L = scribus.createText(15, 20, 200, 20)
             setText("Dir: " + imagedir, L)
             scribus.setText("Dir: " + imagedir, L)
             setTextAlignment(ALIGN_LEFT, L)
             scribus.setTextAlignment(scribus.ALIGN_LEFT, L)
             setFont("Luxi Sans Regular", L)
             scribus.setFont("Luxi Sans Regular", L)
             setFontSize(10, 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
# 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:
             for x,y in zip(xpos,ypos):
                 if imagecount < len(D):
                 if imagecount < len(D):
                     f = createImage(xframe, ypos[framecount], pwidth, pheight)
                     f = scribus.createImage(x, y, pwidth, pheight)
                     loadImage(imagedir + '/' + D[imagecount], f)
                     scribus.loadImage(imagedir + '/' + D[imagecount], f)
                     setScaleImageToFrame(scaletoframe=1, proportional=1, name=f)
                     scribus.setScaleImageToFrame(scaletoframe=1, proportional=1, name=f)
                     lenfilename = len(D[imagecount])
                     lenfilename = len(D[imagecount])
                     Lpiclen = int(5.3 * lenfilename)
                     Lpiclen = int(5.3 * lenfilename)
# Lpic is the label for each picture, with position and length adjusted
# Lpic is the label for each picture, with position and length adjusted
# according to the text length.
# according to the text length, so if you change the font or its size,
# So if you change the font or its size, you may need to adjust
# you may need to adjust this only approximate calculation.
# this only approximate calculation.
                     Lpic = scribus.createText(x, y + 217, Lpiclen, 15)
                     Lpic = createText(xframe, ypos[framecount] + 217, Lpiclen, 15)
                     scribus.setText(D[imagecount], Lpic)
                     setText(D[imagecount], Lpic)
                     scribus.setTextAlignment(scribus.ALIGN_RIGHT, Lpic)
                     setTextAlignment(ALIGN_RIGHT, Lpic)
                     scribus.setFont("Luxi Mono Regular", Lpic)
                     setFont("Luxi Mono Regular", Lpic)
                     scribus.setFontSize(8, Lpic)
                     setFontSize(8, Lpic)
                     scribus.setFillColor("White", Lpic)
                     setFillColor("White", Lpic)
                     imagecount += 1
                    imagecount = imagecount + 1
                    framecount = framecount + 1
                if imagecount < len(D):
                    f = createImage(xframe, ypos[framecount], pwidth, pheight)
                    loadImage(imagedir + '/' + 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)
     scribus.setRedraw(1)
     redrawAll()
     scribus.redrawAll()


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



Revision as of 02:41, 3 July 2006

This article is part of the Scripts series.

scribalbum_2.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 properly, but added uppercase equivalents.

As with the others, you can use relative or absolute paths for the directory, but since the directory is a label at the top of the page, an absolute directory entry will be more informative.

It would be nice to be able to use a requestor to click your way to a directory, but so far Scripter does not have a way to do this; you can use fileDialog() to select a file, but there is nothing for choosing a directory.

#!/usr/bin/env python

# File: scribalbum2.py
# originally 2005.02.13  Gregory Pittman
# modified   2006.06.28
# This version for US Letter paper
# Puts 4 or 6 pictures per page
# This version uses fileDialog to choose the directory
# Entry dialog to choose file type(s)
# Filters out files ending with .jpg, .png, .tif, .gif, .pdf, and
# uppercase equivalents.
# Makes a new document, and will not fault if you already have one.

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 = scribus.messageBox('Pictures','- US Letter Paper -\n Four Images per Page?\nYes = 4, No = 6',button1=scribus.BUTTON_YES,button2=scribus.BUTTON_NO)
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()

# 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 == 3:
    xpos = [15, 310, 15, 310]    
    ypos = [42, 187, 388, 533]
if nrimages == 4:
    xpos = [15, 310, 15, 310, 15, 310]
    ypos = [42, 42, 290, 290, 533, 533]
# This proportion is right for photographs (at least for my digital Olympus)
pwidth = 288
pheight = 217
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("Luxi Sans Regular", 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 + 217, Lpiclen, 15)
                    scribus.setText(D[imagecount], Lpic)
                    scribus.setTextAlignment(scribus.ALIGN_RIGHT, Lpic)
                    scribus.setFont("Luxi Mono Regular", 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'}
Dicttype = {'j':'.JPG','p':'.PNG','t':'.TIF','g':'.GIF','P':'.PDF'}

imagedir = valueDialog('Images','Enter the Image Directory')
imagetype = 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=[]
d = os.listdir(imagedir)
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
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("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 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("Luxi Mono Regular", 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)