Beispiel #1
0
    def update_preview(self):
        if self.pages is not None:
            pdf_file = self.pages[self.current_page - 1]
            pdf_file.seek(0)
            document = Document(stream=pdf_file, filetype="PDF")
            image = QPixmap()

            page = document.loadPage(0)

            container_size = self.preview_box.parent().size()

            normalized_zoom_factor = min(
                page.rect.height / container_size.height(),
                page.rect.width / container_size.width(),
            )
            scale_mat = Matrix(normalized_zoom_factor, normalized_zoom_factor)

            image.loadFromData(page.getPixmap(matrix=scale_mat).getPNGData())
            image_size = container_size * self.zoom_factor
            self.preview_box.setPixmap(
                image.scaled(image_size, Qt.KeepAspectRatio))
            self.preview_box.resize(image_size)
        else:
            self.preview_box.setTextFormat(Qt.RichText)
            self.preview_box.setStyleSheet("QLabel { color : darkgray; }")
            self.preview_box.setText(
                f'<center><img src="{appctxt.get_resource("logo.png")}") /><br />Wähle auf der rechten Seite<br />eine Datei und ein Papierformat<br />aus um die Vorschau anzuzeigen</center>'
            )

            self.preview_box.resize(QSize(200, 200))
Beispiel #2
0
 def proper_display(window: FullscreenWindow):
     mat = Matrix(window.staticZoom, window.staticZoom)
     page_pix = return_page(window.file, window.currentPage).getPixmap(
         matrix=mat, alpha=False)
     fmt = QImage.Format_RGBA8888 if page_pix.alpha else QImage.Format_RGB888
     qt_img = QImage(page_pix.samples, page_pix.width, page_pix.height, page_pix.stride, fmt)
     if window.invertImage:
         qt_img.invertPixels(QImage.InvertRgba)
     pixmap = QPixmap(QPixmap.fromImage(qt_img))
     window.mainLabel.setPixmap(pixmap)
Beispiel #3
0
 def zoom_out(self, window: FullscreenWindow):
     if self.currentMatrixZoom > self.minMatrixZoom:
         self.currentMatrixZoom -= 0.1
         page_pix = return_page(window.file, window.currentPage).getPixmap(
             matrix=Matrix(self.currentMatrixZoom, self.currentMatrixZoom))
         fmt = QImage.Format_RGBA8888 if page_pix.alpha else QImage.Format_RGB888
         qt_img = QImage(page_pix.samples, page_pix.width, page_pix.height, page_pix.stride, fmt)
         if window.invertImage:
             qt_img.invertPixels(QImage.InvertRgba)
         pixmap = QPixmap(QPixmap.fromImage(qt_img))
         window.mainLabel.setPixmap(pixmap)
         window.staticZoom = self.currentMatrixZoom
 def proper_display(window: ReaderWindowInteractivity):
     mat = Matrix(window.static_zoom, window.static_zoom)
     page_pix = return_page(window.file,
                            window.current_page).getPixmap(matrix=mat,
                                                           alpha=False)
     fmt = QImage.Format_RGBA8888 if page_pix.alpha else QImage.Format_RGB888
     qt_img = QImage(page_pix.samples, page_pix.width, page_pix.height,
                     page_pix.stride, fmt)
     if window.invert_image:
         qt_img.invertPixels(QImage.InvertRgba)
     pixmap = QPixmap(QPixmap.fromImage(qt_img))
     window.content.setPixmap(pixmap)
 def zoom_out(self, window: ReaderWindowInteractivity):
     if self.currentMatrixZoom > self.minMatrixZoom:
         self.currentMatrixZoom -= 0.25
         page_pix = return_page(window.file, window.current_page).getPixmap(
             matrix=Matrix(self.currentMatrixZoom, self.currentMatrixZoom))
         fmt = QImage.Format_RGBA8888 if page_pix.alpha else QImage.Format_RGB888
         qt_img = QImage(page_pix.samples, page_pix.width, page_pix.height,
                         page_pix.stride, fmt)
         if window.invert_image:
             qt_img.invertPixels(QImage.InvertRgba)
         pixmap = QPixmap(QPixmap.fromImage(qt_img))
         window.content.setPixmap(pixmap)
         window.static_zoom = self.currentMatrixZoom
Beispiel #6
0
def Page_Rect_get_Text_odf(doc, name, page_num, rects, hierarchy, output,
                           style_p):
    page = doc[page_num]
    words = page.getText("words")
    output.text.addElement(P(stylename=style_p, text="_" * 30))
    output.text.addElement(P(stylename=style_p, text=f"page {page_num+1}"))
    for i in range(rects.shape[0]):
        if hierarchy[i, 3] == -1:
            output.text.addElement(P(stylename=style_p, text=""))
            rect = Rect(rects[i, 0], rects[i, 1], rects[i, 2], rects[i, 3])
            mywords = [w for w in words if Rect(w[:4]) in rect]
            mywords.sort(key=itemgetter(3,
                                        0))  # sort by y1, x0 of the word rect
            group = groupby(mywords, key=itemgetter(3))
            out_text = P(stylename=style_p, text="")
            for y1, gwords in group:
                out_text.addText(" ".join(w[4]
                                          for w in gwords).replace("\n", " "))
                out_text.addText(" ")
            output.text.addElement(out_text)
        if hierarchy[i, 3] != -1:
            output.text.addElement(P(stylename=style_p, text=""))
            out_img = P()
            #ncc=int(hierarchy[i,3])
            ncc = i
            clip = Rect(rects[ncc, 0], rects[ncc, 1], rects[ncc, 2], rects[ncc,
                                                                           3])
            pix = page.getPixmap(matrix=Matrix(2, 2), clip=clip)
            name_image = f"Pictures/image-{page.number}-{i}.png"
            #liste_tampon.append(name_tampon)
            #pix.writePNG(name_image)
            pix_png = pix.getPNGData()
            h = pix.height / pix.xres
            w = pix.width / pix.yres
            frame = Frame(width=f"{w}in",
                          height=f"{h}in",
                          anchortype="paragraph")
            href = output.addPicture(name_image,
                                     mediatype="png",
                                     content=pix_png)  #
            frame.addElement(Image(href=f"./{href}"))
            out_img.addElement(frame)
            output.text.addElement(out_img)
    return output
Beispiel #7
0
def Page_Rect_get_Text_odf(doc, page_num, rects, hierarchy, Xrects, output,
                           style_p, style_i, img_quality, col):
    page = doc[page_num]
    words = page.getText("words")
    output.text.addElement(P(stylename=style_p, text="_" * 60))
    if col == 1 or col == 2:
        output.text.addElement(
            P(stylename=style_p, text=f"page {page_num+1} - column {col}"))
    else:
        output.text.addElement(P(stylename=style_p, text=f"page {page_num+1}"))
    for i in range(rects.shape[0]):
        if hierarchy[i, 3] == -1:
            rect = Rect(rects[i, 0], rects[i, 1], rects[i, 2], rects[i, 3])
            allwords = [w for w in words if Rect(w[:4]) in rect]
            # iterate over all rects to exclude
            mywords = []
            for w in allwords:
                exclude = 0
                for Xrect in Xrects:
                    xg = (w[0] + w[2]) / 2
                    yg = (w[1] + w[3]) / 2
                    if Rect(Xrect).contains((xg, yg)):
                        exclude = 1
                if exclude == 0:
                    mywords.append(w)

            mywords.sort(key=itemgetter(3,
                                        0))  # sort by y1, x0 of the word rect
            group = groupby(mywords, key=itemgetter(3))

            output.text.addElement(P(stylename=style_p, text=""))
            out_text = P(stylename=style_p, text="")
            for y1, gwords in group:
                out_text.addText(" ".join(w[4]
                                          for w in gwords).replace("\n", " "))
                out_text.addText(" ")
            output.text.addElement(out_text)
        if hierarchy[i, 3] != -1:

            clip = Rect(rects[i, 0], rects[i, 1], rects[i, 2], rects[i, 3])
            #taking into account quality
            img_qual = img_quality / 50.

            pix = page.getPixmap(matrix=Matrix(img_qual, img_qual), clip=clip)

            name_image = f"Pictures/image-{page.number}-{col}{i}.png"
            pix_png = pix.getPNGData()
            h = pix.height / pix.xres
            w = pix.width / pix.yres
            #if quality is larger than 2 keep the frame the same as if it was 2
            h *= 2 / img_qual
            w *= 2 / img_qual
            #if image is too small (h<20px) it is probably an artifact
            #so do not print it
            if pix.height * 2 / img_qual > 20:
                output.text.addElement(P(stylename=style_p, text=""))
                out_img = P()
                frame = Frame(stylename=style_i,
                              width=f"{w}in",
                              height=f"{h}in",
                              anchortype="paragraph")
                href = output.addPicture(name_image,
                                         mediatype="png",
                                         content=pix_png)  #
                frame.addElement(Image(href=f"./{href}"))
                out_img.addElement(frame)
                output.text.addElement(out_img)
    return output