Advertisement
incomestreamsurfer

Image Upscaler

Jul 13th, 2023
1,132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. import os
  2. import glob
  3. import requests
  4.  
  5. def upscale_image(input_image_path, output_image_path, engine_id, api_key, api_host="https://api.stability.ai", width=None, height=None):
  6. # Open the input image file
  7. with open(input_image_path, "rb") as file:
  8. image_data = file.read()
  9.  
  10. # Set up request headers and parameters
  11. headers = {
  12. "Accept": "image/png",
  13. "Authorization": f"Bearer {api_key}",
  14. }
  15.  
  16. files = {
  17. "image": image_data,
  18. }
  19.  
  20. data = {}
  21. if width:
  22. data["width"] = width
  23. if height:
  24. data["height"] = height
  25.  
  26. # Send POST request to the API
  27. response = requests.post(
  28. f"{api_host}/v1/generation/{engine_id}/image-to-image/upscale",
  29. headers=headers,
  30. files=files,
  31. data=data
  32. )
  33.  
  34. # Check the response status code
  35. if response.status_code != 200:
  36. raise Exception(f"Non-200 response: {response.text}")
  37.  
  38. # Write the response content (upscaled image data) to output file
  39. with open(output_image_path, "wb") as f:
  40. f.write(response.content)
  41.  
  42. print(f"Upscaled image is saved at: {output_image_path}")
  43.  
  44.  
  45. # Find all PNG files in the current directory
  46. image_files = glob.glob('*.png')
  47.  
  48. # Upscale each image file
  49. for image_file in image_files:
  50. output_file = f"upscaled_{image_file}"
  51. upscale_image(image_file, output_file, "esrgan-v1-x2plus", "YOUR_API_KEY", width=1024)
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement