def pdf2img(input_pdf, postfix='.png'): img = Image(input_pdf) img.density('300') size = "%sx%s" % (img.columns(), img.rows()) output_img = Image(size, bgcolor) output_img.type = img.type output_img.composite(img, 0, 0, PythonMagick.CompositeOperator.SrcOverCompositeOp) output_img.resize(str(img.rows())) output_img.magick('JPG') output_img.quality(75) output_jpg = input_pdf.replace(".pdf", postfix) if os.path.exists(output_jpg): os.remove(output_jpg) output_img.write(output_jpg)
def pdf2img(input_pdf, postfix='.png', **kwargs): # print os.path.exists(input_pdf) img = Image(input_pdf) img.density('300') size = "%sx%s" % (img.columns(), img.rows()) output_img = Image(size, bgcolor) output_img.type = img.type output_img.composite(img, 0, 0, PythonMagick.CompositeOperator.SrcOverCompositeOp) output_img.resize(str(img.rows())) output_img.magick('JPG') output_img.quality(75) if 'out_path' in kwargs: output_jpg = kwargs['out_path'] else: output_jpg = input_pdf + postfix if os.path.exists(output_jpg): os.remove(output_jpg) output_img.write(output_jpg)
def convertMGtoPIL(magickimage): 'works with grayscale and color' img = PMImage(magickimage) # make copy img.depth = 8 # this takes 0.04 sec. for 640x480 image img.magick = "RGB" w, h = img.columns(), img.rows() blb=Blob() img.write(blb) data = blb.data # convert string array to an RGB Pil image pilimage = Image.fromstring('RGB', (w, h), data) return pilimage
def pdf_to_image(): for pdf in [ pdf_file for pdf_file in os.listdir(pdf_dir) if pdf_file.endswith(".pdf") ]: input_pdf = pdf_dir + "\\" + pdf + "[1]" img = Image() img.density('300') print input_pdf img.read(input_pdf) size = "%sx%s" % (img.columns(), img.rows()) output_img = Image(size, bg_colour) output_img.type = img.type output_img.composite(img, 0, 0, PythonMagick.CompositeOperator.SrcOverCompositeOp) output_img.resize(str(img.rows())) output_img.magick('JPG') output_img.quality(75) output_jpg = input_pdf.replace(".pdf", ".jpg") output_img.write(output_jpg)
from PythonMagick import Image from datetime import datetime start_time = datetime.now() pdf_dir = "/run/media/gru/Storage/Thesis-Latex/figures/vis-results" bg_colour = "#ffffff" for root, _, pdfs in os.walk(pdf_dir): for pdf in pdfs: if '.pdf' in pdf: input_pdf = os.path.join(root, pdf) print(input_pdf) img = Image() # img.density('300') img.read(input_pdf) size = "%sx%s" % (img.columns(), img.rows()) output_img = Image(size, bg_colour) output_img.type = img.type output_img.composite( img, 0, 0, PythonMagick.CompositeOperator.SrcOverCompositeOp) output_img.resize(str(800)) output_img.magick('PNG') output_img.quality(75) output_jpg = input_pdf.replace(".pdf", ".png") output_img.write(output_jpg) print(datetime.now() - start_time)
import os, PythonMagick from PythonMagick import Image from datetime import datetime start_time = datetime.now() pdf_dir = r"C:\MyPDFs" bg_colour = "#ffffff" for pdf in [pdf_file for pdf_file in os.listdir(pdf_dir) if pdf_file.endswith(".pdf")]: input_pdf = pdf_dir + "\\" + pdf img = Image() img.density('300') img.read(input_pdf) size = "%sx%s" % (img.columns(), img.rows()) output_img = Image(size, bg_colour) output_img.type = img.type output_img.composite(img, 0, 0, PythonMagick.CompositeOperator.SrcOverCompositeOp) output_img.resize(str(img.rows())) output_img.magick('JPG') output_img.quality(75) output_jpg = input_pdf.replace(".pdf", ".jpg") output_img.write(output_jpg) print datetime.now() - start_time