Overblog Tous les blogs Top blogs Emploi, Enseignement & Etudes Tous les blogs Emploi, Enseignement & Etudes
Editer l'article Suivre ce blog Administration + Créer mon blog
MENU
http://yoir.over-blog.com/

yoir.over-blog.com/

Publicité

Image Resize Python



Here we'll be doing many operations on an image using OpenCV functions. To make this session interactive image of Thanos(A character from a famous movie ) is being taken for example.

Various operations of OpenCV in Python

The various operation goes as follows:

Importing the required libraries required for the program

Numpy.resize¶ numpy.resize (a, newshape) source ¶ Return a new array with the specified shape. If the new array is larger than the original array, then the new array is filled with repeated copies of a. Note that this behavior is different from a.resize(newshape) which fills with zeros instead of repeated copies of a. Parameters a arraylike. Narrator Content-aware image resizing, is when you change the size of an image, taking into account what's in that image. That means different images are resized differently. Seam carving is one implementation of content aware resizing, in which an uninteresting sequence of pixels is removed from the image. Let's motivate content-area resizing with an image. Resize an image. Parameters: arr: ndarray. The array of image to be resized. Size: int, float or tuple. Int - Percentage of current size. Float - Fraction of current.

cv2 is the OpenCV library and the numpy library is included as many times the image will be treated as an array.

Reading and displaying of an image – OpenCV Python

How to resize image in python tkinter. Python by Handsome Hedgehog on May 05 2020 Donate. 0 Source: stackoverflow.com. Delphi queries related to 'how to resize image in python tkinter' reshape image size tkinter; how to make image size changer using tkinter; resize image in canvas python; image size python tkinter. Free photo resizer and image compressor to crop, resize images in JPEG PNG GIF format to the exact pixels or proportion you specified, compress them to reduce the file sizes, making it easy to use them as your desktop wallpaper, Facebook cover.

cv2.imread() is a function which takes image name as a parameter(provided the image is saved in the same folder where the program is or else just give the path to the image) and cv2.imshow() display the image with name and its parameters are (‘name_of_the_window',variable_in_which_image _is_stored). The output is given as:

Printing the dimensions of an image in OpenCV

Normally, the dimensions are width*height but OpenCV takes as height*width. The output will be as: (175, 289, 3) as displayed for my image. The values are (height, width, channel) where the channel is the RGB components.

Resizing of an image in Python with OpenCV

As seen in code the height and width are specified as 300. Both values are then inserted into the variable called dim(dimension of new image). The third line uses the function cv2.resize() which actually does the main work of changing the size. The parameters are the original image, dimension, and the algorithm to be used for this purpose. For time being we are not going to focus on the algorithm but sticking to the implementation part. The output after this is:

Rotation of an image in OpenCV

As we know now image.shape() returns a tuple up to indexing 2 where the first two values are height and width hence the first line extracts the values of height and width, We then discover the center coordinates by moving towards half of both height and width. The function cv2.RotationMatrix2D returns a matrix that contains the image of rotated coordinates. The parameters are center, angle_of_rotation(here we are rotating for an angle of 180 degrees) and scaling factor. Later the transformation is done by cv2.warpAffine() function whose parameters are the original_image, obtained matrix and the dimension of the rotated image. The output is:

Cropping of an image in Python

The first line returns that part of the image which starts and ends between (beginX:endX), (beginY,endY). This crop the image. Just for fun, we crop to get the Thanos's gauntlet. The output is:

The basics of OpenCV ends here.

  • Related Questions & Answers
  • Selected Reading
PythonServer Side ProgrammingProgramming

Python provides lots of libraries for image processing, including −

Resize
  • OpenCV − Image processing library mainly focused on real-time computer vision with application in wide-range of areas like 2D and 3D feature toolkits, facial & gesture recognition, Human-computer interaction, Mobile robotics, Object identification and others.

  • Numpy and Scipy libraries − For image manipuation and processing.

  • Sckikit − Provides lots of alogrithms for image processing.

  • Python Imaging Library (PIL) − To perform basic operations on images like create thumnails, resize, rotation, convert between different file formats etc.

In this section we are going to see some basics of image processing in python.

Install required library

Our first step will be to install the required library, like openCV, pillow or other which we wants to use for image processing. We can use pip to install the required library, like −

That's it: now we can play with our image.

Image: Open() and show()

First, open the file/image and show. You can rotate the image while showing like below −

Output

As the above variable im, is a pillow object. We can retreive some information about the opened image −

Convert and Save() Image

We can change the format of image from one form to another, like below −

Now if we see the folder, we have same image in two different formats.

Resize-thumbnails()

We can change the size of image using thumbnail() method of pillow −

The image will change as follows:

Converting to grayscale image − convert()

We can make the grayscale image from our original colored image.

Where 'L' stands for 'luminous'.


Above example is from the PIL library of python. Musiclab realguitar v5 0 1 7388. We can use other library like open-cv, matplotlib & numpy for image processing. Below are some of the example program to demonstrate the use of much powerful library for image processing.

Showing image in grayscale

Output

Another way to write above program with a tick/line to mark the image.

Output

numpy.resize(a, new_shape)[source]

Return a new array with the specified shape.

If the new array is larger than the original array, then the newarray is filled with repeated copies of a. Note that this behavioris different from a.resize(new_shape) which fills with zeros insteadof repeated copies of a.

Parameters
aarray_like

Array to be resized.

new_shapeint or tuple of int
Image Resize Python Opencv

Shape of resized array.

Returns
reshaped_arrayndarray

The new array is formed from the data in the old array, repeatedif necessary to fill out the required number of elements. Thedata are repeated in the order that they are stored in memory.

Resize Image Python Tkinter

Notes

Python
Image Resize Python Pil Code

Warning: This functionality does not consider axes separately,i.e. it does not apply interpolation/extrapolation.It fills the return array with the required number of elements, takenfrom a as they are laid out in memory, disregarding strides and axes.(This is in case the new shape is smaller. For larger, see above.)This functionality is therefore not suitable to resize images,or data where each axis represents a separate and distinct entity.

Image Resize Python Numpy

Examples





Publicité
Partager cet article
Repost0
Pour être informé des derniers articles, inscrivez vous :
Commenter cet article