We know that OpenCV is widely used for working with images and has a wide range of functions for this. But what if we want to process image files without using any external library like OpenCV. Let’s see how we can do that.
Image scaling (using nearest neighbor interpolation):
Nearest neighbor interpolation — this is the easiest way to interpolate. This method simply determines the "closest" neighboring pixel and takes a value for its intensity.
Consider a small image whose width is w and the height is — h that we want to change from width p to width q, assuming p & gt; m and q & gt; n. Now we need two scaling constants:
scale_x = p / w scale_y = q / h
Now we simply iterate over all the pixels in the output image, referring to the original pixels we are copying from. scaling our control variables with scale_x and scale_y , and rounding the resulting scaled index values.
Visual representation:
The image is 3X3 pixels (9 pixels in total), now if we want to increase the image size to 6X6, then according to the nearest neighboring algorithm 6/3 (i.e. 2) pixels should have the same RGB value as the value pixel to the original image.
Image scaling program:
|
Output:
Grayscale the image:
Using the mean value method, this method emphasizes the intensity of a pixel, rather than showing what RGB values ​​it is composed of. When we calculate the average RGB value and assign it the RGB value for a pixel, since the RGB value of the pixel is the same, it will not be able to create any color, since all colors are generated due to a different ratio of RGB values, since in this case the ratio would be 1: 1: 1. Therefore, the rendered image will look like a gray image.
Visual representation:

Grayscale image program:
|
Output:
Cropping an image:
Cropping basically removes unwanted pixel. This can be done by placing the required pixel in another grid of images, the size of which matches the required after cropping.
Consider an image with a size of 10 × 10 pixels, and if we only want to crop the center of an image with a size of 4 × 4 pixels, then we need to collect pixel values ​​from (10-4) / 2, starting at (3, 3) up to 4 pixels in the x direction and 4 pixels in the y direction .
Visual representation:

Image cropper:
# using matplotlib and numpy
import
matplotlib.image as img
import
numpy as npy
# read image in variable m
m
=
img.imread (
"taj.png"
)
# definition of image size width (w) height (h)
w, h
=
m.shape [:
2
]
# required image size after cropping
xNew
=
int
(w
*
1
/
4
)
yNew
=
int
(h
*
1
/
4
)
newImage
=
npy.zeros ([xNew, yNew,
4
])
# print width source image height
print
(w)
print
(h)
for
i
in
range
(
1
, xNew):
for
j
in
range
(
1
, yNew):
# crop from 100 to 100 pixels of the original image
newImage [i, j]
=
m [
100
+
i,
100
+
j]
# save the image
img.imsave (
’cropped.png’
, newImage)
Output:
Image Processing Without OpenCV | python __del__: Questions
How can I make a time delay in Python?
5 answers
I would like to know how to put a time delay in a Python script.
Answer #1
import time
time.sleep(5) # Delays for 5 seconds. You can also use a float value.
Here is another example where something is run approximately once a minute:
import time
while True:
print("This prints once a minute.")
time.sleep(60) # Delay for 1 minute (60 seconds).
Answer #2
You can use the sleep()
function in the time
module. It can take a float argument for sub-second resolution.
from time import sleep
sleep(0.1) # Time in seconds
Image Processing Without OpenCV | python __del__: Questions
How to delete a file or folder in Python?
5 answers
How do I delete a file or folder in Python?
Answer #1
os.remove()
removes a file.os.rmdir()
removes an empty directory.shutil.rmtree()
deletes a directory and all its contents.
Path
objects from the Python 3.4+ pathlib
module also expose these instance methods:
pathlib.Path.unlink()
removes a file or symbolic link.pathlib.Path.rmdir()
removes an empty directory.