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 pad_image_to_x480(file): from PythonMagick import Image, CompositeOperator fname = file.split(".")[0] ext = file.split(".")[-1] outfile = os.path.join(destdir, fname + "_" + "l" + ".jpg") ## Make BG layer bgimg = Image('400x480', 'white') ## Open Primary image img = Image(file) img.backgroundColor("white") img.sample('350x432') # Composite + Save Primary over bg, padding primary with white of bg type = img.type img.composite(bgimg, 0, 0, CompositeOperator.DstOverCompositeOp) img.magick('JPG') img.type = type img.quality(100) img.write(outfile)
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)