Repository: AUTOMATIC1111/stable-diffusion-webui

Issue:
Portrait mode images generates in landscape mode in img2img [Bug]: 
### Is there an existing issue for this?

- [X] I have searched the existing issues and checked the recent builds/commits

### What happened?

Image in portrait mode shows up fine in the preview, but when the alternative image is generated it is rotated to landscape mode.

### Steps to reproduce the problem

1. Load a image that was taken using a phone in portrait mode.
2. Set a prompt and press generate.


### What should have happened?

It should have generated the output image in portrait mode as well.

### Commit where the problem happens

6bd6154a92eb05c80d66df661a38f8b70cc13729

### What platforms do you use to access UI ?

Windows

### What browsers do you use to access the UI ?

Microsoft Edge

### Command Line Arguments

```Shell
--xformers
```


### Additional information, context and logs

When images are taken in portrait mode, they are often stored as landscape, but have information that it is portrait so that they can be displayed correctly in image viewers, this should be used to determine how the output image should be generated.


Discussion:
[This](https://stackoverflow.com/a/58116860/12572922) looks like a promising solution. Can you provide an image that exhibits the problem? 
As seen in responses to #2374, people seem to be against features that are "necessary for very few niche scenarios." Even before reading that, #3524 to me seemed borderline niche. But unlike with #2374, the solution to #3524 wouldn't add any clutter to the UI, so it seems innocuous enough to add.
![example](https://user-images.githubusercontent.com/13852004/197708283-c46e2618-9a76-447c-8391-e533a8e372d2.png)

As you can see, the image has rotated when it was generated (I used only .2 for denoising strength so that it's clearly seen). This also makes the AI unable to properly identify features, as, for example, a person standing up won't be recognized as the person is now "on his/her side" and it will try to generate some kind of weird creature instead as it try to make sense of it all.
Also, I don't know if this should be seen as a niche scenario, as AI generation on phones are very popular and it's pretty common to take portrait photos on the phone as opposed to turning the phone sideways. As AI image to image becomes more popular on phones (through services, as obviously it doesn't have a graphics card) this will probably be a important feature.
> [EXAMPLE]
> 
> As you can see, the image has rotated when it was generated (I used only .2 for denoising strength so that it's clearly seen). This also makes the AI unable to properly identify features, as, for example, a person standing up won't be recognized as the person is now "on his/her side" and it will try to generate some kind of weird creature instead as it try to make sense of it all.

Is there any way you can send me the problematic photo itself, so I can test with it? Any way I can think of sending an iPhone photo to my desktop might strip it of the problematic EXIF data.
The photo is from the desktop, it was taken using an Android phone:

![IMG_0068](https://user-images.githubusercontent.com/13852004/197710216-c82d2760-03f7-480a-8768-9529b3294280.JPG)


I think, in general, wysiwyg should be implemented anywhere possible, because otherwise it becomes pretty unintuitive.

Resolving patch:
diff --git a/modules/img2img.py b/modules/img2img.py
--- a/modules/img2img.py
+++ b/modules/img2img.py
@@ -39,6 +39,8 @@ def process_batch(p, input_dir, output_dir, args):
             break
 
         img = Image.open(image)
+        # Use the EXIF orientation of photos taken by smartphones.
+        img = ImageOps.exif_transpose(img) 
         p.init_images = [img] * p.batch_size
 
         proc = modules.scripts.scripts_img2img.run(p, *args)
@@ -61,19 +63,25 @@ def img2img(mode: int, prompt: str, negative_prompt: str, prompt_style: str, pro
     is_batch = mode == 2
 
     if is_inpaint:
+        # Drawn mask
         if mask_mode == 0:
             image = init_img_with_mask['image']
             mask = init_img_with_mask['mask']
             alpha_mask = ImageOps.invert(image.split()[-1]).convert('L').point(lambda x: 255 if x > 0 else 0, mode='1')
             mask = ImageChops.lighter(alpha_mask, mask.convert('L')).convert('L')
             image = image.convert('RGB')
+        # Uploaded mask
         else:
             image = init_img_inpaint
             mask = init_mask_inpaint
+    # No mask
     else:
         image = init_img
         mask = None
 
+    # Use the EXIF orientation of photos taken by smartphones.
+    image = ImageOps.exif_transpose(image) 
+
     assert 0. <= denoising_strength <= 1., 'can only work with strength in [0.0, 1.0]'
 
     p = StableDiffusionProcessingImg2Img(


Test patch: