PySide: showing an base64 image on a QMainWindow [Resolved]



  • Hi,
    I'm looking for some guide because I'm really lost with pyside.

    I want to show an image, which I have it in base64, is like 150x150px and I get it from MySQL server , in a QMainWindow.
    but I don't know how nor where should I start looking. :(

    Some googleing says that i should use:
    QWebSettings.setUserStyleSheetUrl(location)
    but, for that i would need a web server, and it seems ugly, I think it must be a cleaner and easier way.

    Does anybody knows what type of widget from the QT designer widgets box i should use?

    I'm currently using pyside with Ubuntu 10.10, designer-qt4 to do the design widgets, and python 2.6.6
    Any info, suggestion, idea, anything will be appreciate.

    Grizmio.



  • I do not know how it works in python but in C++ I would just read it to a QByteArray, decode to raw data, load from it (with loadFromData) to QPixmap and show it on a QLabel.



  • Actually I have checked the PySide documentation and it seems all required tools is there.



  • [quote author="VCsala" date="1293047083"]I do not know how it works in python but in C++ I would just read it to a QByteArray, decode to raw data, load from it (with loadFromData) to QPixmap and show it on a QLabel.[/quote]

    Yes, you are right as mentioned in "this":http://www.qtcentre.org/threads/25186-Encode-decode-base-64-images thread



  • thanks for the guide

    I read http://www.qtcentre.org/threads/25186-Encode-decode-base-64-images
    and I tried to mod the following example/code to work in pyside:

    @
    QByteArray by = QByteArray::fromBase64("iV...........AABAgQIECBAgAABAgQIECBwrQLfmeUk6E23pFsAAAAASUVORK5CYII=");
    QImage image = QImage::fromData(by, "PNG");
    QLabel label;
    label.setPixmap(QPixmap::fromImage(image));
    label.show();
    @

    First, I created the call.base64 using:
    @
    <?php
    $imgfile = "call.png";
    $handle = fopen($imgfile, "r");
    $imgbinary = fread(fopen($imgfile, "r"), filesize($imgfile));
    $fw = fopen('call.base64', 'w');
    print_r(base64_encode($imgbinary));
    fwrite($fw, base64_encode($imgbinary));
    fclose($fw);
    //echo '<img />';
    ?>
    @

    then using pyside:
    @
    imagen64 = open('call.base64','r').read()
    imagen = QtGui.QImage()
    bytearr = QtCore.QByteArray(imagen64)
    print "devuelta:", imagen.loadFromData( bytearr, 'PNG' )
    self.label.setPixmap( QtGui.QPixmap.fromImage(imagen) )
    print self.label.pixmap()
    @

    but, doesn't this display anything.
    if I set a text first in the label, it disappear when setting the pixmap

    when the next gets executed:
    @print "devuelta:", imagen.loadFromData( bytearr, 'PNG' )@
    it returns "devuelta: False"

    even if I use:
    @
    bytearr = QtCore.QByteArray("iV...........AABAgQIECBAgAABAgQIECBwrQLfmeUk6E23pFsAAAAASUVORK5CYII=")
    @

    doesn't work.
    thanks for your time and help

    [EDIT: added @-tags for code formatting, Volker]



  • grizmio, please add @-tags around your code or use the icon in the editor. It makes your code much more easier to read.



  • Thanks Volker, it looks really nice now, lesson learned :)



  • Just checked the code now: In your pyside code, you do not decode the base64 encoded data. Instead you feed the base64 characters into the image. I'm not familiar with PySide, but there should be a fromBase64 method too.



  • yes, you are righ there is a fromBase64 method, but even if I change it, still don't work.
    maybe I'm using a bad encoded image.
    Now I'm using:
    @
    bytearr.fromBase64( imagen64 )
    @

    if I run it, the print bytearr.length() prints 0
    here's the actual method:
    @
    def getImage(self):
    imagen64 = open('call.base64','r').read()
    print "#############################"

        imagen = QtGui.QImage()
        bytearr = QtCore.QByteArray()
        bytearr.fromBase64(  imagen64  ) 
        pprint.pprint( bytearr.length() )
        print imagen.loadFromData(   bytearr, 'PNG'   )
        self.label.setPixmap(    QtGui.QPixmap.fromImage(imagen)   )
        print self.label.pixmap()
        self.label.show()
        # self.label.setAlignment(QtCore.Qt.AlignBottom | QtCore.Qt.AlignRight)
        print "DONE"
    

    @
    here is the photo/image, maybe someone could encode it just for check:
    !http://img543.imageshack.us/img543/5513/callq.png!
    here you could find the magic string
    "image in base64 with php":http://pastebin.com/raw.php?i=agGhq88V

    any ideas?anything will be appreciate :)

    [EDIT: fixed image tag, Volker]



  • In C++ Qt fromBase64() is a static function, i.e. it returns a the decoded data, but does not manipulate the data of an instance. Maybe there is something similar in Python, I don't know.

    Naively I would try

    @
    bytearr = QtCore.QByteArray.fromBase64( imagen64 )
    @

    but I don't know if this is valid Python syntax.



  • genius!!! that is my mistake! now it works!
    I think it's time to study about static functions.

    really really thank you!
    :):):):)

    EOF



  • You're welcome. I'm glad that it helped - I never did anything with Python, so this was just a blind guess :-)



  • a nice blind guess!

    if someone else wants to do the same, or is searching for an example:
    @
    def getImage(self):
    imagen64 = open('call.base64','r').read()
    imagen = QtGui.QImage()
    bytearr = QtCore.QByteArray.fromBase64( imagen64 )
    imagen.loadFromData( bytearr, 'PNG' )
    self.label.setPixmap( QtGui.QPixmap.fromImage(imagen) )
    @
    where:
    call.base64 is a file containing the base64 string with no CR nor LF, with a string of length:1888
    encoded using php:
    @
    $imgfile = "call.png";
    $handle = fopen($imgfile, "r");
    $imgbinary = fread(fopen($imgfile, "r"), filesize($imgfile));
    $fw = fopen('call.base64', 'w');
    fwrite($fw, base64_encode($imgbinary));
    fclose($fw);
    @
    with imports:
    @
    from PySide import QtCore, QtGui
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.