Skip to main content

Ralsina.Me — Roberto Alsina's website

Posts about programming (old posts, page 64)

Slow-Slow and Fast-Fast (video)

My pre­vi­ous post ex­plained how to cache whole web pages as im­ages. Now see it in ac­tion. This is a light­weight RSS read­er, op­ti­mized for com­ic books (but it works for any feed) and for off­line use (but it works on­line too, of course).

Not ready for pub­lic use yet, but if you look around you can find the code some­where ;-)

Capturing a webpage as an image using Pyhon and Qt

For a small project I am do­ing I want­ed the ca­pa­bil­i­ty to see web pages off­line. So, I start­ed think­ing of a way to do that, and all so­lu­tions were an­noy­ing or im­prac­ti­cal.

So, I googled and found Cu­ty­Capt which us­es Qt and We­bKit to turn web pages in­to im­ages. Good enough for me!

Since I want­ed to use this from a PyQt ap­p, it makes sense to do the same thing Cu­ty­Capt does, but as a python mod­ule/scrip­t, so here's a quick im­ple­men­ta­tion that works for me, even if it lacks a bunch of Cu­ty­Cap­t's fea­tures.

With a lit­tle ex­tra ef­fort, it can even save as PDF or SVG, which would let you use it al­most like a re­al web page.

You just use it like this:

python  capty.py http://www.kde.org kde.png

And here's the code [down­load cap­ty.py]

# -*- coding: utf-8 -*-

"""This tries to do more or less the same thing as CutyCapt, but as a
python module.

This is a derived work from CutyCapt: http://cutycapt.sourceforge.net/

////////////////////////////////////////////////////////////////////
//
// CutyCapt - A Qt WebKit Web Page Rendering Capture Utility
//
// Copyright (C) 2003-2010 Bjoern Hoehrmann <bjoern@hoehrmann.de>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// $Id$
//
////////////////////////////////////////////////////////////////////

"""

import sys
from PyQt4 import QtCore, QtGui, QtWebKit


class Capturer(object):
    """A class to capture webpages as images"""

    def __init__(self, url, filename):
        self.url = url
        self.filename = filename
        self.saw_initial_layout = False
        self.saw_document_complete = False

    def loadFinishedSlot(self):
        self.saw_document_complete = True
        if self.saw_initial_layout and self.saw_document_complete:
            self.doCapture()

    def initialLayoutSlot(self):
        self.saw_initial_layout = True
        if self.saw_initial_layout and self.saw_document_complete:
            self.doCapture()

    def capture(self):
        """Captures url as an image to the file specified"""
        self.wb = QtWebKit.QWebPage()
        self.wb.mainFrame().setScrollBarPolicy(
            QtCore.Qt.Horizontal, QtCore.Qt.ScrollBarAlwaysOff)
        self.wb.mainFrame().setScrollBarPolicy(
            QtCore.Qt.Vertical, QtCore.Qt.ScrollBarAlwaysOff)

        self.wb.loadFinished.connect(self.loadFinishedSlot)
        self.wb.mainFrame().initialLayoutCompleted.connect(
            self.initialLayoutSlot)

        self.wb.mainFrame().load(QtCore.QUrl(self.url))

    def doCapture(self):
        self.wb.setViewportSize(self.wb.mainFrame().contentsSize())
        img = QtGui.QImage(self.wb.viewportSize(), QtGui.QImage.Format_ARGB32)
        painter = QtGui.QPainter(img)
        self.wb.mainFrame().render(painter)
        painter.end()
        img.save(self.filename)
        QtCore.QCoreApplication.instance().quit()

if __name__ == "__main__":
    """Run a simple capture"""
    app = QtGui.QApplication(sys.argv)
    c = Capturer(sys.argv[1], sys.argv[2])
    c.capture()
    app.exec_()

Learn python! For free! With me! (in part)

I'm one of the speak­ers for the free python cour­ses in FM La Tribu, in Buenos Aires.

Ev­ery sat­ur­day you can learn some­thing from one of the best python pro­gram­mers in a thou­sand miles, or from me.

I'll be teach­ing about vir­tualen­v, build­out, nose and oth­er things on Au­gust 21, about GUI stuff on Sep­tem­ber 25 and Oc­to­ber 2, and about PyQt in Oc­to­ber 30.

This is all for free, and I hope lots of peo­ple show up!

The full sched­ule is here.

Free software is good for me.

I make a liv­ing work­ing with free soft­ware (BTW, if you need sysad­mins that know what they do, con­tact us: http://www.net­man­ager­s.­com.ar)

But that's not that big a deal, I could make a liv­ing do­ing some­thing else. I am sure I would be com­pe­tent at some­thing else, even if I have no idea what that would be right now.

It does, how­ev­er give me free­dom to play, which is much more im­por­tan­t. There­fore, this post is sort of a sta­tus up­date on things I play with. Not games, those are not re­al­ly my thing, but things that I do for fun.

Yes, some of these may mean I am a very strange per­son.

I'll lim­it my­self to the last cou­ple of weeks or so.

There's been sort of a bump in in­ter­est in Mar­ave, my dis­trac­tion free ed­i­tor and it's be­cause it has been re­viewed in Lin­ux Jour­nal!

I have read the ar­ti­cle (sad­ly I can't link to it) and it was a su­per pos­i­tive re­view, here are some choice quotes:

"mar­ave makes the dull world of text edit­ing ro­man­tic and im­mer­sive with beau­ti­ful min­i­mal­is­m"

"... it does­n't just have min­i­mal­ism and sim­plic­i­ty, it has min­i­mal­ism and sim­plic­i­ty com­bined with beau­ty and a pal­pa­ble de­sign eth­ic. mar­ave has soul, and I love that."

So thanks for the kind words to the au­thor, and some­thing I no­ticed: you ran in­to a big bug in mar­ave and did­n't no­tice :-)

The "crick­et bat" icon (it's a screw­driv­er ;-) should show you the con­fig di­a­log. How­ev­er, it seems in Ubun­tu (and maybe in oth­er dis­tros, I don't know) the con­fig is not vis­i­ble,and all you see is the text move around a bit. This is what he should have seen:

marave15

I have nev­er been able to re­pro­duce it, but I am go­ing to in­stall a Ubun­tu VM just for this, so maybe soon.

On re­lat­ed news, mar­ave was al­so re­viewed in a ger­man mag­a­zine a cou­ple of months ago, and I have not been able to get a copy of the ar­ti­cle. (BTW, is­n't it rea­son­able to send a copy of these to the au­thor of the pro­gram you are re­view­ing? Nei­ther mag­a­zine even men­tioned it to me!)

In any case, if any­one has this mag­a­zine and can tell me what the ar­ti­cle about dis­trac­tion-free ed­i­tors say, you will make my day:

Here's the ar­ti­cle teas­er

And this seems to be the mag­a­zine is­sue:

https://shop.linuxnewmedia.de/store/eh01/EH10165/de/product_image/variant/bounding_product

On new projects (yes, I al­ways have new pro­ject­s), I ran in­to this awe­some blog post by Roger Als­ing about ap­proach­ing Mona Lisa with just 50 poly­gons <http://roger­als­ing.­com/2008/12/07/­ge­net­ic-pro­gram­ming-evo­lu­tion-of-­mon­a-lisa/ > and be­ing a nerd and hav­ing awe­some pro­gram­ming tools at my com­mand... I wrote a frame­work to test that kind of al­go­rithm­s.

I called it evo­lu­to and it's at http://evo­lu­to.­google­code.­com.

I on­ly did a very sim­ple al­go­rith­m, based on translus­cent tri­an­gles, but it did work... for some def­i­ni­tion of work!

You can even see the lo­cal min­i­mum that does­n't let her right eye form right :-)

Evo­lu­to has a li­brary of al­go­rithms (cur­rent­ly emp­ty) and you can ed­it and reap­ply on-the-fly the one you wan­t, and see the gen­er­a­tions change on-screen.

It would take some work to make it a pol­ished pro­gram, but it does work.

I folled around a bit with cre­at­ing a nice PDF pre­sen­ta­tion play­er but it's still very ear­ly:

109301062

In what's per­haps my most es­tab­lished pro­jec­t, rst2pdf. I have fixed a bunch of bugs, and a re­lease is a bit over­due:

Issue 186: Text not wrapping around images when specified

I fixed this by adding a new CLI op­tion, so be­hav­iour was not changed, but now you can have im­ages with text flow­ing be­side it. It will not look great but it work­s.

Issue 307: Replace directive doesn't replace text in header/footer in some cases

This was an in­ter­est­ing prob­lem! It was very en­ter­tain­ing.

Made it work with Sphinx 1.*

There is a piece bro­ken stil­l, but what I fixed was not ter­ri­bly hard.

Unbroken bookrest

I need to work much more with this, but at least what worked be­fore works again. If you don't know what bookrest is, it's a rst2pdf graph­i­cal fron­tend / word pro­ces­sor, here's a taste:

I al­so made sev­er­al re­leas­es for my AUR pack­ages

Plus I worked, and some oth­er stuff. All in al­l, not a bad stretch.


Contents © 2000-2020 Roberto Alsina