Skip to content

Commit f279650

Browse files
committed
Updating the .webp guide in python.
1 parent 808625d commit f279650

5 files changed

+128
-126
lines changed

Course/.DS_Store

2 KB
Binary file not shown.

Course/9_converting_images_to_optimised_formats/.ipynb_checkpoints/changing-image-types-and-next-generation-formats-checkpoint.ipynb

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@
260260
"name": "stdout",
261261
"output_type": "stream",
262262
"text": [
263+
"changing-image-types-and-next-generation-formats-starter-code.ipynb\r\n",
263264
"changing-image-types-and-next-generation-formats.ipynb\r\n",
264265
"example-1.png\r\n",
265266
"example-2.jpg\r\n"
@@ -324,14 +325,7 @@
324325
"cell_type": "markdown",
325326
"metadata": {},
326327
"source": [
327-
"If you check your current working directory, you'll now see two extra images:"
328-
]
329-
},
330-
{
331-
"cell_type": "markdown",
332-
"metadata": {},
333-
"source": [
334-
"![converting images in python](https://sempioneer.com/wp-content/uploads/2020/06/image-conversion-in-python.png)"
328+
"If you check your current working directory, you'll now see two extra images."
335329
]
336330
},
337331
{
@@ -427,20 +421,6 @@
427421
"--------------------------------------------"
428422
]
429423
},
430-
{
431-
"cell_type": "markdown",
432-
"metadata": {},
433-
"source": [
434-
"![converting from Webp formats to older formats](https://sempioneer.com/wp-content/uploads/2020/06/converting-webp-format-to-jpg-png-with-python.png)"
435-
]
436-
},
437-
{
438-
"cell_type": "markdown",
439-
"metadata": {},
440-
"source": [
441-
"------------------------"
442-
]
443-
},
444424
{
445425
"cell_type": "markdown",
446426
"metadata": {},
@@ -477,7 +457,7 @@
477457
"name": "stdout",
478458
"output_type": "stream",
479459
"text": [
480-
"These are all of the files in our current working directory: ['.DS_Store', 'converting-from-webp-to-png-format.png', 'new-format-image-from-png.webp', 'converting-from-webp-to-jpg-format.jpg', 'new-format-image-from-jpeg.webp', '.ipynb_checkpoints', 'example-1.png', 'new-image.jpg', 'example-2.jpg', 'new-image.png', 'changing-image-types-and-next-generation-formats.ipynb']\n"
460+
"These are all of the files in our current working directory: ['.DS_Store', 'converting-from-webp-to-png-format.png', 'new-format-image-from-png.webp', 'changing-image-types-and-next-generation-formats-starter-code.ipynb', 'converting-from-webp-to-jpg-format.jpg', 'new-format-image-from-jpeg.webp', '.ipynb_checkpoints', 'example-1.png', 'new-image.jpg', 'example-2.jpg', 'new-image.png', 'changing-image-types-and-next-generation-formats.ipynb']\n"
481461
]
482462
}
483463
],
@@ -626,7 +606,7 @@
626606
}
627607
],
628608
"source": [
629-
"# Deleting any files that contain the sring Converted\n",
609+
"# Deleting any files that contain the string Converted\n",
630610
"[os.remove(file) for file in os.listdir() if 'Converted' in file]"
631611
]
632612
},
@@ -641,7 +621,7 @@
641621
},
642622
{
643623
"cell_type": "code",
644-
"execution_count": 64,
624+
"execution_count": 19,
645625
"metadata": {},
646626
"outputs": [
647627
{
@@ -662,7 +642,7 @@
662642
"[None, None, None, None, None, None]"
663643
]
664644
},
665-
"execution_count": 64,
645+
"execution_count": 19,
666646
"metadata": {},
667647
"output_type": "execute_result"
668648
}
@@ -688,7 +668,7 @@
688668
},
689669
{
690670
"cell_type": "code",
691-
"execution_count": 18,
671+
"execution_count": 20,
692672
"metadata": {},
693673
"outputs": [],
694674
"source": [
@@ -700,13 +680,14 @@
700680
},
701681
{
702682
"cell_type": "code",
703-
"execution_count": 24,
683+
"execution_count": 21,
704684
"metadata": {},
705685
"outputs": [
706686
{
707687
"name": "stdout",
708688
"output_type": "stream",
709689
"text": [
690+
"changing-image-types-and-next-generation-formats-starter-code.ipynb\r\n",
710691
"changing-image-types-and-next-generation-formats.ipynb\r\n",
711692
"example-1.png\r\n",
712693
"example-2.jpg\r\n"
@@ -742,7 +723,7 @@
742723
},
743724
{
744725
"cell_type": "code",
745-
"execution_count": 37,
726+
"execution_count": 22,
746727
"metadata": {},
747728
"outputs": [],
748729
"source": [
@@ -769,7 +750,7 @@
769750
},
770751
{
771752
"cell_type": "code",
772-
"execution_count": 41,
753+
"execution_count": 23,
773754
"metadata": {},
774755
"outputs": [
775756
{
@@ -786,7 +767,7 @@
786767
"[None, None]"
787768
]
788769
},
789-
"execution_count": 41,
770+
"execution_count": 23,
790771
"metadata": {},
791772
"output_type": "execute_result"
792773
}
@@ -813,7 +794,7 @@
813794
},
814795
{
815796
"cell_type": "code",
816-
"execution_count": 42,
797+
"execution_count": 24,
817798
"metadata": {},
818799
"outputs": [],
819800
"source": [
@@ -854,7 +835,7 @@
854835
"name": "python",
855836
"nbconvert_exporter": "python",
856837
"pygments_lexer": "ipython3",
857-
"version": "3.7.6"
838+
"version": "3.7.9"
858839
}
859840
},
860841
"nbformat": 4,

Course/9_converting_images_to_optimised_formats/.ipynb_checkpoints/changing-image-types-and-next-generation-formats-starter-code-checkpoint.ipynb

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"\n",
2323
"- To learn how to convert a single image into different image formats such as .png, .jpg and .Webp\n",
2424
"- To learn how to convert multiple .png & .jpg images within the current working directory into .Webp images.\n",
25-
"- To Learn how to combine image resizing & convertingimages into the .Webp format."
25+
"- To Learn how to combine image resizing & converting images into the .Webp format."
2626
]
2727
},
2828
{
@@ -66,7 +66,7 @@
6666
"\n",
6767
"All of the above formats have <strong> better compression and are higher in quality </strong> when compared to their previous PNG and JPEG counterparts. \n",
6868
"\n",
69-
"Encoding your images into one of the above formats ensures that your images will use less data, cost you less money and will allow your applications to load faster for your users."
69+
"Encoding your images into one of the above formats ensures that your images will use less data, cost you less money and ensures your applications load faster for your users."
7070
]
7171
},
7272
{
@@ -304,14 +304,7 @@
304304
"cell_type": "markdown",
305305
"metadata": {},
306306
"source": [
307-
"If you check your current working directory, you'll now see two extra images:"
308-
]
309-
},
310-
{
311-
"cell_type": "markdown",
312-
"metadata": {},
313-
"source": [
314-
"![converting images in python](https://sempioneer.com/wp-content/uploads/2020/06/image-conversion-in-python.png)"
307+
"If you check your current working directory, you'll now see two extra images."
315308
]
316309
},
317310
{
@@ -391,20 +384,6 @@
391384
"--------------------------------------------"
392385
]
393386
},
394-
{
395-
"cell_type": "markdown",
396-
"metadata": {},
397-
"source": [
398-
"![converting from Webp formats to older formats](https://sempioneer.com/wp-content/uploads/2020/06/converting-webp-format-to-jpg-png-with-python.png)"
399-
]
400-
},
401-
{
402-
"cell_type": "markdown",
403-
"metadata": {},
404-
"source": [
405-
"------------------------"
406-
]
407-
},
408387
{
409388
"cell_type": "markdown",
410389
"metadata": {},
@@ -474,17 +453,38 @@
474453
},
475454
{
476455
"cell_type": "code",
477-
"execution_count": null,
456+
"execution_count": 14,
478457
"metadata": {},
479458
"outputs": [],
480-
"source": []
459+
"source": [
460+
"# Defining a Python user-defined exception\n",
461+
"class Error(Exception):\n",
462+
" \"\"\"Base class for other exceptions\"\"\"\n",
463+
" pass"
464+
]
481465
},
482466
{
483467
"cell_type": "code",
484-
"execution_count": null,
468+
"execution_count": 15,
485469
"metadata": {},
486470
"outputs": [],
487-
"source": []
471+
"source": [
472+
"def convert_image(image_path, image_type):\n",
473+
" # 1. Opening the image:\n",
474+
" im = Image.open(image_path)\n",
475+
" # 2. Converting the image to RGB colour:\n",
476+
" im = im.convert('RGB')\n",
477+
" # 3. Spliting the image path (to avoid the .jpg or .png being part of the image name):\n",
478+
" image_name = image_path.split('.')[0]\n",
479+
" print(f\"This is the image name: {image_name}\")\n",
480+
" \n",
481+
" # Saving the images based upon their specific type:\n",
482+
" if image_type == 'jpg' or image_type == 'png':\n",
483+
" im.save(f\"Converted-to-next-gen-format-{image_name}.webp\", 'webp')\n",
484+
" else:\n",
485+
" # Raising an error if we didn't get a jpeg or png file type!\n",
486+
" raise Error"
487+
]
488488
},
489489
{
490490
"cell_type": "code",
@@ -583,10 +583,30 @@
583583
},
584584
{
585585
"cell_type": "code",
586-
"execution_count": null,
586+
"execution_count": 22,
587587
"metadata": {},
588588
"outputs": [],
589-
"source": []
589+
"source": [
590+
"def convert_image(image_path, image_type, custom_size=300):\n",
591+
" # 1. Opening the image:\n",
592+
" im = Image.open(image_path)\n",
593+
" # 2. Converting the image to RGB colour:\n",
594+
" im = im.convert('RGB')\n",
595+
" # 3. Spliting the image path (to avoid the .jpg or .png being part of the image name):\n",
596+
" image_name = image_path.split('.')[0]\n",
597+
" print(f\"This is the image name: {image_name}\")\n",
598+
" \n",
599+
" # Saving the images based upon their specific type:\n",
600+
" if image_type == 'jpg' or image_type == 'png':\n",
601+
" if im.size[0] > 600:\n",
602+
" im.thumbnail(size=((custom_size, custom_size)))\n",
603+
" im.save(f\"Converted-to-next-gen-format-{image_name}.webp\", 'webp')\n",
604+
" else:\n",
605+
" im.save(f\"Converted-to-next-gen-format-{image_name}.webp\", 'webp')\n",
606+
" else:\n",
607+
" # Raising an error if we didn't get a jpeg or png file type!\n",
608+
" raise Error"
609+
]
590610
},
591611
{
592612
"cell_type": "code",
@@ -647,7 +667,7 @@
647667
"name": "python",
648668
"nbconvert_exporter": "python",
649669
"pygments_lexer": "ipython3",
650-
"version": "3.7.6"
670+
"version": "3.7.9"
651671
}
652672
},
653673
"nbformat": 4,

0 commit comments

Comments
 (0)