Not showing image return from server code

App link:
https://anvil.works/build#clone:QRHWQ4RJQ5BPTFXX=2KEQGLML7WA42BQMSONMTLUR

iam returning image blob object from python code but image is not displaying in app and i tried to print manually in server its working but in app component it was showing NONE

1 Like

Welcome to Anvil!

I assume you are using the Uplink service, as there is no server side code in the clone.

Can you maybe share the uplink code in the forum? Without that I can’t really test it.

edit - if I create a server function that fetches an image when pressing the “GET” button, it works :

@anvil.server.callable
def classify_image():
  resp = anvil.http.request("https://telemix.co.uk/uploads/8/1/6/7/81673020/telemix-medium_orig.jpg")
  return resp

so if that’s not what you are doing we’ll need more info.

@anvil.server.callable
def classify_image():
global landsat_pre
global landsat_extent
sns.set_style(‘white’)
sns.set(font_scale=1.5)
data = et.data.get_data(‘cold-springs-fire’)
os.chdir(os.path.join(et.io.HOME, ‘earth-analytics’))
ls_pre_path = os.path.join(“data”, “cold-springs-fire”, “landsat_collect”,
“LC080340322016070701T1-SC20180214145604”, “crop”,
band.tif”)
landsat_paths_pre = glob(ls_pre_path)
landsat_paths_pre.sort()
landsat_pre_st_path = os.path.join(“data”, “cold-springs-fire”,“outputs”, “landsat_pre_st.tif”)
es.stack(landsat_paths_pre, landsat_pre_st_path)
with rio.open(landsat_pre_st_path) as landsat_pre_src:
landsat_pre = landsat_pre_src.read(masked=True)
landsat_extent = plotting_extent(landsat_pre_src)
#print(landsat_extent, type(landsat_extent))

ep.plot_rgb(landsat_pre,
        rgb=[3, 2, 1],
        extent=landsat_extent,
        title="Landsat True Color Composite Image | 30 meters \n")

return anvil.mpl_util.plot_image()

this is my server side code

app code
def button_1_click(self, **event_args):
“”“This method is called when the button is clicked”""
result= anvil.server.call(‘classify_image’)
print(result)
#self.label_7=Label(text=“true color image is rendered using different bands”)
self.image_1.source=result

Hi @tejaprathap999,

It’s hard to read unformatted code like that! You can put it between triple back-ticks (```) like this to make it readable. For example:

    ```python
    def foo(bar):
        pass
    ```

produces this nicely formatted result:

def foo(bar):
    pass

If you edit your comment to format your code correctly, it will be much easier for us to help you!

3 Likes
@anvil.server.callable
def classify_image():
    global landsat_pre
    global landsat_extent
    sns.set_style('white')
    sns.set(font_scale=1.5)
    data = et.data.get_data('cold-springs-fire')
    os.chdir(os.path.join(et.io.HOME, 'earth-analytics'))
    ls_pre_path = os.path.join("data", "cold-springs-fire", "landsat_collect", 
                                      "LC080340322016070701T1-SC20180214145604", "crop", 
                                      "*band*.tif")
    landsat_paths_pre = glob(ls_pre_path)
    landsat_paths_pre.sort()
    landsat_pre_st_path = os.path.join("data", "cold-springs-fire","outputs", "landsat_pre_st.tif")
    es.stack(landsat_paths_pre, landsat_pre_st_path)
    with rio.open(landsat_pre_st_path) as landsat_pre_src:
        landsat_pre = landsat_pre_src.read(masked=True)
        landsat_extent = plotting_extent(landsat_pre_src)
    #print(landsat_extent, type(landsat_extent))
    
  
    ep.plot_rgb(landsat_pre,
            rgb=[3, 2, 1],
            extent=landsat_extent,
            title="Landsat True Color Composite Image | 30 meters \n")

    return anvil.mpl_util.plot_image()

in button1 , image1 is not rendering on screen when button is clicked

https://anvil.works/build#clone:QRHWQ4RJQ5BPTFXX=2KEQGLML7WA42BQMSONMTLUR

help me with this please…

or else suggest some ideas to display .those are satellite images it takes alot of time for processing .so killed my idea of storing in data tables processing in app code.so please suggest some idea to display .
thank you in advance for your valuable time and help .

Can you produce a simple, stand alone server function that also recreates the issue. One that someone can copy & paste easily?

I’m afraid i don’t understand any of that, sorry. There might be others on here that do.

2 Likes

Hello @tejaprathap999 and welcome to this forum.
I don’t know anything about the code you posted and I see an empty Server Module in your example, but I tried to show how an image taken server-side can be rendered in the client.
So I re-wrote your server module this way:

@anvil.server.callable
def classify_image():
  
  image_base_64 = '.....big base64 string representing my image....'
  image_binary = base64.b64decode(image_base_64)
  my_media = anvil.BlobMedia(content_type="img/png", content=image_binary, name="acme.png")
  return my_media

The key point in my view is that you have to create a BlobMedia object, of appropriate type, and return this to the client.

Then in the client things are as simple as

    self.image_1.source=anvil.server.call('classify_image')

I love how Anvil makes things just simple.

Hope this helps.

https://anvil.works/build#clone:XGDEWXDBFPWCOLTR=N6QOYLLKJH3JHSH52BIDJDAG