Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Saving Photos while the code is running  (Read 1540 times)

0 Members and 2 Guests are viewing this topic.

bi.danila

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Saving Photos while the code is running
« on: January 15, 2017, 10:21:57 am »
Hello there!

I'm interested in creating a program that will allow the user to add an image and have the option to edit it as in: brightness, rotate, effects (grayscale) etc.
I've done all the above and it's working perfectly with a small problem. Since the program isn't an app, everything is done from the console. If the user decides to edit only 1 thing as in only to rotate it once or only add an effect the photo will be saved but I've also added the option for the user to add more things to the photo, instead the result will only be the last selected edit.

The way the program works is the following: I've created two objects, one that will load the photo and the second one that will load "result.jpg". The first edit will be done using the first object (p1), if the user decides to add more the following edits will be done using the second object (p2). The first object saves the edit in result.jpg then p2 will load result.jpg so that the last edit will be remembered. Unfortunately it doesn't work.

Here's a part of the code:

if(v==0)
        {
            if(s==0)
            {
            p1.LoadImg();
            picture1.flipHorizontally();

            //if(!picture1.saveToFile("temp1.jpg"))
            if(!picture1.saveToFile("result.jpg"))
                {
                    std::cout<<"This window will close in 3 sec.   4";
                    Sleep(3000);
                    return -1;
                }

            std::cout<<"Doriti sa continuati prelucrarea pozei?(Da/Nu)\n";
            std::cin>>o;
            if((o[0]=='D'||o[0]=='d') && (o[1]=='A'||o[1]=='a') && o[2]==0)
            {
                s==1;
                goto B;
            }}
            else
            {
            p2.LoadImg();
            picture2.copy(p2.picture,0,0);
            picture2.flipHorizontally();

            if(!picture2.saveToFile("result.jpg"))
                {
                    std::cout<<"This window will close in 3 sec.   4";
                    Sleep(3000);
                    return -1;
                }

            std::cout<<"Doriti sa continuati prelucrarea pozei?(Da/Nu)\n";
            std::cin>>o;
            if((o[0]=='D'||o[0]=='d') && (o[1]=='A'||o[1]=='a') && o[2]==0)
            {
                goto B;
            }}
        }

Also the structures:

class img    //first object
{
    public:
    sf::Image picture;
    std::list<int> pixels={};
    int LoadImg()
        {
            if(a==1)
                {
                    if(!picture.loadFromFile("test1.jpg"))
                        return 0;
                }
        }
};

class temp  //second object
{
    public:
    sf::Image picture;
    std::list<int> pixels={};
    int LoadImg()
    {
        if(!picture.loadFromFile("result.jpg"))
            return 0;
    }
};
 

At first I thought that it might be a problem loading, while running the program, result.jpg but after using: p2.LoadImg(); while running the selected edit and still having no success I believe it might be a problem with saving the photo while the code is running. Any idea how I can force the program to save after every edit in result.jpg so I can reedit the photo after each edit the user chooses to select?

I can post the entire code but I thought that 600 lines of code having the same structure with only 1 problem of loading or saving won't help that much.

Hopefully you can save this failed project!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Saving Photos while the code is running
« Reply #1 on: January 15, 2017, 12:38:23 pm »
So you load the image within p1 and then use picture1 as image? Is that a reference?

Loading and saving jpg files multiple times isn't really recommended as you'll compress the image every save and since jpg is a lossy format, it will reduce quality each time.

Question is, why do you need to load/save the image for every change? I mean if you overwrite the original file, you don't really retain the old version. Wouldn't be a better solution to just keep working on the image in memory and/or to keep copies of it?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

bi.danila

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Saving Photos while the code is running
« Reply #2 on: January 15, 2017, 02:31:04 pm »
No, I don't use p1 as a reference.

I load the image in the img class then declare the object img p1;

I wish to use the photo loaded in img to edit it, the problem is that all the edits that I'm doing to the image won't be saved in the result photo, only the last one I choose.
For example, if I will select the following: Grayscale effect, Flip 1; after exiting the console the result will only be flipped, the first selection won't be saved.

I'm trying to figure out how to save all the edits on the same photo.

Here's the whole code:

#include<iostream>
#include<SFML/Graphics.hpp>
#include<list>
#include<windows.h>

int a;  //declar global a pentru a putea lucra cu selectarea imaginilor, alta metoda nu mi-a venit in cap deoarece nu poti pune in if un obiect imagine valabil pentru tot programul
int s=0;  //variabila globala ce decide lucrul cu poza de inceput sau cea temporara
char o[3];

class img
{
    public:
    sf::Image picture;
    std::list<int> pixels={};
    int LoadImg()
        {
            if(a==1)
                {
                    if(!picture.loadFromFile("test1.jpg"))
                        return 0;
                }

            if(a==2)
                {
                    if(!picture.loadFromFile("test2.jpg"))
                        return 0;
                }

            if(a==3)
                {
                    if(!picture.loadFromFile("test3.jpg"))
                        return 0;
                }

            if(a==4)
                {
                    if(!picture.loadFromFile("test4.jpg"))
                        return 0;
                }

            if(a==5)
                {
                    if(!picture.loadFromFile("test5.jpg"))
                        return 0;
                }

        }
};

/*class temp
{
    public:
    sf::Image picture;
    std::list<int> pixels={};
    int LoadImg()
    {
        if(!picture.loadFromFile("temp1.jpg"))
            return 0;
    }
};*/


class temp
{
    public:
    sf::Image picture;
    std::list<int> pixels={};
    int LoadImg()
    {
        if(!picture.loadFromFile("result.jpg"))
            return 0;
    }
};


int main()
{
    std::cout<<"Buna ziua, selectati imaginea ce doriti sa fie editata:\n";
A:  std::cout<<"Poza 1 (Astronaut)\nPoza 2 (Stonehenge) \nPoza 3 (Frunza) \nPoza 4 (PH) \nPoza 5 (PH) \n";
    std::cin>>a;



    if(!(a>0 && a<4))
    {
        std::cout<<"Invalid Input, introduceti o valoare corecta - \n\n";
        system("PAUSE");
        system("CLS");
        goto A;
    }


    img p1;
    if(!p1.LoadImg())
        {
            std::cout<<"This window will close in 3 sec.";
            Sleep(3000);
            return -1;
        }
    img p3;

    temp p2;
    if(!p2.LoadImg())
        {
            std::cout<<"This window will close in 3 sec.";
            Sleep(3000);
            return -1;
        }


    system("CLS");
    std::cout<<"Alegeti ce doriti sa editati la poza selectata:\n";
    int h;
B:  std::cout<<" 0 : Rotire; \n 1 : Efecte; \n 2 : Cropare; \n 3 : Prelucrare pixeli;\n-1 : RETURN\n";
    std::cin>>h;
    system("CLS");

    if(!(h>-2 && h<4))
    {
        std::cout<<"Invalid Input, introduceti o valoare corecta - \n";
        goto B;
    }

    int v=0;
    if (h==0)
    {
        std::cout<<"----------------------------------------------------------------------------------\n";
        std::cout<<" 0 : Flip 1;\n 1 : Flip 2;\n-1 : RETURN\n";
        std::cout<<"----------------------------------------------------------------------------------\n";
        std::cin>>v;
        system("CLS");
    }
    if (h==1)
    {
        std::cout<<"----------------------------------------------------------------------------------\n";
        std::cout<<" 2 : Grayscale;\n 3 : Efect Danila 1\n 4 : Efect Danila 2\n-1 : RETURN\n";
        std::cout<<"----------------------------------------------------------------------------------\n";
        std::cin>>v;
        system("CLS");
    }
    if (h==3)
    {
        std::cout<<"----------------------------------------------------------------------------------\n";
        std::cout<<" 5 : Adaugare rama pozei\n 6 : Schimbarea culorii pixelilor prezenti pe diagonala principala\n-1 : RETURN\n";
        std::cout<<"----------------------------------------------------------------------------------\n";
        std::cin>>v;
        system("CLS");
    }

    /*Prelucrare Pixeli*/
    sf::Image picture1;
    sf::Image picture2;
    sf::Image picture3;

    if (h==-1)
    {
        goto A;
    }
    if (v==-1)  /*posibilitatea utilizatorului de a da back*/
    {
        goto B;
    }

    sf::Color culoare(0,0,0);

    picture1.create(p1.picture.getSize().x,p1.picture.getSize().y,sf::Color::Black);
    picture1.copy(p1.picture,0,0);

    picture2.create(p2.picture.getSize().x,p2.picture.getSize().y,sf::Color::Black);
    picture2.copy(p2.picture,0,0);



    if(v==0)
        {
            if(s==0)
            {
            //p1.LoadImg();
            picture1.flipHorizontally();

            //if(!picture1.saveToFile("temp1.jpg"))
            /*if(!picture1.saveToFile("result.jpg"))
                {
                    std::cout<<"This window will close in 3 sec.   4";
                    Sleep(3000);
                    return -1;
                }*/


            std::cout<<"Doriti sa continuati prelucrarea pozei?(Da/Nu)\n";
            std::cin>>o;
            if((o[0]=='D'||o[0]=='d') && (o[1]=='A'||o[1]=='a') && o[2]==0)
            {
                s==1;
                goto B;
            }}
            else
            {
            //p2.LoadImg();
            picture1.copy(p1.picture,0,0);
            picture1.flipHorizontally();

           /* if(!picture1.saveToFile("result.jpg"))
                {
                    std::cout<<"This window will close in 3 sec.   4";
                    Sleep(3000);
                    return -1;
                }*/


            std::cout<<"Doriti sa continuati prelucrarea pozei?(Da/Nu)\n";
            std::cin>>o;
            if((o[0]=='D'||o[0]=='d') && (o[1]=='A'||o[1]=='a') && o[2]==0)
            {
                goto B;
            }}
        }




    if(v==1)
        {
            if(s==0)
            {
            //p1.LoadImg();
            picture1.flipVertically();

            /*if(!picture1.saveToFile("result.jpg"))
            {
                std::cout<<"This window will close in 3 sec.   4";
                Sleep(3000);
                return -1;
            }*/


        std::cout<<"Doriti sa continuati prelucrarea pozei?(Da/Nu)\n";
        std::cin>>o;
        if((o[0]=='D'||o[0]=='d') && (o[1]=='A'||o[1]=='a') && o[2]==0)
        {
            goto B;
            s++;
        }

        }
        else
            {
            //p2.LoadImg();
            picture1.copy(p1.picture,0,0);
            picture1.flipVertically();

            /*if(!picture1.saveToFile("result.jpg"))
            {
                std::cout<<"This window will close in 3 sec.   4";
                Sleep(3000);
                return -1;
            }*/


        std::cout<<"Doriti sa continuati prelucrarea pozei?(Da/Nu)\n";
        std::cin>>o;
        if((o[0]=='D'||o[0]=='d') && (o[1]=='A'||o[1]=='a') && o[2]==0)
        {
            goto B;
            s++;
        }

            }
        }

    if(v==2)
    {
        p1.LoadImg();
        for(int i=0;i<picture1.getSize().x;i++)
            for(int j=0;j<picture1.getSize().y;j++)
            {
                culoare.r=picture1.getPixel(i,j).r;
                culoare.g=picture1.getPixel(i,j).r;
                culoare.b=picture1.getPixel(i,j).r;
                picture1.setPixel(i,j,culoare);
            }

            /* if(!picture1.saveToFile("result.jpg"))
            {
                std::cout<<"This window will close in 3 sec.   4";
                Sleep(3000);
                return -1;
            }*/


            std::cout<<"Doriti sa continuati prelucrarea pozei?(Da/Nu)\n";
            std::cin>>o;
            if((o[0]=='D'||o[0]=='d') && (o[1]=='A'||o[1]=='a') && o[2]==0)
            {
                s==1;
                goto B;
            }
    }
    if(v==3)
    {
        for(int i=0;i<picture1.getSize().x;i++)
            for(int j=0;j<picture1.getSize().y;j++)
            {
                culoare.r=255-picture1.getPixel(i,j).r;
                culoare.g=255-picture1.getPixel(i,j).r;
                culoare.b=255-picture1.getPixel(i,j).r;
                picture1.setPixel(i,j,culoare);
            }

             if(!picture1.saveToFile("result.jpg"))
            {
                std::cout<<"This window will close in 3 sec.   4";
                Sleep(3000);
                return -1;
            }

            std::cout<<"Doriti sa continuati prelucrarea pozei?(Da/Nu)\n";
            std::cin>>o;
            if((o[0]=='D'||o[0]=='d') && (o[1]=='A'||o[1]=='a') && o[2]==0)
            {
                goto B;
            }

    }
    if(v==4)
    {
        for(int i=0;i<picture1.getSize().x;i++)
            for(int j=0;j<picture1.getSize().y;j++)
            {
                culoare.r=picture1.getPixel(i,j).r;
                culoare.g=picture1.getPixel(i,j).r+30;
                culoare.b=picture1.getPixel(i,j).r;
                picture1.setPixel(i,j,culoare);
            }

             if(!picture1.saveToFile("result.jpg"))
            {
                std::cout<<"This window will close in 3 sec.   4";
                Sleep(3000);
                return -1;
            }

            std::cout<<"Doriti sa continuati prelucrarea pozei?(Da/Nu)\n";
            std::cin>>o;
            if((o[0]=='D'||o[0]=='d') && (o[1]=='A'||o[1]=='a') && o[2]==0)
            {
                goto B;
            }

    }

    if (v==5)
    {
        int q;
        std::cout<<"Culoarea ramei adaugate sa fie:\n 0 : Alb;\n 1 : Negru;\n";
        std::cin>>q;
        if (q==0)
        {
            picture1.create(p1.picture.getSize().x+50,p1.picture.getSize().y+50,sf::Color::White);
            picture1.copy(p1.picture,25,25);
            for(int j=0;j<=p1.picture.getSize().y+50;j+=1)
                for(int i=0;i<=25;i+=1)
                    picture1.setPixel(i,j,sf::Color::White);     /*Adaugare rama pe partea stanga*/

            for(int j=0;j<=p1.picture.getSize().y+50;j+=1)
                for(int i=p1.picture.getSize().x+25;i<=p1.picture.getSize().x+49;i+=1)
                    picture1.setPixel(i,j,sf::Color::White);     /*Adaugare rama pe partea dreapta*/

            for(int i=0;i<=p1.picture.getSize().x+50;i+=1)
                for(int j=0;j<=25;j+=1)
                    picture1.setPixel(i,j,sf::Color::White);     /*Adaugare rama sus */

            for(int i=0;i<=p1.picture.getSize().x+50;i+=1)
                for(int j=p1.picture.getSize().y+25;j<=p1.picture.getSize().y+50;j+=1)
                    picture1.setPixel(i,j,sf::Color::White);     /*Adaugare rama jos */

             if(!picture1.saveToFile("result.jpg"))
            {
                std::cout<<"This window will close in 3 sec.   4";
                Sleep(3000);
                return -1;
            }

            std::cout<<"Doriti sa continuati prelucrarea pozei?(Da/Nu)\n";
            std::cin>>o;
            if((o[0]=='D'||o[0]=='d') && (o[1]=='A'||o[1]=='a') && o[2]==0)
            {
                goto B;
            }

        }

        if (q==1)
        {
            picture1.create(p1.picture.getSize().x+50,p1.picture.getSize().y+50,sf::Color::White);
            picture1.copy(p1.picture,25,25);
            for(int j=0;j<=p1.picture.getSize().y+50;j+=1)
                for(int i=0;i<=25;i+=1)
                    picture1.setPixel(i,j,sf::Color::Black);     /*Adaugare rama pe partea stanga*/

            for(int j=0;j<=p1.picture.getSize().y+50;j+=1)
                for(int i=p1.picture.getSize().x+25;i<=p1.picture.getSize().x+49;i+=1)
                    picture1.setPixel(i,j,sf::Color::Black);     /*Adaugare rama pe partea dreapta*/

            for(int i=0;i<=p1.picture.getSize().x+50;i+=1)
                for(int j=0;j<=25;j+=1)
                    picture1.setPixel(i,j,sf::Color::Black);     /*Adaugare rama sus */

            for(int i=0;i<=p1.picture.getSize().x+50;i+=1)
                for(int j=p1.picture.getSize().y+25;j<=p1.picture.getSize().y+50;j+=1)
                    picture1.setPixel(i,j,sf::Color::Black);     /*Adaugare rama jos */

             if(!picture1.saveToFile("result.jpg"))
            {
                std::cout<<"This window will close in 3 sec.   4";
                Sleep(3000);
                return -1;
            }

            std::cout<<"Doriti sa continuati prelucrarea pozei?(Da/Nu)\n";
            std::cin>>o;
            if((o[0]=='D'||o[0]=='d') && (o[1]=='A'||o[1]=='a') && o[2]==0)
            {
                goto B;
            }
        }
    }
    if (v==6)
    {
        int h;
        picture1.create(p1.picture.getSize().x,p1.picture.getSize().y,sf::Color::White);
        picture1.copy(p1.picture,0,0);
        int a=p1.picture.getSize().x;
        int b=p1.picture.getSize().y;
        if(a!=b)
        {
            std::cout<<"Dimensiunea pozei nu este egala, doriti sa:\n 0 : Selectati alta poza\n 1 : Continuati cu modificarea diagonalei principale?\n";
            std::cin>>h;
        }

        if (h==1)
        {
        for(int i=0;i<p1.picture.getSize().x;i+=1)
            for(int j=0;j<p1.picture.getSize().y;j+=1)
                if(i==j)
                {
                    picture1.setPixel(i,j,sf::Color::Yellow);
                }
        }
            else
            {
            goto A;
            }

             if(!picture1.saveToFile("result.jpg"))
            {
                std::cout<<"This window will close in 3 sec.   4";
                Sleep(3000);
                return -1;
            }

            std::cout<<"Doriti sa continuati prelucrarea pozei?(Da/Nu)\n";
            std::cin>>o;
            if((o[0]=='D'||o[0]=='d') && (o[1]=='A'||o[1]=='a') && o[2]==0)
            {
                goto B;
            }

        //system("CLS");

    }

   /* if(h==3)
    {
    if(!picture1.saveToFile("result.jpg"))
            {
                std::cout<<"This window will close in 3 sec.   1";
                Sleep(3000);
                return -1;
            }
    }*/


    //sf::Image picture2;

    //sf::Image picture3;

    if(h==2)
    {
        int a,b,c,d;
        std::cout<<"Crop size?";
C:      std::cin>>a;
        if(a<0 || a>p1.picture.getSize().x)
        {
            std::cout<<"invalid input; valoarea introdusa este mai mare decat permite poza sau mai mica ca 0, introduceti o alta valoare:\n";
            goto C;
        }
D:      std::cin>>b;
        if(b<0 || b>p1.picture.getSize().y)
        {
            std::cout<<"invalid input; valoarea introdusa este mai mare decat permite poza sau mai mica ca 0, introduceti o alta valoare:\n";
            goto D;
        }
        std::cout<<"Crop starting point?";
E:      std::cin>>c;
        if(c<0 || c>p1.picture.getSize().x-a)
        {
            std::cout<<"invalid input, valoarea introdusa este mai mare decat permite poza sau mai mica ca 0, introduceti o alta valoare:\n ";
            goto E;
        }
F:      std::cin>>d;
        if(d<0 || d>p1.picture.getSize().y-b)
        {
            std::cout<<"invalid input, valoarea introdusa este mai mare decat permite poza sau mai mica ca 0, introduceti o alta valoare:\n";
            goto F;
        }
        picture1.create(a,b,sf::Color::White);
        picture1.copy(p1.picture,c,d);

         if(!picture1.saveToFile("result.jpg"))
            {
                std::cout<<"This window will close in 3 sec.   4";
                Sleep(3000);
                return -1;
            }

            std::cout<<"Doriti sa continuati prelucrarea pozei?(Da/Nu)\n";
            std::cin>>o;
            if((o[0]=='D'||o[0]=='d') && (o[1]=='A'||o[1]=='a') && o[2]==0)
            {
                goto B;
            }

    }

    /*if(h==2)
    {
        if(!picture1.saveToFile("result3.jpg"))
            {
                std::cout<<"This window will close in 3 sec.   3";
                Sleep(3000);
                return -1;
            }
    }
    if(h==1 or h==0)
    {*/


    //system("PAUSE");


    picture3.create(p1.picture.getSize().x,p1.picture.getSize().y,sf::Color::Black);
    picture3.copy(p1.picture,0,0);

    //picture3.copy(p2.picture,0,0);
    if(!picture3.saveToFile("result.jpg"))
            {
                std::cout<<"This window will close in 3 sec.   4";
                Sleep(3000);
                return -1;
            }

    return EXIT_SUCCESS;
}
 

Compared to when I first posted I changed the code to only use p1 and only save the result using another object, p3 that will copy p1 and save it in result.jpg; No change..

bi.danila

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Saving Photos while the code is running
« Reply #3 on: January 15, 2017, 11:41:09 pm »
Solved the problem using in every if where the selection happens:

picture1.loadFromFile("result.jpg")

And also added in the img object where the picture.loadFromFile was a picture.saveToFile("result.jpg") so that it might update whenever the load was being made.