|
22 | 22 | "\n",
|
23 | 23 | "- To learn how to convert a single image into different image formats such as .png, .jpg and .Webp\n",
|
24 | 24 | "- 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." |
26 | 26 | ]
|
27 | 27 | },
|
28 | 28 | {
|
|
66 | 66 | "\n",
|
67 | 67 | "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",
|
68 | 68 | "\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." |
70 | 70 | ]
|
71 | 71 | },
|
72 | 72 | {
|
|
304 | 304 | "cell_type": "markdown",
|
305 | 305 | "metadata": {},
|
306 | 306 | "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 |
| - "" |
| 307 | + "If you check your current working directory, you'll now see two extra images." |
315 | 308 | ]
|
316 | 309 | },
|
317 | 310 | {
|
|
391 | 384 | "--------------------------------------------"
|
392 | 385 | ]
|
393 | 386 | },
|
394 |
| - { |
395 |
| - "cell_type": "markdown", |
396 |
| - "metadata": {}, |
397 |
| - "source": [ |
398 |
| - "" |
399 |
| - ] |
400 |
| - }, |
401 |
| - { |
402 |
| - "cell_type": "markdown", |
403 |
| - "metadata": {}, |
404 |
| - "source": [ |
405 |
| - "------------------------" |
406 |
| - ] |
407 |
| - }, |
408 | 387 | {
|
409 | 388 | "cell_type": "markdown",
|
410 | 389 | "metadata": {},
|
|
474 | 453 | },
|
475 | 454 | {
|
476 | 455 | "cell_type": "code",
|
477 |
| - "execution_count": null, |
| 456 | + "execution_count": 14, |
478 | 457 | "metadata": {},
|
479 | 458 | "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 | + ] |
481 | 465 | },
|
482 | 466 | {
|
483 | 467 | "cell_type": "code",
|
484 |
| - "execution_count": null, |
| 468 | + "execution_count": 15, |
485 | 469 | "metadata": {},
|
486 | 470 | "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 | + ] |
488 | 488 | },
|
489 | 489 | {
|
490 | 490 | "cell_type": "code",
|
|
583 | 583 | },
|
584 | 584 | {
|
585 | 585 | "cell_type": "code",
|
586 |
| - "execution_count": null, |
| 586 | + "execution_count": 22, |
587 | 587 | "metadata": {},
|
588 | 588 | "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 | + ] |
590 | 610 | },
|
591 | 611 | {
|
592 | 612 | "cell_type": "code",
|
|
647 | 667 | "name": "python",
|
648 | 668 | "nbconvert_exporter": "python",
|
649 | 669 | "pygments_lexer": "ipython3",
|
650 |
| - "version": "3.7.6" |
| 670 | + "version": "3.7.9" |
651 | 671 | }
|
652 | 672 | },
|
653 | 673 | "nbformat": 4,
|
|
0 commit comments