Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API rotates image #144

Open
daslicht opened this issue May 6, 2024 · 6 comments
Open

API rotates image #144

daslicht opened this issue May 6, 2024 · 6 comments
Labels
bug Something isn't working

Comments

@daslicht
Copy link

daslicht commented May 6, 2024

Hello,
is ther a way to stop teh API rotating images?

Input File:
Screenshot 2024-05-06 at 16 44 05

Processed Image:

Screenshot 2024-05-06 at 16 44 30

@xzitlou
Copy link

xzitlou commented May 6, 2024

Hello @daslicht , I will check the API and I will let you know once the problem is fixed.

@daslicht
Copy link
Author

daslicht commented May 6, 2024

Nice ! another issue is that batch processing isnt working, I replaced curl for downloading teh images since my server has outdated openssl, thre seams to go something wrong in the get_results function

<?

use Functions as F;
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ERROR | E_PARSE);

class BackgroundRemoverAPI
{

    private $headers;
    private $api_url;
    private $results_url;

//    private function download_file($url, $filename) : void
//    {
//        $curl = curl_init();
//        $url = "https://api.backgroundremoverai.com" . $url;
//        echo $url;
//        curl_setopt($curl, CURLOPT_URL, $url);
//        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//        curl_setopt($curl, CURLOPT_SSLVERSION, 3);
//        $data = curl_exec($curl);
//        $error = curl_error($curl);
//        var_dump($data );
//        curl_close($curl);
//        # Make sure destination path exists
//        $destination_path = "./processed_images/";
//        $destination_file = fopen($destination_path . $filename, "w+");
//        fwrite($destination_file, $data);
//        fclose($destination_file);
//    }

    private function download_file($url, $filename) : void
    {
        $url = "https://api.backgroundremoverai.com" . $url;
        $destination_path = "./processed_images/";

        if (file_put_contents($destination_path.$filename, file_get_contents($url)))
        {
            echo "File downloaded successfully";
        }
        else
        {
            echo "File downloading failed.";
        }

    }


    private function get_results($params)
    {

        if (isset($params->error)) {
            print_r($params->error);
            return;
        }

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $this->results_url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $this->headers);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_decode(json_encode($params), true));
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        $content = json_decode(curl_exec($curl));
        curl_close($curl);

        if ($content->finished == false) {
            if (intval($content->queue_count) > 0) {
                print_r("queue: $content->queue_count");
            }

            sleep(5);
            $results = $this->get_results($params);
            return;
        }



        foreach ($content->files as $f) {
       
            $this->download_file($f->url, $f->filename);
        }
    }

    public function convert_files(array $file_list) : void
    {

        $post_data['lang'] = 'en';
        $post_data['convert_to'] = 'image-backgroundremover';

        foreach ($file_list as $index => $file) {
            $post_data['file[' . $index . ']'] = curl_file_create(
                realpath($file),
                mime_content_type($file),
                basename($file)
            );
        }

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $this->api_url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $this->headers);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        $content = curl_exec($curl);
        curl_close($curl);

        $this->get_results( json_decode($content) );
        //return json_decode($content);
    }

    function __construct()
    {
        //$this->file_list = $files_list;//['/path/to/files/blah.jpeg'];
        $this->headers = array("Authorization: 08144e1b39cc41a190d691242edaad61");
        $this->api_url = "https://api.backgroundremoverai.com/v1/convert/";
        $this->results_url = "https://api.backgroundremoverai.com/v1/results/";
    }


}

//$resp = convert_files($file_list, $headers, $api_url);
//get_results($resp, $results_url, $headers);

@xzitlou
Copy link

xzitlou commented May 6, 2024

Let me check this issue too.

@xzitlou xzitlou added the bug Something isn't working label May 6, 2024
@cfl-chenfangliang
Copy link

cfl-chenfangliang commented May 17, 2024

I have the same problem as you, with high probability is your image exif info contains rotating info. you can try:
from PIL import Image, ImageOps
img = Image.open('path/to/image').convert('RGB')

img = ImageOps.exif_transpose(img)

@daslicht
Copy link
Author

thanks will try what is PIL?

@cfl-chenfangliang
Copy link

thanks will try what is PIL?

pythom Image library. The basic idea is to process the exif information of the image. you need to deal with it yourself if you use other language

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants