Difference between revisions of "Gallery.py"

From Scribus Wiki
Jump to: navigation, search
(Created page with "{{Scripting Index]] ===gallery.py=== <syntaxhighlight lang=python> #!/usr/bin/env python # -*- coding: utf-8 -*- # gallery.py # created 2016.11.19 Gregory Pittman """ USAGE...")
 
Line 1: Line 1:
{{Scripting Index]]
+
{{Scripting Index}}
  
 
===gallery.py===
 
===gallery.py===

Revision as of 17:44, 19 November 2016

This article is part of the Scripts series.

gallery.py

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

"""
USAGE
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 
directory.

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

try:
	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.'
	sys.exit(1)

try:
    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)
	sys.exit(2)

original_units = scribus.getUnit()
scribus.setUnit(scribus.UNIT_POINTS)
framecount = scribus.selectionCount()
filetype = [".jpg",".jpeg",".png",".tif",".JPG",".JPEG",".PNG",".TIF"] # here are the file formats we are looking for
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)
    sys.exit(2)
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(250,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)
    scribus.setScaleImageToFrame(1,1,currentframe)
    frame += 1
    framecount -= 1
    nextimage += 1
    if framecount <= 0:
        break
scribus.setUnit(original_units)