Cara menggunakan resize png image python

Pada dasarnya banyak cara untuk menggabungkan Dua/lebih gambar menjadi satu file gambar.  Kali ini yang akan dibahas adalah cara menggabungkan dua/lebih file gambar menjadi satu file gambar menggunakan program bawaan windows, yaitu Windows Paint.  

Program Windows Paint adalah program editor bawaan windows baik itu windows XP, windows 7, windows 8 ataupun windows 10. Program Windows Paint ini sudah terpasang otomatis pada sistem operasi Windoes sehingga tidak perlu memasang (install) lagi dari luar. Dengan paint ini kita bisa bebas mengukur gambar besar kecil sesuai dengan selera kemudian menambah gambar sesuai keinginan, bisa satu, dua, tiga, empat sampai sebanyak banyaknya kemudian menata gambar-gambar tersebut menjadi satu bagian yang kemudian bisa kita simpan dengan 8 pilihan format yang berbeda seperti JPG, PNG, GIF, BMP Monochrom, 16, 256, 24-bit bitmap).

Berikut contoh dan langkah-langkah menggabungkan beberapa file gambar menjadi satu file gambar menggunakan program windows paint :

Python is a popular object-oriented programming language for image-related tasks for webpages, visualizations, or when using Python for machine-learning operations through frameworks like OpenCV and Scikit Learn.

Reducing the size of an image means changing its dimensions by removing its pixels. Scaling up an image increases the number of its pixels but lowers quality. Either way, the image’s aspect ratio changes, which results in distortion.

This article describes how to resize images in bulk with the Pillow library, a popular fork of the Python Imaging Library (PIL); and, to maintain the quality and aspect ratio, in OpenCV, a robust library of programming functions for computer vision. Also explained is how to resize and crop Python images with Cloudinary through automation.

Resize Images in Python With Pillow

Pillow is a fork of the Python Imaging Library (PIL) that supports Python 3 and numerous image formats, including PNG, JPEG, TIFF, and PPM. When you load an image from a file, create a new image, or generate separate instances for images, you create an instance of PIL’s Image class.

To resize an image with Pillow’s resize() method:

  1. Import the PIL image class:
  2. Load the image from a file with the open() function:
    1. image = Image.open('myimage.jpg')

    The above command returns an Image object. In case of failure, the command returns an OSError exception.

  3. Call the resize() method on the new image instance, passing a tuple argument with two integers to specify the width and height you desire:
    1. image = Image.open('myimage.jpg')
    2. new_image = image.resize((500, 500))
    3. resize()0

    Note: Instead of modifying the image file, this function returns a separate Image instance with the new dimensions.

The resize() method has two drawbacks, however:

  • Oftentimes, resizing to an exact width and height changes the image’s aspect ratio, leading to distortions.
  • If you set the size of the new instance to be larger than that of the original, resize() “blows up” the instance, reducing its quality.

As a solution, resize the image with the more advanced Pillow method, resize()4:

  1. Perform steps 1 and 2 of the above procedure.
  2. Call the resize()4 method on the Image instance, passing a tuple argument with two integers to specify the width and height you desire:
    1. resize()7
    2. resize()8
    3. resize()9
    4. image0

As shown under image1, the size of the new instance is 400×350 pixels. The aspect ratio of the original image remains unchanged. In addition, if the dimensions of the original are smaller than that specified for the new instance, instead of “blowing up” the image, resize()4 returns an instance of the same size.

Resize Images in Python With OpenCV

OpenCV is an open-source computer-vision library with thousands of machine-learning and deep-learning algorithms for face detection, object recognition, and many other computer-vision tasks. Given that numerous computer-vision models require a certain size and quality level for their images, resizing is critical. To determine which image variation performs best, experiment with different sizes or resolutions.

Here is the full syntax for the resize() method in OpenCV:

image4

The parameters are as follows:

srcThe file path in which the input image resides.dsizeThe size of the output image, which adheres to the syntax image5.fxThe scale factor for the X axis.fyThe scale factor for the Y axis.interpolationThe technique for adding or removing pixels during the resizing process. The default is image6.

Note: Apply either image7 or image8 and image9, or all three.

To perform a simple resizing task with OpenCV:

  1. Import the OpenCV library:
    1. open()0
    2. open()1
  2. Acquire a sample image and specify its current size:
    1. open()2
    2. open()3
    3. open()4
    4. open()5
  3. Resize the image of, say, a size of 800×600 pixels, to 300×300 pixels:
    1. open()6

As in the previous example on resizing images with Pillow’s resize() method, this procedure changes the aspect ratio, causing distortions. To maintain that ratio, run the following command to resize the image to 75% of its width and height:

open()7

In addition, for a resized instance that is larger than the original, you can customize the interpolation of the resize operation. Even though doing that causes quality loss, it might be the right choice for certain computer-vision applications.

Here are the values for the open()8 argument:

image6The standard bilinear interpolation, ideal for enlarged images.image = Image.open('myimage.jpg')0The nearest neighbor interpolation, which, though fast to run, creates blocky images.image = Image.open('myimage.jpg')1The interpolation for the pixel area, which scales down images.image = Image.open('myimage.jpg')2The bicubic interpolation with 4×4-pixel neighborhoods, which, though slow to run, generates high-quality instances.image = Image.open('myimage.jpg')3The Lanczos interpolation with an 8×8-pixel neighborhood, which generates images of the highest quality but is the slowest to run.

Resize and Crop Python Images With Cloudinary Through Automation

A cloud-based service for managing images and videos, Cloudinary offers a generous free-forever subscription plan. While on that platform, you can upload images and apply built-in effects, filters, and modifications.

You can also resize images through automation, focusing on the most important elements with AI, or adapt them to your website design by, for example, specifying the width, height, and aspect ratio as qualifiers for the new image instances. Cloudinary then automatically performs the resizing and cropping tasks to meet the criteria. No manual efforts are required.