# Flip the image HORIZONTALLY

**URL:** <https://discourse.processing.org/t/flip-the-image-horizontally/25165>\
**Category:** Beginners\
**Tags:** homework\
**Created:** [November 4, 2020, 2:22am UTC](https://discourse.processing.org/t/flip-the-image-horizontally/25165 "2020-11-04T02:22:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BettyBai](https://avatars.discourse-cdn.com/v4/letter/b/aeb1de/32.png) [@BettyBai](https://discourse.processing.org/u/BettyBai)\
**Post date:** [November 4, 2020, 2:22am UTC](https://discourse.processing.org/t/flip-the-image-horizontally/25165/1 "2020-11-04T02:22:25Z")

</div>

> please format code with \</\> button \* [homework policy](https://discourse.processing.org/faq/#homework) \* [asking questions](https://discourse.processing.org/t/2147)

Hi, I’m new to processing, and I’m trying to flip the image horizontally. However, I am facing the problem of only flipping half of the image, could you guys help me with it?

Here is my code:

PImage img;

void setup()  
{  
img = loadImage( “coffee.JPG”);  
size(img.width,img.height); // use size of the image  
noLoop();  
}

void draw()  
{  
// Use for loops to go through every pixel  
for (int y = 0; y \< img.height; y++ )  
{  
for (int x = 0; x \< img.width/2; x++ )  
{  
// get the color values  
float r = red (img.get(x, y));  
float g = green(img.get(x, y));  
float b = blue (img.get(x, y));

```
  / ********image processing goes here********** /

  // Set the new color to the pixel
  img.set(width-x, y, color(r,g,b));    
}

```

}  
image(img, 0, 0); // display the image  
}

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 4, 2020, 3:33am UTC](https://discourse.processing.org/t/flip-the-image-horizontally/25165/2 "2020-11-04T03:33:02Z")

</div>

Yes. Please check your for-loops

---

<div class="post-metadata">

**Author:** ![matheplica](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/matheplica/32/21035_2.png) [@matheplica](https://discourse.processing.org/u/matheplica)\
**Post date:** [November 4, 2020, 10:32am UTC](https://discourse.processing.org/t/flip-the-image-horizontally/25165/3 "2020-11-04T10:32:40Z")

</div>

just add

```auto
scale(1, -1);

```

before

```auto
image(img, 0, 0);

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [November 4, 2020, 6:02pm UTC](https://discourse.processing.org/t/flip-the-image-horizontally/25165/4 "2020-11-04T18:02:42Z")

</div>

Hello,

Please format your code:  
[https://discourse.processing.org/faq#format-your-code](https://discourse.processing.org/faq#format-your-code)

I usually just skip over unformatted code because I have to fix it to test it!

> [@BettyBai](#):
>
> I am facing the problem of only flipping half of the image

Your code appears to do that.

This image:  
 ![tint1](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/1/1a1f8bf0d6fb1dc3744c3a8099e9595978c75020.jpeg)

becomes this with your code:  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/0/0794ac8be38d5e009a8d6612dba688187b5bce22.jpeg)

I had to manually set size() to image size.

There is a comment here about passing parameters to size() in settings() and loadImage():

> **[settings() / Reference](https://processing.org/reference/settings_.html)**
>
> The settings() function is new with Processing 3.0. It's not needed in most sketches. It's only useful when it's absolutely necessary to define the parameters to size() with a variable. …

`:)`
