Importação automática de imagens V2

From Scribus Wiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
Este artigo é parte da seção de Scripts_e_Plugins.
Scripts para iniciantes Scripts de extensão/PyQt Questões sobre Py Plugins Outros
Voltar para a página principal

Versão em inglês.

Básico

scribalbum2.py

Esta é uma nova versão do script de importação de imagens que não usa o TKinter, mas o valueDialog() nas entradas.

Nesta nova versão você poderá selecionar entre 4 ou 6 imagens por página, além de poder selecionar o diretório através de uma interface gráfica.

As imagens são carregadas na seqüência Acima > Esquerda > Direita > Abaixo.

#!/usr/bin/env python

# File: scribalbum2.py
# criado originalmente em 2005.02.13 por Gregory Pittman
# modificado em 2006.06.28
# Atenção: Esta é a versão Carta (padrão EUA)
# Coloca 4 a 6 imagens por página
# Esta versão usa uma diálogo de arquivos e
# de diretório nas seleções.
# Filtragem automática de casos com
# .jpg, .png, .tif, .gif, .pdf, e
# equivalentes em maiúsculo.
# Cria um novo documento e não falhará se você já tiver criado um.

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('Imagens','- Papel Carta EUA-\n Quatro Imagens por Página?\nSim = 4, Não = 6',button1=scribus.BUTTON_YES,button2=scribus.BUTTON_NO)
imagedir = scribus.fileDialog('Selecione o diretório de imagens','Diretórios',isdir=True)
imagetype = scribus.valueDialog('Tipos de imagens','Entre com o tipo de imagem sendo\n j=jpg,p=png,t=tif,g=gif,P=pdf\n "jptgP" seleciona todos','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]
# Esta proporção é a certa para fotografias (pelo menos para a minha Olympus digital)
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 é o quadro no topo de cada página exibindo o nome do diretório
            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)
# Aqui, as imagens são carregadas na página, quatro por vez, então volta ao início e cria uma página nova
            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 é o rótulo de cada imagem com a posição e o comprimento ajustados de
# acordo com o tamanho do texto. Caso você altere a fonte ou seu tamanho,
# será necessário alterar apenas o cálculo aproximado.
                    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 ('Não encontrado','Nenhuma imagem encontrada com\n esta seleção',scribus.BUTTON_OK)

scribalbum_a4.py

Esta versão coloca 8 imagens em uma página A4 deixando pouco espaço extra. O uso é igual ao descrito acima. Esta versão será atualizada para ser similar em funcionalidade ao script acima (por isso suas strings não estão traduzidas).


#!/usr/bin/env python

# File: scribalbum2.py
# criado originalmente em 2005.02.13 por Gregory Pittman
# modificado em 2006.06.28
# Atenção: Esta é a versão A4 (padrão DIN)
# # Esta versão usa uma diálogo de arquivos e
# de diretório nas seleções.
# Filtragem automática de casos com
# .jpg, .png, .tif, .gif, .pdf, e
# equivalentes em maiúsculo.
# Cria um novo documento 


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)