I want to generate an image with stablediffusion (this works on my kindle) and load and show it with fbink (both works with other Pictures). But the generated picture doesent work, i guess because stablediff. give webp-format back. Its hard to install anything on the kindle (webp-lib ...). Any suggestions? Thanks!
UserWarning: image file could not be identified because WEBP support not installed
The Error:UnidentifiedImageError(
PIL.UnidentifiedImageError: cannot identify image file
for this command: Image.open
Here the code:
Code:
import requests
import os
import base64
import time
from PIL import Image
horde_api_key = "..."
class RequestData(object):
def __init__(self):
self.client_agent = "cli_request.py:1.1.0:(discord)db0#1625"
self.api_key = horde_api_key
self.filename = "test2.webp"
self.imgen_params = {
"n": 1,
"width": 384,
"height":512,
"steps": 50,
"sampler_name": "k_euler",
"cfg_scale": 7.5,
"denoising_strength": 0.6,
}
self.submit_dict = {
"prompt":"Forrest",
"api_key": horde_api_key,
"nsfw": False,
"censor_nsfw": False,
"trusted_workers": False,
"models": ["stable_diffusion"],
"r2": True
}
self.source_image = None
self.source_processing = "img2img"
self.source_mask = None
def get_submit_dict(self):
submit_dict = self.submit_dict.copy()
submit_dict["params"] = self.imgen_params
submit_dict["source_processing"] = self.source_processing
return submit_dict
def generate():
request_data = RequestData()
headers = {
"apikey": request_data.api_key,
"Client-Agent": request_data.client_agent,
}
submit_req = requests.post(f'https://stablehorde.net/api/v2/generate/async', json=request_data.get_submit_dict(), headers=headers)
if submit_req.ok:
submit_results = submit_req.json()
req_id = submit_results['id']
is_done = False
while not is_done:
chk_req = requests.get(f'https://stablehorde.net/api/v2/generate/check/{req_id}')
if not chk_req.ok:
print(chk_req.text)
return
chk_results = chk_req.json()
is_done = chk_results['done']
time.sleep(0.8)
retrieve_req = requests.get(f'https://stablehorde.net/api/v2/generate/status/{req_id}')
if not retrieve_req.ok:
print(retrieve_req.text)
return
results_json = retrieve_req.json()
if results_json['faulted']:
print("Something went wrong when generating the request.")
return
results = results_json['generations']
for iter in range(len(results)):
final_filename = request_data.filename
if len(results) > 1:
final_filename = f"{iter}_{request_data.filename}"
img_data = requests.get(results[iter]["img"]).content
with open(final_filename, 'wb') as f:
f.write(img_data)
# Ensure the file is fully written and closed before opening as an image
with open(final_filename, 'rb') as f:
imgg = Image.open(f)
# test writing with other format
imgg.save('test2a.png')
return final_filename
else:
print(submit_req.text)
def main():
image_file = generate()
if image_file:
print(f"Image saved as {image_file}")
else:
print("Failed to generate image.")
if __name__ == "__main__":
main()