Black background python image

Asked

Viewed 303 times

0

I’m using pdf2image to export the pdf pages and join in just one jpg, one page at the bottom of the other forming a "gut", but my whole file gets black bg.

#!/usr/bin/env python
# coding: UTF-8

import img2pdf
import os
import time
import sys
import tempfile
import webbrowser
import colorama
import PIL
import numpy as np
from colorama import Fore, Back, Style
from PIL import Image
from pdf2image import convert_from_path

class ConvertPdf2Img():

@classmethod
def export(self, one_page = False):
    filename = os.path.join(sys.path[0] ,'pdfs', 'nt.pdf')
    save_dir = os.path.join(sys.path[0] ,'jpgs')
    list_jpg = []

    time.sleep(2)
    print(Fore.GREEN + "Iniciando exportacao PDF > JPEG..." + Style.RESET_ALL)

    with tempfile.TemporaryDirectory():
        images_from_path = convert_from_path(filename, 300)

        i = 0
        for page in images_from_path:
            i += 1

            print(Fore.RED + "Exportando pagina page-" + str(i) + ".jpg ..." + Style.RESET_ALL)

        save_filename = os.path.join(save_dir, 'page-' + str(i) + '.jpg')
        page.save(os.path.join(save_dir, save_filename), 'JPEG')

        list_jpg.append(save_filename)

    time.sleep(2)

    images = map(Image.open, list_jpg)
    widths, heights = zip(*(i.size for i in images))

    total_width = max(widths)
    total_height = sum(heights)

    new_im = Image.new('RGB', (total_width, total_height))

    offset = 0
    for im in images:

        # center
        x = int((total_width - im.size[0])/2)

        new_im.paste(im, (x, offset))
        offset += im.size[1]

        new_im.save(os.path.join(save_dir, 'tripa.jpg'))

     if(one_page):
         for jpg in list_jpg:
             os.remove(jpg)

     print(Fore.GREEN + "JPEG finalizado ! Abrindo pasta com jpg(s) convertidos..." + Style.RESET_ALL)
     time.sleep(2)
     webbrowser.open('file:///' + save_dir)

 # chamando metodo
 me = ConvertPdf2Img()
 me.export(True)

inserir a descrição da imagem aqui

1 answer

0

The map has to be converted into a list.

images = list(map(Image.open, list_jpg))
  • so it returns me error 'Memoryerror'

  • if I use a pdf with 5 pages for example it works, but I will have pdf with 18 for example, ai ta exploding

Browser other questions tagged

You are not signed in. Login or sign up in order to post.