2015-01-07 11 views
13

Sto cercando di convertire un PDF in PNG - questo tutto funziona bene, però, l'immagine di uscita è ancora trasparente anche quando credo di avere disabilitato:Python Wand convertire i PDF in PNG disabilitare trasparente (alpha_channel)

with Image(filename='sample.pdf', resolution=300) as img: 
    img.background_color = Color("white") 
    img.alpha_channel = False 
    img.save(filename='image.png') 

che questo produce le immagini, ma sono trasparenti, ho anche provato il seguito:

with Image(filename='sample.pdf', resolution=300, background=Color('white')) as img: 
    img.alpha_channel = False 
    img.save(filename='image.png') 

che produce questo errore:

Traceback (most recent call last): 
    File "file_convert.py", line 20, in <module> 
    with Image(filename='sample.pdf', resolution=300, background=Color('white')) as img: 
    File "/Users/Frank/.virtualenvs/wand/lib/python2.7/site-packages/wand/image.py", line 1943, in __init__ 
    raise TypeError("blank image parameters can't be used with image " 
TypeError: blank image parameters can't be used with image opening parameters 

risposta

6

Da un previous answer, provare a creare un'immagine vuota con un colore di sfondo, quindi sovrapporre.

from wand.image import Image 
from wand.color import Color 

with Image(filename="sample.pdf", resolution=300) as img: 
    with Image(width=img.width, height=img.height, background=Color("white")) as bg: 
    bg.composite(img,0,0) 
    bg.save(filename="image.png") 
3

L'altra risposta (composizione con un'immagine bianca) funziona, ma solo nell'ultima pagina, come fa impostando direttamente il canale alfa. Le seguenti opere bacchetta 0.4.2:

im = wand_image(filename='/tmp/foo.pdf', resolution=200) 
for i, page in enumerate(im.sequence): 
    with wand_image(page) as page_image: 
     page_image.alpha_channel = False 
     page_image.save(filename='/tmp/foo.pdf.images/page-%s.png' % i) 

credo che questo è probabilmente un bug nel bacchetta. Sembra che l'impostazione del canale alfa per un PDF dovrebbe influire su tutte le pagine su, ma non su.

7

Ho anche avuto alcuni PDF da convertire in PNG. Questo ha funzionato per me e sembra più semplice di immagini compositing, come indicato sopra .:

all_pages = Image(blob=self.pdf)  # PDF will have several pages. 
single_image = all_pages.sequence[0] # Just work on first page 
with Image(single_image) as i: 
    i.format = 'png' 
    i.background_color = Color('white') # Set white background. 
    i.alpha_channel = 'remove'   # Remove transparency and replace with bg. 

Riferimento: wand.image

2

Per coloro che sono ancora avendo problemi con questo, ho trovato la soluzione (che funziona nella versione 0.4. 1 e sopra, non sono sicuro delle versioni precedenti). così si dovrebbe basta usare qualcosa di simile:

with Image(filename='sample.pdf', resolution=300) as img: 
img.background_color = Color("white") 
img.alpha_channel = 'remove' 
img.save(filename='image.png') 
2

Compilare le altre risposte, qui è la funzione che uso per convertire un PDF in pagine:

import os 
import sys 
import shutil 

from wand.image import Image, Color 


def convert_pdf(filename, output_path, resolution=150): 
    """ Convert a PDF into images. 

     All the pages will give a single png file with format: 
     {pdf_filename}-{page_number}.png 

     The function removes the alpha channel from the image and 
     replace it with a white background. 
    """ 
    all_pages = Image(filename=filename, resolution=resolution) 
    for i, page in enumerate(all_pages.sequence): 
     with Image(page) as img: 
      img.format = 'png' 
      img.background_color = Color('white') 
      img.alpha_channel = 'remove' 

      image_filename = os.path.splitext(os.path.basename(filename))[0] 
      image_filename = '{}-{}.png'.format(image_filename, i) 
      image_filename = os.path.join(output_path, image_filename) 

      img.save(filename=image_filename) 
+0

oh, così buona risposta. grazie – madjardi

Problemi correlati