Difference between revisions of "Gallery.py"

From Scribus Wiki
Jump to: navigation, search
Line 93: Line 93:
if frame_width == 0:
if frame_width == 0:
     pil_found = 0
     pil_found = 0
Then, farther down, make this edit:
<syntaxhighlight lang=python>

Revision as of 18:39, 19 November 2016

This article is part of the Scripts series.

Here is another script to automatically load images. Rather than the set layouts that other scripts generate, this one relies on the user to have a number of image frames created and selected, on one or more pages. You then select a directory of images. If you run out of frames or images, the script quits. Meanwhile, if you have Python Imaging Library, it will resize your frames proportionally to the image (as written with an arbitrary width of 250 points). What this means is that you could just have tiny frames, where you mainly are selecting some X,Y position on the page for the frame, not worrying about sizing it.

As stated in the USAGE notes, if you select your frames in the Outline dialog (note that you select multiple frames with Ctrl-click in the dialog, as opposed to Shift-click on the page itself), it can be easier than scrolling down the physical pages.


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# gallery.py
# created 2016.11.19  Gregory Pittman

Before running this script, you should have created and selected one or more image frames.
Run the script, after which the script informs you of your selection number, then asks for a 

Images are loaded from your chosen directory, and frame size adjusted to 300 points width, and
height adjusted proportionally according to the image size (if you have PIL).
The script will quit either when you run out of selected frames or run out of images in the
selected directory.

HINT: Using the Outline dialog can facilitate frame selection, especially when you are selecting
frames on more than one page. The order of selection also matters, changing the order of frames
to receive images.


import sys
import os

	import scribus
except ImportError:
	print 'Unable to import the scribus module. This script will only run within'
	print 'the Python interpreter embedded in Scribus. Try Script->Execute Script.'

    from PIL import Image
    pil_found = 1
except ImportError:
    pil_found = 0

if not scribus.haveDoc():
	scribus.messageBox('Error','You must have a document open',scribus.ICON_WARNING,scribus.BUTTON_OK)

original_units = scribus.getUnit()
framecount = scribus.selectionCount()
filetype = [".jpg",".jpeg",".png",".tif",".JPG",".JPEG",".PNG",".TIF"] # here are the file formats we are looking for. Add more if desired.
if framecount == 0:
    scribus.messageBox('Scribus - Script Error','There is no object selected. Please select at least one image frame and try again.',scribus.ICON_WARNING, scribus.BUTTON_OK)
imageframes = []
images = []
frame = int(0)
nextimage = 0

scribus.messageBox('Image File', 'You have selected ' + str(framecount) + ' frames',scribus.ICON_NONE, scribus.BUTTON_OK)
imagedir = scribus.fileDialog('Select Image Directory', 'All Supported Formats(*.*)',isdir=True)
files = os.listdir(imagedir)
for t in files:
    for filetp in filetype:
        if t.endswith(filetp):
            images.append(imagedir +"/"+ t)
nrimages = len(images)
while nextimage <= nrimages - 1:
    newimage = images[nextimage]
    currentframe = scribus.getSelectedObject(frame)
    if pil_found == 1:  # if you don't have PIL, frame will not be resized
        im = Image.open(newimage)
        xsize, ysize = im.size
        scribus.sizeObject(float(250),float(250*ysize/xsize),currentframe) # if you do not want a standard width frame of 250 points, edit this command or comment out
    scribus.loadImage(newimage, currentframe)
    frame += 1
    framecount -= 1
    nextimage += 1
    if framecount <= 0:


If you want to choose the width or maybe even opt out of resizing, add this just after the line nrimages = len(images):

frame_width = scribus.valueDialog('Size for Width', 'Change this to the standard width you desire, in points.\nChange to 0 to opt out of resizing.',"250")
frame_width = int(frame_width)
if frame_width == 0:
    pil_found = 0

Then, farther down, make this edit: