script for Automatic import of images

Previous topic - Next topic

ni_soros

Hi,

Noob here, just trying to figure this out: I want to automatically import images using the scribalbum_letter.py script on this page:
http://wiki.scribus.net/canvas/Automatic_import_of_images:_Versions_not_requiring_Tkinter

but I am not able to run the script in the Scribus Python Console (Script>>Show Console). It returns many syntax and indentation error messages that I do not understand. Can someone help me out?

Thanks in advance!


Nermander

Have you been able to run other scripts?

How are you trying to run it? Are you pasting into the console?

polpero

Hi Nermander

Open Scribus (dont create any page)
Try Scripts/Execute script.../scribalbum_letter.py
select your folder and image Type
and voilà...

It worked well for me...

ni_soros

Thanks for the help.

Nermander, I ran the default scripts that were bundled into the program and those worked fine.

I have both pasted it into the console and run the .py file.

When I run the console, I am able to select an image directory and an image type, but then get these error messages below:

'\nThis is scribalbum_letter.py. This script will load the images from a\ndirectory, creating a new document of US Letter size.\n\nUSAGE: It is probably better not to already have any document in Scribus,\nsince memory usage can become intense when a large number of images is\nused. \nYou are first presented with a dialog to choose either 4 or 6 images per page.\nNext select a directory where your images are located. Finally, select the \ntypes of image files, using the code you see in the last dialog. \nNOTE: If you choose PDF, only the first page will be imported, and the PDF \nwill be rasterized.\n\nAUTHOR: Gregory Pittman Original version: 2005.02.13, this version: 2008.07.23\n\nLICENSE: This program is free software; you can redistribute it and/or modify \nit under the terms of the GNU General Public License as published by the Free \nSoftware Foundation; either version 2 of the License, or (at your option) any \nlater version.\n\n'
  File "<console>", line 1
    while (nrimages != '4') and (nrimages != '6'):
                   ^
SyntaxError: invalid syntax
  File "<console>", line 1
    nrimages = scribus.valueDialog('Pictures','- US Letter Paper -\n Four or Six Images per Page?\nChange to 6 as desired','4')
   ^
IndentationError: unexpected indent
  File "<console>", line 4
    d = os.listdir(imagedir)
    ^
SyntaxError: invalid syntax
  File "<console>", line 5
    D.sort()
    ^
SyntaxError: invalid syntax
  File "<console>", line 4
    if nrimages == '6':
     ^
SyntaxError: invalid syntax
  File "<console>", line 1
    xpos = [15, 310, 15, 310, 15, 310]
   ^
IndentationError: unexpected indent
  File "<console>", line 1
    ypos = [42, 42, 290, 290, 533, 533]
   ^
IndentationError: unexpected indent
  File "<console>", line 1
    else:
       ^
SyntaxError: invalid syntax
  File "<console>", line 1
    result = scribus.messageBox ('Not Found','No Images found with\n this search selection',scribus.BUTTON_OK)
   ^
IndentationError: unexpected indent



Polpero, thanks for the tip about not creating a page when I open the program. However, when I executed the script as suggested -- Scripts/Execute script.../scribalbum_letter.py -- I am told to choose the number of pages for the document -- 4 or 6 (something I don't see when I do it in the console), I select the image directory, then the image type, and then see a pop-up window that says this:

Traceback (most recent call last:
  File "", line 8, in
  File "C:\Users\Back Office\Box Sync\Satellite Photos\Image Creation with Scribus\scribalbum_letter.py", line 71, in
    scribus.setFont(labelFont, L)
ValueError: Font not found.


Unlike pasting the code into the console, this actually creates a page with a text box in it that says, "Dir: C:/Users/Back Office/Box".


Thanks again for your help!  :D I wonder if it has to do with the first line of the code, which is for linux, but I am in Windows...

ni_soros

Anyone understand that/have any thoughts??

a.l.e

hi,

could you explain us, why you are pasting the script into the console?

the "right" way to tweak a script is to store it under a different name, modify it, and then run the new file...

and: how good are you python skills? do you know that indenting has a meaning in python?

in my eyes you trying to do something somehow silly (but that should work) in a slightly wrong way (which triggers the errors)... and we can get that fixed? :-)

ciao
a.l.e

ni_soros

a.l.e,

So in the first half of ^^my message^^, I had been pasting the script into the console just to see if it would work. The first box of code above was the error message that was returned, with a bunch of indentation errors.

But I also did just what you said --

I pasted the following into a file that I titled scribalbum_letter.py (also available at http://wiki.scribus.net/canvas/Automatic_import_of_images:_Versions_not_requiring_Tkinter:

#!/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)


I actually tried this on a Linux machine, my personal laptop, and this worked when I ran the code in Scribus, but I need to make it run on the computer at my work running Windows.

When I do the same thing on the Windows computer, this happens:
- I am prompted to select a directory -- This is good!
- I am prompted to select the number of images per page -- This is good, too!

But then the script crashes and returns this error message:

Traceback (most recent call last):
  File "<string>", line 8, in <module>
  File "C:\Users\Back Office\Box Sync\Satellite Photos\Image Creation with Scribus\scribalbum_letter.py", line 71, in <module>
    scribus.setFont(labelFont, L)
ValueError: Font not found.


What up with that???

a.l.e

is the font "DejaVu Sans Book" installed on your windows computer?

ni_soros

Thank you so much, a.l.e!  It was the font! I changed it to "Calibri Light" and it worked perfectly.