Skip to content

Commit a893672

Browse files
committed
Small PIL task
1 parent 356f644 commit a893672

File tree

4 files changed

+50
-3
lines changed

4 files changed

+50
-3
lines changed

.DS_Store

0 Bytes
Binary file not shown.
3.25 MB
Loading

notebooks/lab-6/lab6-part1-numpy.ipynb

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@
166166
},
167167
{
168168
"cell_type": "code",
169-
"execution_count": 8,
169+
"execution_count": 1,
170170
"metadata": {},
171171
"outputs": [],
172172
"source": [
@@ -310,7 +310,7 @@
310310
"cell_type": "markdown",
311311
"metadata": {},
312312
"source": [
313-
"These next two functions are really neat, since these are image editing techniques which feature in production image editing software (and apps like Instagram and the iPhone Photos app)!\n",
313+
"These next two functions - `brightness` and `contrast` - are really neat, since these are image editing techniques which feature in production image editing software (and apps like Instagram and the iPhone Photos app)!\n",
314314
"\n",
315315
"`brightness`: the first thing that you'll note about this function is that, in addition to a NumPy array\n",
316316
"representing an image, it takes in a factor. Scale the brightness of the image by multiplying every element\n",
@@ -336,6 +336,30 @@
336336
"save_image(brightness(img, 1.5), \"cs41img_bright.png\")"
337337
]
338338
},
339+
{
340+
"cell_type": "markdown",
341+
"metadata": {},
342+
"source": [
343+
"`brightness_PIL`: it turns out that for many image processing tasks, like this one, NumPy isn't necessarily the best way to go. You'll recall that your implementation of `brightness` likely took a few minutes to run. Can we use Python's image processing libraries to speed up the computation time and make our code even more pythonic?\n",
344+
"\n",
345+
"Take a moment and check out the `Image.point()` function included with `PIL`. It takes in a lambda (which takes in one argument), then applies that lambda to each point (R, G, B value) in the image. So now that we've implemented our solution to `brightness` above, how might we implement `brightness_PIL`, which makes use of this functionality from `PIL` in order to achieve the same effect?"
346+
]
347+
},
348+
{
349+
"cell_type": "code",
350+
"execution_count": 8,
351+
"metadata": {},
352+
"outputs": [],
353+
"source": [
354+
"\n",
355+
"def brightness_PIL(img, factor):\n",
356+
" # Implement your function here\n",
357+
" return img\n",
358+
"\n",
359+
"img = brightness_PIL(Image.open(\"cs41img.png\"), 1.5)\n",
360+
"img.save(\"cs41img_bright_PIL.png\")\n"
361+
]
362+
},
339363
{
340364
"attachments": {
341365
"SOLcs41img_contrast.png": {
@@ -522,7 +546,7 @@
522546
"name": "python",
523547
"nbconvert_exporter": "python",
524548
"pygments_lexer": "ipython3",
525-
"version": "3.7.4"
549+
"version": "3.8.0"
526550
}
527551
},
528552
"nbformat": 4,

solutions/lab-6/lab6-part1-numpy.ipynb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,29 @@
347347
"save_image(brightness(img, 1.5), \"cs41img_bright.png\")"
348348
]
349349
},
350+
{
351+
"cell_type": "markdown",
352+
"metadata": {},
353+
"source": [
354+
"`brightness_PIL`: it turns out that for many image processing tasks, like this one, NumPy isn't necessarily the best way to go. You'll recall that your implementation of `brightness` likely took a few minutes to run. Can we use Python's image processing libraries to speed up the computation time and make our code even more pythonic?\n",
355+
"\n",
356+
"Take a moment and check out the `Image.point()` function included with `PIL`. It takes in a lambda (which takes in one argument), then applies that lambda to each point (R, G, B value) in the image. So now that we've implemented our solution to `brightness` above, how might we implement `brightness_PIL`, which makes use of this functionality from `PIL` in order to achieve the same effect?"
357+
]
358+
},
359+
{
360+
"cell_type": "code",
361+
"execution_count": null,
362+
"metadata": {},
363+
"outputs": [],
364+
"source": [
365+
"def brightness_PIL(img, factor):\n",
366+
" img = img.point(lambda p: min(p*factor, 255))\n",
367+
" return img\n",
368+
"\n",
369+
"img = brightness_PIL(Image.open(\"cs41img.png\"), 1.5)\n",
370+
"img.save(\"cs41img_bright_PIL.png\")"
371+
]
372+
},
350373
{
351374
"attachments": {
352375
"SOLcs41img_contrast.png": {

0 commit comments

Comments
 (0)