def NumpytoIM(img, usm=None, verbose=False): if verbose: print "Converting numpy array to ImageMagick" out_img = PMImage() if img.dtype == 'uint16': out_img.depth(16) else: out_img.depth(8) out_img.magick('RGB') h,w,c = img.shape size_str = str(w)+'x'+str(h) out_img.size(size_str) b = Blob() b.data = img.tostring() out_img.read(b) out_img.magick('PNG') # Check if USM sharpening should be used if usm != None: if verbose: print "Running unsharp mask filter" r,s,a,t = (usm) out_img.unsharpmask(r,s,a,t) return out_img
def extractCover(uuid, data): """Extract the cover, resize, and save.""" f = NamedTemporaryFile(delete=False) f.write(data) f.close() image = Image(f.name) image.magick('JPEG') image.sample('256x256') image.write('htdocs/images/'+uuid+'.jpg') unlink(f.name)
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 readPdf_page_by_page(filepath): #获取一个pdf对象 pdf_input = PdfFileReader(open(filepath, 'rb')) #获取pdf页数 page_count = pdf_input.getNumPages() #获取pdf第n页的内容 for n in range(page_count): im = Image() #im.density("300") im.read(filepath + '[' + str(1) + ']') im.magick("jpg") im.write(filepath + str(n + 1) + ".jpg")
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 walk_menu(entry): if isinstance(entry, xdg.Menu.Menu) and entry.Show is True: map(walk_menu, entry.getEntries()) elif isinstance(entry, xdg.Menu.MenuEntry) and entry.Show is True: # byte 1 signals another entry conn.sendall('\x01') img_path = icon_attr(entry.DesktopEntry).encode('utf-8') if img_path and os.path.isfile(img_path): try: # Create an empty image and set the background color to # transparent. This is important to have transparent background # when converting from SVG img = Image() img.backgroundColor(Color(0, 0, 0, 0xffff)) img.read(img_path) # scale the image to 48x48 pixels img.scale(Geometry(48, 48)) # ensure the image is converted to ICO img.magick('ICO') b = Blob() img.write(b) # icon length plus data conn.sendall(struct.pack('i', len(b.data))) conn.sendall(b.data) except Exception: conn.sendall(struct.pack('i', 0)) else: conn.sendall(struct.pack('i', 0)) name = entry.DesktopEntry.getName() # name length plus data conn.sendall(struct.pack('i', len(name))) conn.sendall(name) command = re.sub(' -caption "%c"| -caption %c', ' -caption "%s"' % name, entry.DesktopEntry.getExec()) command = re.sub(' [^ ]*%[fFuUdDnNickvm]', '', command) if entry.DesktopEntry.getTerminal(): command = 'xterm -title "%s" -e %s' % (name, command) # command length plus data conn.sendall(struct.pack('i', len(command))) conn.sendall(command)
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 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)
def to_imagemagick(img, bits=16): '''Convert numpy array to Imagemagick format. :param img: image to convert :type img: Numpy ndarray :rtype: PythonMagick.Image ''' if not isinstance(img, PMImage): img = _scale(img, bits=bits) LOGGER.debug("Converting from Numpy to ImageMagick.") out_img = PMImage() if img.dtype == np.uint8: out_img.depth(8) else: out_img.depth(16) shape = img.shape # Convert also B&W images to 3-channel arrays if len(shape) == 2: tmp = np.empty((shape[0], shape[1], 3), dtype=img.dtype) tmp[:, :, 0] = img tmp[:, :, 1] = img tmp[:, :, 2] = img img = tmp out_img.magick('RGB') out_img.size(str(shape[1]) + 'x' + str(shape[0])) blob = Blob() blob.data = img.tostring() out_img.read(blob) out_img.magick('PNG') return out_img return img
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"