We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN 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.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `modules/img2img.py`
Content:
```
1 import math
2 import os
3 import sys
4 import traceback
5 
6 import numpy as np
7 from PIL import Image, ImageOps, ImageChops
8 
9 from modules import devices
10 from modules.processing import Processed, StableDiffusionProcessingImg2Img, process_images
11 from modules.shared import opts, state
12 import modules.shared as shared
13 import modules.processing as processing
14 from modules.ui import plaintext_to_html
15 import modules.images as images
16 import modules.scripts
17 
18 
19 def process_batch(p, input_dir, output_dir, args):
20     processing.fix_seed(p)
21 
22     images = [file for file in [os.path.join(input_dir, x) for x in os.listdir(input_dir)] if os.path.isfile(file)]
23 
24     print(f"Will process {len(images)} images, creating {p.n_iter * p.batch_size} new images for each.")
25 
26     save_normally = output_dir == ''
27 
28     p.do_not_save_grid = True
29     p.do_not_save_samples = not save_normally
30 
31     state.job_count = len(images) * p.n_iter
32 
33     for i, image in enumerate(images):
34         state.job = f"{i+1} out of {len(images)}"
35         if state.skipped:
36             state.skipped = False
37 
38         if state.interrupted:
39             break
40 
41         img = Image.open(image)
42         p.init_images = [img] * p.batch_size
43 
44         proc = modules.scripts.scripts_img2img.run(p, *args)
45         if proc is None:
46             proc = process_images(p)
47 
48         for n, processed_image in enumerate(proc.images):
49             filename = os.path.basename(image)
50 
51             if n > 0:
52                 left, right = os.path.splitext(filename)
53                 filename = f"{left}-{n}{right}"
54 
55             if not save_normally:
56                 processed_image.save(os.path.join(output_dir, filename))
57 
58 
59 def img2img(mode: int, prompt: str, negative_prompt: str, prompt_style: str, prompt_style2: str, init_img, init_img_with_mask, init_img_inpaint, init_mask_inpaint, mask_mode, steps: int, sampler_index: int, mask_blur: int, inpainting_fill: int, restore_faces: bool, tiling: bool, n_iter: int, batch_size: int, cfg_scale: float, denoising_strength: float, seed: int, subseed: int, subseed_strength: float, seed_resize_from_h: int, seed_resize_from_w: int, seed_enable_extras: bool, height: int, width: int, resize_mode: int, inpaint_full_res: bool, inpaint_full_res_padding: int, inpainting_mask_invert: int, img2img_batch_input_dir: str, img2img_batch_output_dir: str, *args):
60     is_inpaint = mode == 1
61     is_batch = mode == 2
62 
63     if is_inpaint:
64         if mask_mode == 0:
65             image = init_img_with_mask['image']
66             mask = init_img_with_mask['mask']
67             alpha_mask = ImageOps.invert(image.split()[-1]).convert('L').point(lambda x: 255 if x > 0 else 0, mode='1')
68             mask = ImageChops.lighter(alpha_mask, mask.convert('L')).convert('L')
69             image = image.convert('RGB')
70         else:
71             image = init_img_inpaint
72             mask = init_mask_inpaint
73     else:
74         image = init_img
75         mask = None
76 
77     assert 0. <= denoising_strength <= 1., 'can only work with strength in [0.0, 1.0]'
78 
79     p = StableDiffusionProcessingImg2Img(
80         sd_model=shared.sd_model,
81         outpath_samples=opts.outdir_samples or opts.outdir_img2img_samples,
82         outpath_grids=opts.outdir_grids or opts.outdir_img2img_grids,
83         prompt=prompt,
84         negative_prompt=negative_prompt,
85         styles=[prompt_style, prompt_style2],
86         seed=seed,
87         subseed=subseed,
88         subseed_strength=subseed_strength,
89         seed_resize_from_h=seed_resize_from_h,
90         seed_resize_from_w=seed_resize_from_w,
91         seed_enable_extras=seed_enable_extras,
92         sampler_index=sampler_index,
93         batch_size=batch_size,
94         n_iter=n_iter,
95         steps=steps,
96         cfg_scale=cfg_scale,
97         width=width,
98         height=height,
99         restore_faces=restore_faces,
100         tiling=tiling,
101         init_images=[image],
102         mask=mask,
103         mask_blur=mask_blur,
104         inpainting_fill=inpainting_fill,
105         resize_mode=resize_mode,
106         denoising_strength=denoising_strength,
107         inpaint_full_res=inpaint_full_res,
108         inpaint_full_res_padding=inpaint_full_res_padding,
109         inpainting_mask_invert=inpainting_mask_invert,
110     )
111 
112     p.scripts = modules.scripts.scripts_txt2img
113     p.script_args = args
114 
115     if shared.cmd_opts.enable_console_prompts:
116         print(f"\nimg2img: {prompt}", file=shared.progress_print_out)
117 
118     p.extra_generation_params["Mask blur"] = mask_blur
119 
120     if is_batch:
121         assert not shared.cmd_opts.hide_ui_dir_config, "Launched with --hide-ui-dir-config, batch img2img disabled"
122 
123         process_batch(p, img2img_batch_input_dir, img2img_batch_output_dir, args)
124 
125         processed = Processed(p, [], p.seed, "")
126     else:
127         processed = modules.scripts.scripts_img2img.run(p, *args)
128         if processed is None:
129             processed = process_images(p)
130 
131     shared.total_tqdm.clear()
132 
133     generation_info_js = processed.js()
134     if opts.samples_log_stdout:
135         print(generation_info_js)
136 
137     if opts.do_not_show_images:
138         processed.images = []
139 
140     return processed.images, generation_info_js, plaintext_to_html(processed.info)
141
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/modules/img2img.py b/modules/img2img.py
--- a/modules/img2img.py
+++ b/modules/img2img.py
@@ -39,6 +39,8 @@
             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 @@
     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(

{"golden_diff": "diff --git a/modules/img2img.py b/modules/img2img.py\n--- a/modules/img2img.py\n+++ b/modules/img2img.py\n@@ -39,6 +39,8 @@\n             break\r\n \r\n         img = Image.open(image)\r\n+        # Use the EXIF orientation of photos taken by smartphones.\r\n+        img = ImageOps.exif_transpose(img) \r\n         p.init_images = [img] * p.batch_size\r\n \r\n         proc = modules.scripts.scripts_img2img.run(p, *args)\r\n@@ -61,19 +63,25 @@\n     is_batch = mode == 2\r\n \r\n     if is_inpaint:\r\n+        # Drawn mask\r\n         if mask_mode == 0:\r\n             image = init_img_with_mask['image']\r\n             mask = init_img_with_mask['mask']\r\n             alpha_mask = ImageOps.invert(image.split()[-1]).convert('L').point(lambda x: 255 if x > 0 else 0, mode='1')\r\n             mask = ImageChops.lighter(alpha_mask, mask.convert('L')).convert('L')\r\n             image = image.convert('RGB')\r\n+        # Uploaded mask\r\n         else:\r\n             image = init_img_inpaint\r\n             mask = init_mask_inpaint\r\n+    # No mask\r\n     else:\r\n         image = init_img\r\n         mask = None\r\n \r\n+    # Use the EXIF orientation of photos taken by smartphones.\r\n+    image = ImageOps.exif_transpose(image) \r\n+\r\n     assert 0. <= denoising_strength <= 1., 'can only work with strength in [0.0, 1.0]'\r\n \r\n     p = StableDiffusionProcessingImg2Img(\n", "issue": "Portrait mode images generates in landscape mode in img2img [Bug]: \n### Is there an existing issue for this?\n\n- [X] I have searched the existing issues and checked the recent builds/commits\n\n### What happened?\n\nImage in portrait mode shows up fine in the preview, but when the alternative image is generated it is rotated to landscape mode.\n\n### Steps to reproduce the problem\n\n1. Load a image that was taken using a phone in portrait mode.\r\n2. Set a prompt and press generate.\r\n\n\n### What should have happened?\n\nIt should have generated the output image in portrait mode as well.\n\n### Commit where the problem happens\n\n6bd6154a92eb05c80d66df661a38f8b70cc13729\n\n### What platforms do you use to access UI ?\n\nWindows\n\n### What browsers do you use to access the UI ?\n\nMicrosoft Edge\n\n### Command Line Arguments\n\n```Shell\n--xformers\n```\n\n\n### Additional information, context and logs\n\nWhen 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.\n", "before_files": [{"content": "import math\r\nimport os\r\nimport sys\r\nimport traceback\r\n\r\nimport numpy as np\r\nfrom PIL import Image, ImageOps, ImageChops\r\n\r\nfrom modules import devices\r\nfrom modules.processing import Processed, StableDiffusionProcessingImg2Img, process_images\r\nfrom modules.shared import opts, state\r\nimport modules.shared as shared\r\nimport modules.processing as processing\r\nfrom modules.ui import plaintext_to_html\r\nimport modules.images as images\r\nimport modules.scripts\r\n\r\n\r\ndef process_batch(p, input_dir, output_dir, args):\r\n    processing.fix_seed(p)\r\n\r\n    images = [file for file in [os.path.join(input_dir, x) for x in os.listdir(input_dir)] if os.path.isfile(file)]\r\n\r\n    print(f\"Will process {len(images)} images, creating {p.n_iter * p.batch_size} new images for each.\")\r\n\r\n    save_normally = output_dir == ''\r\n\r\n    p.do_not_save_grid = True\r\n    p.do_not_save_samples = not save_normally\r\n\r\n    state.job_count = len(images) * p.n_iter\r\n\r\n    for i, image in enumerate(images):\r\n        state.job = f\"{i+1} out of {len(images)}\"\r\n        if state.skipped:\r\n            state.skipped = False\r\n\r\n        if state.interrupted:\r\n            break\r\n\r\n        img = Image.open(image)\r\n        p.init_images = [img] * p.batch_size\r\n\r\n        proc = modules.scripts.scripts_img2img.run(p, *args)\r\n        if proc is None:\r\n            proc = process_images(p)\r\n\r\n        for n, processed_image in enumerate(proc.images):\r\n            filename = os.path.basename(image)\r\n\r\n            if n > 0:\r\n                left, right = os.path.splitext(filename)\r\n                filename = f\"{left}-{n}{right}\"\r\n\r\n            if not save_normally:\r\n                processed_image.save(os.path.join(output_dir, filename))\r\n\r\n\r\ndef img2img(mode: int, prompt: str, negative_prompt: str, prompt_style: str, prompt_style2: str, init_img, init_img_with_mask, init_img_inpaint, init_mask_inpaint, mask_mode, steps: int, sampler_index: int, mask_blur: int, inpainting_fill: int, restore_faces: bool, tiling: bool, n_iter: int, batch_size: int, cfg_scale: float, denoising_strength: float, seed: int, subseed: int, subseed_strength: float, seed_resize_from_h: int, seed_resize_from_w: int, seed_enable_extras: bool, height: int, width: int, resize_mode: int, inpaint_full_res: bool, inpaint_full_res_padding: int, inpainting_mask_invert: int, img2img_batch_input_dir: str, img2img_batch_output_dir: str, *args):\r\n    is_inpaint = mode == 1\r\n    is_batch = mode == 2\r\n\r\n    if is_inpaint:\r\n        if mask_mode == 0:\r\n            image = init_img_with_mask['image']\r\n            mask = init_img_with_mask['mask']\r\n            alpha_mask = ImageOps.invert(image.split()[-1]).convert('L').point(lambda x: 255 if x > 0 else 0, mode='1')\r\n            mask = ImageChops.lighter(alpha_mask, mask.convert('L')).convert('L')\r\n            image = image.convert('RGB')\r\n        else:\r\n            image = init_img_inpaint\r\n            mask = init_mask_inpaint\r\n    else:\r\n        image = init_img\r\n        mask = None\r\n\r\n    assert 0. <= denoising_strength <= 1., 'can only work with strength in [0.0, 1.0]'\r\n\r\n    p = StableDiffusionProcessingImg2Img(\r\n        sd_model=shared.sd_model,\r\n        outpath_samples=opts.outdir_samples or opts.outdir_img2img_samples,\r\n        outpath_grids=opts.outdir_grids or opts.outdir_img2img_grids,\r\n        prompt=prompt,\r\n        negative_prompt=negative_prompt,\r\n        styles=[prompt_style, prompt_style2],\r\n        seed=seed,\r\n        subseed=subseed,\r\n        subseed_strength=subseed_strength,\r\n        seed_resize_from_h=seed_resize_from_h,\r\n        seed_resize_from_w=seed_resize_from_w,\r\n        seed_enable_extras=seed_enable_extras,\r\n        sampler_index=sampler_index,\r\n        batch_size=batch_size,\r\n        n_iter=n_iter,\r\n        steps=steps,\r\n        cfg_scale=cfg_scale,\r\n        width=width,\r\n        height=height,\r\n        restore_faces=restore_faces,\r\n        tiling=tiling,\r\n        init_images=[image],\r\n        mask=mask,\r\n        mask_blur=mask_blur,\r\n        inpainting_fill=inpainting_fill,\r\n        resize_mode=resize_mode,\r\n        denoising_strength=denoising_strength,\r\n        inpaint_full_res=inpaint_full_res,\r\n        inpaint_full_res_padding=inpaint_full_res_padding,\r\n        inpainting_mask_invert=inpainting_mask_invert,\r\n    )\r\n\r\n    p.scripts = modules.scripts.scripts_txt2img\r\n    p.script_args = args\r\n\r\n    if shared.cmd_opts.enable_console_prompts:\r\n        print(f\"\\nimg2img: {prompt}\", file=shared.progress_print_out)\r\n\r\n    p.extra_generation_params[\"Mask blur\"] = mask_blur\r\n\r\n    if is_batch:\r\n        assert not shared.cmd_opts.hide_ui_dir_config, \"Launched with --hide-ui-dir-config, batch img2img disabled\"\r\n\r\n        process_batch(p, img2img_batch_input_dir, img2img_batch_output_dir, args)\r\n\r\n        processed = Processed(p, [], p.seed, \"\")\r\n    else:\r\n        processed = modules.scripts.scripts_img2img.run(p, *args)\r\n        if processed is None:\r\n            processed = process_images(p)\r\n\r\n    shared.total_tqdm.clear()\r\n\r\n    generation_info_js = processed.js()\r\n    if opts.samples_log_stdout:\r\n        print(generation_info_js)\r\n\r\n    if opts.do_not_show_images:\r\n        processed.images = []\r\n\r\n    return processed.images, generation_info_js, plaintext_to_html(processed.info)\r\n", "path": "modules/img2img.py"}], "after_files": [{"content": "import math\r\nimport os\r\nimport sys\r\nimport traceback\r\n\r\nimport numpy as np\r\nfrom PIL import Image, ImageOps, ImageChops\r\n\r\nfrom modules import devices\r\nfrom modules.processing import Processed, StableDiffusionProcessingImg2Img, process_images\r\nfrom modules.shared import opts, state\r\nimport modules.shared as shared\r\nimport modules.processing as processing\r\nfrom modules.ui import plaintext_to_html\r\nimport modules.images as images\r\nimport modules.scripts\r\n\r\n\r\ndef process_batch(p, input_dir, output_dir, args):\r\n    processing.fix_seed(p)\r\n\r\n    images = [file for file in [os.path.join(input_dir, x) for x in os.listdir(input_dir)] if os.path.isfile(file)]\r\n\r\n    print(f\"Will process {len(images)} images, creating {p.n_iter * p.batch_size} new images for each.\")\r\n\r\n    save_normally = output_dir == ''\r\n\r\n    p.do_not_save_grid = True\r\n    p.do_not_save_samples = not save_normally\r\n\r\n    state.job_count = len(images) * p.n_iter\r\n\r\n    for i, image in enumerate(images):\r\n        state.job = f\"{i+1} out of {len(images)}\"\r\n        if state.skipped:\r\n            state.skipped = False\r\n\r\n        if state.interrupted:\r\n            break\r\n\r\n        img = Image.open(image)\r\n        # Use the EXIF orientation of photos taken by smartphones.\r\n        img = ImageOps.exif_transpose(img) \r\n        p.init_images = [img] * p.batch_size\r\n\r\n        proc = modules.scripts.scripts_img2img.run(p, *args)\r\n        if proc is None:\r\n            proc = process_images(p)\r\n\r\n        for n, processed_image in enumerate(proc.images):\r\n            filename = os.path.basename(image)\r\n\r\n            if n > 0:\r\n                left, right = os.path.splitext(filename)\r\n                filename = f\"{left}-{n}{right}\"\r\n\r\n            if not save_normally:\r\n                processed_image.save(os.path.join(output_dir, filename))\r\n\r\n\r\ndef img2img(mode: int, prompt: str, negative_prompt: str, prompt_style: str, prompt_style2: str, init_img, init_img_with_mask, init_img_inpaint, init_mask_inpaint, mask_mode, steps: int, sampler_index: int, mask_blur: int, inpainting_fill: int, restore_faces: bool, tiling: bool, n_iter: int, batch_size: int, cfg_scale: float, denoising_strength: float, seed: int, subseed: int, subseed_strength: float, seed_resize_from_h: int, seed_resize_from_w: int, seed_enable_extras: bool, height: int, width: int, resize_mode: int, inpaint_full_res: bool, inpaint_full_res_padding: int, inpainting_mask_invert: int, img2img_batch_input_dir: str, img2img_batch_output_dir: str, *args):\r\n    is_inpaint = mode == 1\r\n    is_batch = mode == 2\r\n\r\n    if is_inpaint:\r\n        # Drawn mask\r\n        if mask_mode == 0:\r\n            image = init_img_with_mask['image']\r\n            mask = init_img_with_mask['mask']\r\n            alpha_mask = ImageOps.invert(image.split()[-1]).convert('L').point(lambda x: 255 if x > 0 else 0, mode='1')\r\n            mask = ImageChops.lighter(alpha_mask, mask.convert('L')).convert('L')\r\n            image = image.convert('RGB')\r\n        # Uploaded mask\r\n        else:\r\n            image = init_img_inpaint\r\n            mask = init_mask_inpaint\r\n    # No mask\r\n    else:\r\n        image = init_img\r\n        mask = None\r\n\r\n    # Use the EXIF orientation of photos taken by smartphones.\r\n    image = ImageOps.exif_transpose(image) \r\n\r\n    assert 0. <= denoising_strength <= 1., 'can only work with strength in [0.0, 1.0]'\r\n\r\n    p = StableDiffusionProcessingImg2Img(\r\n        sd_model=shared.sd_model,\r\n        outpath_samples=opts.outdir_samples or opts.outdir_img2img_samples,\r\n        outpath_grids=opts.outdir_grids or opts.outdir_img2img_grids,\r\n        prompt=prompt,\r\n        negative_prompt=negative_prompt,\r\n        styles=[prompt_style, prompt_style2],\r\n        seed=seed,\r\n        subseed=subseed,\r\n        subseed_strength=subseed_strength,\r\n        seed_resize_from_h=seed_resize_from_h,\r\n        seed_resize_from_w=seed_resize_from_w,\r\n        seed_enable_extras=seed_enable_extras,\r\n        sampler_index=sampler_index,\r\n        batch_size=batch_size,\r\n        n_iter=n_iter,\r\n        steps=steps,\r\n        cfg_scale=cfg_scale,\r\n        width=width,\r\n        height=height,\r\n        restore_faces=restore_faces,\r\n        tiling=tiling,\r\n        init_images=[image],\r\n        mask=mask,\r\n        mask_blur=mask_blur,\r\n        inpainting_fill=inpainting_fill,\r\n        resize_mode=resize_mode,\r\n        denoising_strength=denoising_strength,\r\n        inpaint_full_res=inpaint_full_res,\r\n        inpaint_full_res_padding=inpaint_full_res_padding,\r\n        inpainting_mask_invert=inpainting_mask_invert,\r\n    )\r\n\r\n    p.scripts = modules.scripts.scripts_txt2img\r\n    p.script_args = args\r\n\r\n    if shared.cmd_opts.enable_console_prompts:\r\n        print(f\"\\nimg2img: {prompt}\", file=shared.progress_print_out)\r\n\r\n    p.extra_generation_params[\"Mask blur\"] = mask_blur\r\n\r\n    if is_batch:\r\n        assert not shared.cmd_opts.hide_ui_dir_config, \"Launched with --hide-ui-dir-config, batch img2img disabled\"\r\n\r\n        process_batch(p, img2img_batch_input_dir, img2img_batch_output_dir, args)\r\n\r\n        processed = Processed(p, [], p.seed, \"\")\r\n    else:\r\n        processed = modules.scripts.scripts_img2img.run(p, *args)\r\n        if processed is None:\r\n            processed = process_images(p)\r\n\r\n    shared.total_tqdm.clear()\r\n\r\n    generation_info_js = processed.js()\r\n    if opts.samples_log_stdout:\r\n        print(generation_info_js)\r\n\r\n    if opts.do_not_show_images:\r\n        processed.images = []\r\n\r\n    return processed.images, generation_info_js, plaintext_to_html(processed.info)\r\n", "path": "modules/img2img.py"}]}