Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
1 ตอบกลับ
10212 มุมมอง

Hi,

I want to display image in my custom module from url. I have an api and I  fetched the image url such as thumbnail from it. But while doing like given below code , its not worked.  

Thanks in advance

class img(models.Model):
_name = 'img.books'

photo = fields.Binary(string='Image')

def search_button(self):
url = "https://www.googleapis.com/books/v1/volumes?q=" + isbn_no;
response = requests.get(url)
json_data = response.json()
loaded_json = json.dumps(json_data)
book = json_data['items'][0]
image_url = book["volumeInfo"]['imageLinks']['smallThumbnail'] // here contains image url such as thumbnail
            
img_obj =Image.open(BytesIO(image_url.content))
buffer = BytesIO()
img_obj.save(buffer, format="JPEG")
img = base64.b64encode(buffer.getvalue().strip())
self.photo = img


อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

Hi,

Try to rewrite your code like below

import base64
import requests

class img(models.Model):
_name = 'img.books'

photo = fields.Binary(string='Image')

def search_button(self):
url = "https://www.googleapis.com/books/v1/volumes?q=" + isbn_no;
response = requests.get(url)
json_data = response.json()
loaded_json = json.dumps(json_data)
book = json_data['items'][0]
image_url = book["volumeInfo"]['imageLinks']['smallThumbnail'] // here contains image url such as thumbnail
img = base64.b64encode(requests.get(image_url).content)
            self.photo = img

Regards

อวตาร
ละทิ้ง
ผู้เขียน

Thank you Cybrosys Techno Solutions.....

Its working

Related Posts ตอบกลับ มุมมอง กิจกรรม
2
ก.ค. 22
12065
0
ก.พ. 21
1908
5
พ.ค. 25
33904
0
พ.ย. 23
1917
0
ต.ค. 23
1542