couleur de fond d'evince

Avell Diroll avelldiroll at gmail.com
Ven 29 Jan 11:29:42 UTC 2010


spir wrote:
> On Fri, 29 Jan 2010 10:28:55 +0100
> Avell Diroll <avelldiroll at gmail.com> wrote:
>> Je vois 2 solutions possibles, modifier l'affichage (comme cela a été 
>> suggéré avec compiz-negative, mais cela nécessite une carte video 
>> supportant le compositing) ou modifier le document pdf (en changeant la 
>> couleur de fond avec gimp ou pdftk).
> 
> D'accord! La solution Compiz est en bonne voie ;-) (cf autre réponse)

Pour ceux qui ne peuvent pas faire tourner compiz ou qui le trouve trop 
lourd, voici un petit script qui permet de réaliser une copie d'un 
document pdf en changeant la couleur du fond (par défaut en gris).

C'est écrit en python (installé par défaut sous ubuntu) et nécéssite 
l'installation de 2 paquets: python-imaging et python-pypdf.

Ce script peut-être utilisé depuis la ligne de commande ou depuis nautilus :

1- copier le texte après les # dans un fichier (pdf_background.py par 
exemple)
2- rendre le fichier executable ("chmod +x pdf_background.py" ou clic 
droit sur le fichier > Propriétés > Permissions > Authoriser l'exécution)
3- déplacer le fichier dans le dossier $HOME/.gnome2/nautilus-scripts
4- un clic droit sur un fichier propose maintenant l'option 
script>pdf_background.py qui produira une copie grisée du pdf 
sélectionné appelée nomdupdf-with-bg.pdf

Ce programme est non testé en dehors du classique ça-marche-chez-moi™.

Ju

##############pdf_background.py############################
#!/usr/bin/env python

# This is a quick and dirty script to change the background colour
# of pdf files
# might be of use for people who don't like white background on screen
#
# This script require:
# *python: http://python.org/
# *PIL: http://www.pythonware.com/products/pil/
# *pyPDF: http://pybrary.net/pyPdf/
# On ubuntu (and debian like distros) you should be able to
# run this script
# after installing these packages:
# sudo apt-get install python-imaging python-pypdf

# pdf_background.py
# Copyright (c) 2010 Avell Diroll <avelldirolll at gmail.com>
#

"""pdf_background.py
This is a quick and dirty script to change the background colour of pdf 
files"""

__author__ =    "Avell Diroll <avelldirolll at gmail.com>"
__version__ =   "0.1"
__date__ =      "2010-01-29"
__copyright__ = "Copyright (c) 2010 %s. All rights reserved." % __author__
__licence__ =   "BSD"


from PIL import Image
from pyPdf import PdfFileWriter, PdfFileReader
from optparse import OptionParser
import sys
import tempfile

def change_background(input_file, output_file, background_colour):

     original = PdfFileReader(open(input_file))
     output = PdfFileWriter()


     for page_number in range(original.getNumPages()):
         page_width, page_height = 
original.getPage(page_number)['/MediaBox'][2:4]
         page_width = int(round(page_width))
         page_height = int(round(page_height))

         background_file = tempfile.NamedTemporaryFile()
         background = Image.new("RGB", (page_width, page_height), 
background_colour)
         background.save(background_file, "PDF")

         current_page = original.getPage(page_number)
         background_page = PdfFileReader(background_file)
         page = background_page.getPage(0)
         page.mergePage(current_page)
         output.addPage(page)

     output_stream = open(output_file, "wb")
     output.write(output_stream)
     output_stream.close()


def main():

     #chose your background colour (hexadecimal RGB value)
     background_colour = "#aaaaaa"

     # create the options we want to parse
     usage = "\n%%prog [options] pdf_file \n\n%s" %__doc__
     version="%%prog %s %s" %(__version__, __date__)
     optParser = OptionParser(usage=usage, version=version)
     optParser.add_option("-c", "--colour", type="string",
               dest="background_colour",
               default="#aaaaaa", metavar="#aaaaaa",
               help="background colour - hexadecimal RGB value")
     optParser.add_option("-s", "--suffix", type="string", dest="suffix",
               default="-with-bg",
               help="Suffix to append at the end of the output filename, "
               "default: -with-bg")
     (options, args) = optParser.parse_args()

     # check that they passed in atleast one file to process
     if len(args) != 1:
         optParser.error("Process one file at a time\n")

     input_file = args[0]
     output_file = args[0][:-4] + options.suffix + ".pdf"
     print output_file, background_colour

     change_background(input_file, output_file, background_colour)

     return 0


if __name__ == '__main__':
     sys.exit(main())




Plus d'informations sur la liste de diffusion ubuntu-fr