Example #1
0
    def split(self, raw_num):
        # Open Raw with imagemagick (format r00000.jpg)
        data = file('../raw/r' + zero_pad(raw_num, 5) + '.jpg', 'rb').read()
        raw = magick.Image(magick.Blob(data))

        images = deque()
        for area in templates[self.data['size']]:
            images.append({'img': self.crop(raw, area), 'area': area})

        return images
Example #2
0
def load_pdf_page(filepath, page_number):
    pdf_image_key = filepath + "[{0}]".format(page_number)
    if pdf_image_key in images_dict:
        image = images_dict[pdf_image_key]
        return image
    im = PythonMagick.Image(pdf_image_key)
    blob = PythonMagick.Blob()
    im.write(blob, "png")
    data = numpy.frombuffer(blob.data, dtype='uint8')
    image = cv2.imdecode(data, cv.CV_LOAD_IMAGE_COLOR)
    images_dict[pdf_image_key] = image
    return image
Example #3
0
def pdf2imageextract(pdffilename, pages):
    pdf_im = pyPdf.PdfFileReader(file(pdffilename, "rb"))
    npage = pdf_im.getNumPages(
    )  #It gives the number of pages in given input pdf file

    if int(pages) <= npage:
        for p in range(int(pages)):
            im = PythonMagick.Image()
            print type(im)
            im.density('300')
            im.read('{}[{}]'.format(pdffilename, str(p)))  #format strings
            im.write('{0}{1}{2}'.format('file_out-', str(p), '.png'))
            blob = PythonMagick.Blob()
            im.write(blob, "png")
            print blob.data
            print type(blob)
            data = PythonMagick.get_blob_data(blob)
            print type(data)
    else:
        print "Page Limit Exceeded"
Example #4
0
     canvas = Magick.Image(Magick.Geometry(outimagewidth, outimageheight),
                           "transparent")
     canvas.magick('PNG')
     gidarray = getTMXlayerData(path)
     for y in range(0, height):
         for x in range(0, width):
             Gid = x + (width * y)
             Tid = int(gidarray[Gid])
             if Tid != 0:
                 TidX = (Tid * tilewidth %
                         tilesetimgwidth) - tilewidth  #why need's -32?
                 TidY = ((Tid / (tilesetimgwidth / tilewidth)) *
                         tileheight % tilesetimgheight)
                 PosX = x * tilewidth
                 PosY = y * tileheight
                 tsi = Magick.Image(Magick.Blob(tilesetimageraw))
                 tsi.crop(Magick.Geometry(tilewidth, tileheight, TidX,
                                          TidY))  #or string "WxH+x+y"
                 canvas.composite(tsi, PosX, PosY,
                                  Magick.CompositeOperator.CopyCompositeOp)
     canvas.write(datafolder + '/' + str(imgc) + "_" + path.attrib['name'] +
                  '.png')
     #create layer
     stack = setORAlayer(
         stack, 'data/' + str(imgc) + "_" + path.attrib['name'] + '.png',
         path.attrib['name'], 'No' if 'visible' in path.attrib else 'Yes')
 elif tagtype == 'imagelayer':
     for images in path:
         #convert to png... Gimp don't handle mix of jpg and png on data folder...
         imgsrc = images.attrib['source']
         imgdest = datafolder + "/" + str(
i = 1
for row in rows:
    imageFile = row[1].replace('\\','/')
    try:
        img = PythonMagick.Image(imageFile)
    except Exception as e:
        eprint(imageFile + ': ' + e)
        continue
    imageFile = img.fileName()
    if max(img.size().height(), img.size().width()) > thumbDimension :
        try:
            img.transform(transformStr)
        except RuntimeError as e:
            eprint(str(row[0]) + " " + imageFile + ": " + str(e))
    blob = PythonMagick.Blob()
    img.quality(jpgQuality)
    img.write(blob, "jpg")
    #workaround in order the access the data within the blob
    try:
        blobData = blob.data
    except UnicodeDecodeError as e:
        blobData = e.object

    c.execute(updateQuery, (blobData, row[0]))
    if i%10 == 0:
        conn.commit()
    # progress
    print (str(i) + "/" + str(len(rows)) + ': ' + imageFile)
    i = i+1
Example #6
0
    def process(self, json_file):
        f = open(json_file, 'r')
        self.data = json.load(f)
        f.close()
        self.generate_special_lookup()
        self.developed = datetime.datetime.strptime(self.data['develop_date'],
                                                    "%Y-%m-%d")

        if self.make_display:
            self.display_path = display_sized_path
            # Make directory
            if not os.path.exists(self.display_path):
                os.makedirs(self.display_path)

        self.raw_start_num = zero_trim(self.data['raw_start'])
        self.raw_end_num = zero_trim(self.data['raw_end'])

        self.current_image_number = 0
        self.start_number_num = zero_trim(self.data['start_number'])
        self.last_digitized = datetime.datetime.now()
        self.image_num_strs = deque()
        if self.data['size'] == 'manual':
            for img_num in range(
                    self.start_number_num,
                    self.start_number_num + self.data['number_of_images']):
                if self.make_display:
                    img_num_str = zero_pad(img_num, 5)
                    data = file(img_num_str + '.jpg', 'rb').read()
                    img = magick.Image(magick.Blob(data))
                    self.display_size(img)
                self.add_comment(img_num_str, img_num_str + '.jpg')
                self.image_num_strs.append(img_num_str)
                self.current_image_number += 1

        else:
            for raw_num in range(self.raw_start_num, self.raw_end_num + 1):
                images = self.split(raw_num)

                # An order-dependant deque
                while images and (self.current_image_number <
                                  self.data['number_of_images']):
                    i = images.popleft()
                    image = i['img']
                    area = i['area']
                    img_num = self.start_number_num + self.current_image_number
                    img_num_str = zero_pad(img_num, 5)

                    rotation = "landscape"
                    if img_num_str in self.lookup:
                        if 'rotation' in self.lookup[img_num_str]:
                            rotation = self.lookup[img_num_str]['rotation']
                    self.rotate(image, area, rotation)

                    if self.make_display:
                        self.display_size(image)

                    image.write(img_num_str + '.jpg')
                    self.image_num_strs.append(img_num_str)

                    self.add_comment(img_num_str, img_num_str + '.jpg')

                    # Increment after
                    self.current_image_number += 1

        if self.make_html:
            self.generate_html()
def __magick_load_wrapper__(fseq,width,height,dtype_dest,dtype_src,monochrome):
    import PythonMagick
    if monochrome: A = np.zeros((height,width,len(fseq)),dtype=dtype_dest) # MONO
    else: A = np.zeros((height,width,3,len(fseq)),dtype=dtype_dest)        # RGB
    i=0
    
    for fn in fseq:
        imageObj = PythonMagick.Image()
        buffer = PythonMagick.Blob()
        dtype_src_MagickBlob = dtype_src
        
        # Get pixel buffer
        try:
            imageObj.read(fn)
        except RuntimeError as e:
            # Read and report errors due to unknown TIFF fields, etc, but try and continue anyway.
            print(e)
        
        # Specify any necessary conversions to make the data readable.
        if dtype_src == 'uint12':
            imageObj.depth(16)
            dtype_src_MagickBlob=np.uint16
        
        # Write converted data into blob buffer
        imageObj.write(buffer)
        
        # Transfer buffer into to numpy array
        if type(dtype_src_MagickBlob) is type:
            try:
                #frame = np.fromstring(buffer.data, dtype_src_MagickBlob) # deprecated in python3
                frame = np.frombuffer(buffer.data, dtype=dtype_src_MagickBlob)
            except UnicodeDecodeError as e:
                # Solution to UnicodeDecodeError from http://www.imagemagick.org/discourse-server/viewtopic.php?t=34437
                frame = np.frombuffer(e.object, dtype=dtype_src_MagickBlob)
            except ValueError as e:
                # Error will be thrown if string size is not multiple of element size, i.e
                # we guessed incorrectly.
                print(errmsg)
                print("\nError! The dtype of the images was not consistent and this has caused an error.")
                exit()
        else:
            raise ValueError("Pixel format %s not currently supported!"\
                              % dtype_src_MagickBlob)

        # allocate 3rd dimension for color channels?
        if ('RGB' in str(imageObj.colorSpace())):
            extraPixels = len(frame) - height*width*3
            if extraPixels < 0: raise IndexError("ERROR: insufficient bytes for RGB data"\
                                                +" in frame: %s (%i)" % (fn,extraPixels))
            frame = frame[:-extraPixels].reshape(height,width,3)
        else:
            extraPixels = len(frame) - height*width
            if extraPixels < 0: raise IndexError("ERROR: insufficient bytes for data"\
                                                +" in frame: %s (%i)" % (fn,extraPixels))
            frame = frame[:-extraPixels].reshape(height,width)
    
        # Collapse color channel data on `monochrome' flag.
        if monochrome and ('RGB' in str(imageObj.colorSpace())):
            frame = __make_monochromatic__(frame,dtype_dest)

        del imageObj
        del buffer

        A[...,i]=frame.astype(dtype_dest)
        i+=1
    return A
Example #8
0
def grey_array(imgfile):
    i = mil.Image(imgfile)
    b = mil.Blob()
    i.write(b, 'GRAY')
    a = numpy.fromstring(b.data, 'float32').reshape((i.rows(), i.columns()))
Example #9
0
        def webView_didFinishLoadForFrame_(self, webview, frame):
            # don't care about subframes
            if frame == webview.mainFrame():
                view = frame.frameView().documentView()

                output = StringIO.StringIO()

                if HasMagick:
                    output.write(self.captureView(view).representationUsingType_properties_(
                        AppKit.NSPNGFileType, None))
                    blob = PythonMagick.Blob(output)
                    mimg = PythonMagick.Image(blob)
                    mimg.quality(QUALITY)

                    if FORMAT=="GIF" and not MK_MONOCHROME and not MK_GRAYSCALE and not MK_DITHER and MK_COLORS != 0 and not MK_COLORS <= 256:
                        mimg.quantizeColors(256)
                        mimg.quantizeDither()
                        mimg.quantize()

                    if MK_MONOCHROME:
                        mimg.quantizeColorSpace(PythonMagick.ColorspaceType.GRAYColorspace)
                        mimg.quantizeColors(2)
                        mimg.quantizeDither()
                        mimg.quantize()
                        mimg.monochrome()
                    elif MK_GRAYSCALE:
                        mimg.quantizeColorSpace(PythonMagick.ColorspaceType.GRAYColorspace)
                        if MK_COLORS > 0 and MK_COLORS < 256:
                            mimg.quantizeColors(MK_COLORS)
                        else:
                            mimg.quantizeColors(256)
                        mimg.quantizeDither()
                        mimg.quantize()
                    else:
                        if MK_COLORS > 0:
                            mimg.quantizeColors(MK_COLORS)
                            if MK_DITHER:
                                mimg.quantizeDither()
                            mimg.quantize()

                    if FORMAT=="JPG":
                        mimg.write(blob, "jpg")
                    elif FORMAT=="PNG":
                        mimg.write(blob, "png")
                    elif FORMAT=="AUTO" or FORMAT=="GIF":
                        mimg.write(blob, "gif")
                    output = StringIO.StringIO()
                    output.write(blob.data)
                else:
                    if FORMAT=="AUTO" or FORMAT=="GIF":
                        output.write(self.captureView(view).representationUsingType_properties_(
                            AppKit.NSGIFFileType, None))
                    elif FORMAT=="JPG":
                        output.write(self.captureView(view).representationUsingType_properties_(
                            AppKit.NSJPEGFileType, None))
                    elif FORMAT=="PNG":
                        output.write(self.captureView(view).representationUsingType_properties_(
                            AppKit.NSPNGFileType, None))

                RENDERS[WebkitLoad.req_img] = output

                # url of the rendered page
                web_url = frame.dataSource().initialRequest().URL().absoluteString()

                httpout = WebkitLoad.httpout

                httpout.write("<!-- Web Rendering Proxy v%s by Antoni Sawicki -->\n"
                              % (__version__))
                httpout.write("<!-- Request for [%s] frame [%s] -->\n"
                              % (WebkitLoad.req_url, web_url))

                domdocument = frame.DOMDocument()
                # Get title
                httpout.write("<HTML><HEAD>")
                httpout.write((u"<TITLE>%s</TITLE>"
                                % domdocument.title()).encode('utf-8', errors='ignore'))
                httpout.write("</HEAD>\n<BODY>\n")

                if AUTOWIDTH:
                    httpout.write("<script>document.write('<span style=\"display: none;\"><img src=\"http://width-' + document.body.clientWidth + '-px.jpg\" width=\"0\" height=\"0\"></span>');</script>\n")

                if ISMAP == True:
                    httpout.write("<A HREF=\"http://%s\">"
                                  "<IMG SRC=\"http://%s\" ALT=\"wrp-render\" ISMAP>\n"
                                  "</A>\n" % (WebkitLoad.req_map, WebkitLoad.req_img))
                    mapfile = StringIO.StringIO()
                    mapfile.write("default %s\n" % (web_url))
                else:
                    httpout.write("<IMG SRC=\"http://%s\" ALT=\"wrp-render\" USEMAP=\"#map\">\n"
                                  "<MAP NAME=\"map\">\n" % (WebkitLoad.req_img))

                domnodelist = domdocument.getElementsByTagName_('A')
                i = 0
                while  i < domnodelist.length():
                    turl = domnodelist.item_(i).valueForKey_('href')
                    #TODO: crashes? validate url? insert web_url if wrong?
                    myrect = domnodelist.item_(i).boundingBox()

                    xmin = Foundation.NSMinX(myrect)
                    ymin = Foundation.NSMinY(myrect)
                    xmax = Foundation.NSMaxX(myrect)
                    ymax = Foundation.NSMaxY(myrect)

                    if ISMAP == True:
                        mapfile.write("rect %s %i,%i %i,%i\n".decode('utf-8', errors='ignore') % (turl, xmin, ymin, xmax, ymax))
                    else:
                        httpout.write("<AREA SHAPE=\"RECT\""
                                      " COORDS=\"%i,%i,%i,%i\""
                                      " ALT=\"%s\" HREF=\"%s\">\n".decode('utf-8', errors='ignore')
                                      % (xmin, ymin, xmax, ymax, turl, turl))

                    i += 1

                if ISMAP != True:
                    httpout.write("</MAP>\n")

                httpout.write("</BODY>\n</HTML>\n")

                if ISMAP == True:
                    RENDERS[WebkitLoad.req_map] = mapfile

                # Return to Proxy thread and Loop...
                RESP.put('')
                self.getURL(webview)
Example #10
0
    def __main_qt():
        # Render the page.
        # If this method times out or loading failed, a
        # RuntimeException is thrown
        try:
            while True:
                req = REQ.get()
                WebkitRenderer.httpout = req[0]
                WebkitRenderer.req_url = req[1]
                WebkitRenderer.req_img = req[2]
                WebkitRenderer.req_map = req[3]
                if WebkitRenderer.req_url == "http://wrp.stop/" or WebkitRenderer.req_url == "http://www.wrp.stop/":
                    print ">>> Terminate Request Received"
                    QApplication.exit(0)
                    break

                # Initialize WebkitRenderer object
                renderer = WebkitRenderer()
                renderer.logger = logger
                renderer.width = WIDTH
                renderer.height = HEIGHT
                renderer.timeout = 60
                renderer.wait = WAIT
                renderer.grabWholeWindow = False

                image = renderer.render(WebkitRenderer.req_url)
                qBuffer = QBuffer()

                if HasMagick:
                    image.save(qBuffer, 'png', QUALITY)
                    blob = PythonMagick.Blob(qBuffer.buffer().data())
                    mimg = PythonMagick.Image(blob)
                    mimg.quality(QUALITY)

                    if FORMAT=="GIF" and not MK_MONOCHROME and not MK_GRAYSCALE and not MK_DITHER and MK_COLORS != 0 and not MK_COLORS <= 256:
                        mimg.quantizeColors(256)
                        mimg.quantizeDither()
                        mimg.quantize()

                    if MK_MONOCHROME:
                        mimg.quantizeColorSpace(PythonMagick.ColorspaceType.GRAYColorspace)
                        mimg.quantizeColors(2)
                        mimg.quantizeDither()
                        mimg.quantize()
                        mimg.monochrome()
                    elif MK_GRAYSCALE:
                        mimg.quantizeColorSpace(PythonMagick.ColorspaceType.GRAYColorspace)
                        if MK_COLORS > 0 and MK_COLORS < 256:
                            mimg.quantizeColors(MK_COLORS)
                        else:
                            mimg.quantizeColors(256)
                        mimg.quantizeDither()
                        mimg.quantize()
                    else:
                        if MK_COLORS > 0:
                            mimg.quantizeColors(MK_COLORS)
                            if MK_DITHER:
                                mimg.quantizeDither()
                            mimg.quantize()

                    if FORMAT=="AUTO" or FORMAT=="JPG":
                        mimg.write(blob, "jpg")
                    elif FORMAT=="PNG":
                        mimg.write(blob, "png")
                    elif FORMAT=="GIF":
                        mimg.write(blob, "gif")
                    output = StringIO.StringIO()
                    output.write(blob.data)
                else:
                    if FORMAT=="AUTO" or FORMAT=="JPG":
                        image.save(qBuffer, 'jpg', QUALITY)
                    elif FORMAT=="PNG":
                        image.save(qBuffer, 'png', QUALITY)

                    output = StringIO.StringIO()
                    output.write(qBuffer.buffer().data())

                RENDERS[req[2]] = output

                del renderer
                print ">>> done: %s [%d kb]..." % (WebkitRenderer.req_img, output.len/1024)

                RESP.put('')

            QApplication.exit(0)
        except RuntimeError, e:
            logger.error("main: %s" % e)
            print >> sys.stderr, e
            QApplication.exit(1)