Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. PySide: showing an base64 image on a QMainWindow [Resolved]
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Language Bindings
13 Posts 4 Posters 13.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kamalakshantv
    wrote on last edited by
    #4

    [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

    1 Reply Last reply
    0
    • G Offline
      G Offline
      grizmio
      wrote on last edited by
      #5

      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]

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #6

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

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0
        • G Offline
          G Offline
          grizmio
          wrote on last edited by
          #7

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

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #8

            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.

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • G Offline
              G Offline
              grizmio
              wrote on last edited by
              #9

              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]

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #10

                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.

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  grizmio
                  wrote on last edited by
                  #11

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

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

                  EOF

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goetz
                    wrote on last edited by
                    #12

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

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      grizmio
                      wrote on last edited by
                      #13

                      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
                      @

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved