def pdf2images(name): np = getPdfNumPages(name) for p in range(np): i = Image() i.density('200') i.quality(100) i.depth(24) #i.backgroundColor( #i.channel( i.read(name + '[' + str(p) + ']') i.write(name + str(p) + defaultImageExtension)
def convert_photo_to_jpeg(photo_path): print 'Converting %s to JPEG...' % (photo_path,) image = Image(photo_path) image.magick("jpeg") image.quality(100) (_, file_name) = os.path.split(photo_path) file_name = convert_local_filename_to_flickr(file_name) file_name = os.path.join(tempfile.mkdtemp('.flickrsmartsync'), file_name) image.write(file_name) print 'Done converting photo!' return file_name
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)
import os import PythonMagick import ghostscript from PythonMagick import Image pdf_dir = 'C:\\Users\\user\\Desktop\\fp' 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 = PythonMagick.Image(input_pdf) img.density('300') 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(100) output_jpg = input_pdf.replace(".pdf", ".jpg") output_img.write(output_jpg) print "finish"
import os from PyPDF4 import PdfFileReader, PdfFileWriter from tempfile import NamedTemporaryFile from PythonMagick import Image reader = PdfFileReader(open("D:/data/pdf-scan/普通生物学(清晰PDF版).pdf", "rb")) for page_num in range(reader.getNumPages()): writer = PdfFileWriter() writer.addPage(reader.getPage(page_num)) temp = NamedTemporaryFile(prefix=str(page_num), suffix=".pdf", delete=False) writer.write(temp) print(temp.name) tempname = temp.name temp.close() im = Image(tempname) im.quality(100) # 0-100 full compression # 不保持比例 #im.sample('298x412!') # 保持比例 im.sample('1788x2526') #im.density("3000") # DPI, for better quality # im.read(tempname) im.write("D:/data/pdf-scan/temp-pdf/{}.jpeg".format(page_num)) os.remove(tempname)
def modify_images(application_path): output_dirs = [ path.join(application_path, "..", "512"), path.join(application_path, "..", "150") ] count_files = 0 override = None FileUtilities.create_needed_folder(output_dirs) print() print( "\t--------------------------------------------------------------------" ) print() print("\tErstelle die Bilder in der Größe \"512px\":") print() for filename in listdir(application_path): if not filename.endswith(".jpg"): continue if path.isfile(path.join(output_dirs[0], filename)): if override is None: override = TerminalUtilities.query_yes_no( "\tSollen die bereits vorhandenen Bilder überschrieben werden?", False) if not override: continue try: img = Image(path.join(application_path, filename)) # noinspection PyArgumentList img.strip() # noinspection PyArgumentList img.trim() img.quality(80) img.resize("512x512>") img.write(path.join(output_dirs[0], filename)) except RuntimeError as error: TerminalUtilities.error_handler(str(error), "RuntimeError") print("\t" + filename + "\t erfolgreich erstellt.") count_files += 1 print() print("\tInsgesamt wurden " + str(count_files) + " Bilder in der Größe \"512px\" erzeugt.") print() print( "\t--------------------------------------------------------------------" ) print() print("\tErstelle die Bilder in der Größe \"150px\":") print() count_files = 0 for filename in listdir(output_dirs[0]): if not filename.endswith(".jpg"): continue if (not override) and (path.isfile(path.join(output_dirs[1], filename))): continue img = Image(path.join(output_dirs[0], filename)) img.resize("150x150>") img.write(path.join(output_dirs[1], filename)) print("\t" + filename + "\t erfolgreich erstellt.") count_files += 1 print() print("\tInsgesamt wurden " + str(count_files) + " Bilder in der Größe \"150px\" erzeugt.")