RingK
RingK

Reputation: 83

Error getting image from url

I'm trying to show an image from a url (stored in a list) in a Image widget in kivy, this is my function:

class ImageScreen(Screen):

    image_source = ObjectProperty()

    def get_image(self):
        filename = 'imagelist.txt'
        txt = open(filename, 'r')

        with txt as file:
            images = [line.rstrip('\n') for line in txt]

        n = random.randint(0, len(images))

        self.image_source.source = str(images[n])

Here's my widget in .kv:

<ImageScreen>:
    image_source: imagesource
    on_enter: root.get_image()
    Image:
        id: imagesource
        source: 'preload.jpg'

When i call the function i get this error:

[ERROR] [Image] Error reading file http://www.webcomics.it/scottecscomics/files/2015/09/342-Gelado.jpg

I tried with AsyncImage widget instead of Image:

    AsyncImage:
        id: imagesource
        source: 'preload.jpg'

but i get this error:

Exception: Unknown <jpe> type, no loader found.
an integer is required

I can open the image in my browser copying the url from the error, and have kivy to show it if loaded locally... What am I doing wrong?

EDIT: I tried loading many image files found on the internet, the problem is with .jpg files, works fine with .png files... But I have no problems showing .jpg files saved locally... I really have no idea on how to fix this...

Upvotes: 3

Views: 1355

Answers (2)

Yoav Glazner
Yoav Glazner

Reputation: 8066

The following code worked for me in kivy 1.8.0 and 1.9.1

import kivy
import datetime

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.factory import Factory
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.scatter import Scatter
import random
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.properties import *



Builder.load_string("""
<ImageScreen>:
    image_source: imagesource
    on_enter: root.get_image()
    AsyncImage:
        id: imagesource
        source: 'preload.jpg'
""")

images = []
class ImageScreen(Screen):

    image_source = ObjectProperty()

    def get_image(self):
        filename = 'imagelist.txt'

        with  open(filename, 'r') as filetxt:
            images = [line.rstrip('\n') for line in filetxt]
        print images
        img = random.choice(images)

        self.image_source.source = img


sm = ScreenManager()
sm.add_widget(ImageScreen(name='img'))
class MyApp(App):


    def build(self):
        return sm


if __name__ == '__main__':
    MyApp().run()

imagelist.txt:

http://www.webcomics.it/scottecscomics/files/2015/09/342-Gelado.jpg

Upvotes: 0

Tshirtman
Tshirtman

Reputation: 5949

It's a bug, fixed in https://github.com/kivy/kivy/commit/9bc466dea1a007223ce983d18f250d5bb3c69841 (after 1.9.1 release), you can patch it yourself, or install the master version, next release shouldn't have this bug.

Regards.

Upvotes: 2

Related Questions