Aisthetic

부분 유료
분류별 Aisthetic | 업데이트됨 לפני חודשיים | Video, Images
인기

7.7 / 10

지연 시간

575ms

서비스 수준

100%

Health Check

N/A

모든 자습서로 돌아가기 (1)

Contrast Correct POST - Python Tutorial

Intro

Digital cameras often see color and luminance differently than the human eye. For instance our eyes are much more sensitive to changes in dark tones than brighter tones. This endpoint is designed to brighten or darken images in a non-linear way that makes them more pleasing to the human eye. Values of gamma >1 will brighten the image intelligently, and conversely a gamma < 1 will reduce the brightness similarly.

b64 Encoding An Image

First we read an image in and base 64 encode it in a URL safe manner. Traditional b64 encodings can contain “+”, “=” and “/” characters which are not safe for POST requests in general. For this reason it is important to use a url-safe encoder.

with open("path_to_my_image/image.jpg", "rb") as image_file:  
    img_str = base64.urlsafe_b64encode(image_file.read()).decode()

##Sending Our POST Request

import requests
import json 
import io
from PIL import Image

url = "https://aisthetic.p.rapidapi.com/gamma-correct/"
querystring = {"gamma":"1.3"}

headers = {
    'x-rapidapi-host': "aisthetic.p.rapidapi.com",
    'x-rapidapi-key': "your-RapidAPI-key-here",
    'content-type': "application/x-www-form-urlencoded"
    }

response = requests.request("POST", url, data=payload, headers=headers, params=querystring)

print(response.text)

Retrieving Resulting Image

The endpoint will return a URL where we can download our contrast corrected image

url = json.loads(json.loads(response.text))["body"]
r = requests.get(url)
stream = io.BytesIO(r.content)
img = Image.open(stream)
img.save("my_image.jpg")