Skip to content

Commit 839cfae

Browse files
authored
Merge pull request udacity#85 from GabrielePicco/chapther6AllowLoadFromURL
Added ability to load images from an URL
2 parents a7bbe6a + ce0c525 commit 839cfae

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

Diff for: style-transfer/Style_Transfer_Exercise.ipynb

+7-2
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,13 @@
3838
"%matplotlib inline\n",
3939
"\n",
4040
"from PIL import Image\n",
41+
"from io import BytesIO\n",
4142
"import matplotlib.pyplot as plt\n",
4243
"import numpy as np\n",
4344
"\n",
4445
"import torch\n",
4546
"import torch.optim as optim\n",
47+
"import requests\n",
4648
"from torchvision import transforms, models"
4749
]
4850
},
@@ -109,8 +111,11 @@
109111
"def load_image(img_path, max_size=400, shape=None):\n",
110112
" ''' Load in and transform an image, making sure the image\n",
111113
" is <= 400 pixels in the x-y dims.'''\n",
112-
" \n",
113-
" image = Image.open(img_path).convert('RGB')\n",
114+
" if \"http\" in img_path:\n",
115+
" response = requests.get(img_path)\n",
116+
" image = Image.open(BytesIO(response.content)).convert('RGB')\n",
117+
" else:\n",
118+
" image = Image.open(img_path).convert('RGB')\n",
114119
" \n",
115120
" # large images will slow down processing\n",
116121
" if max(image.size) > max_size:\n",

Diff for: style-transfer/Style_Transfer_Solution.ipynb

+7-2
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,13 @@
3838
"%matplotlib inline\n",
3939
"\n",
4040
"from PIL import Image\n",
41+
"from io import BytesIO\n",
4142
"import matplotlib.pyplot as plt\n",
4243
"import numpy as np\n",
4344
"\n",
4445
"import torch\n",
4546
"import torch.optim as optim\n",
47+
"import requests\n",
4648
"from torchvision import transforms, models"
4749
]
4850
},
@@ -158,8 +160,11 @@
158160
"def load_image(img_path, max_size=400, shape=None):\n",
159161
" ''' Load in and transform an image, making sure the image\n",
160162
" is <= 400 pixels in the x-y dims.'''\n",
161-
" \n",
162-
" image = Image.open(img_path).convert('RGB')\n",
163+
" if \"http\" in img_path:\n",
164+
" response = requests.get(img_path)\n",
165+
" image = Image.open(BytesIO(response.content)).convert('RGB')\n",
166+
" else:\n",
167+
" image = Image.open(img_path).convert('RGB')\n",
163168
" \n",
164169
" # large images will slow down processing\n",
165170
" if max(image.size) > max_size:\n",

0 commit comments

Comments
 (0)