1
votes

I have an image that i need to reduce it's it resolution without in an approach other than resizing. I tried to change the Dpi but apparently my image doesn't have a DPI (i don't know if that is possible). I want my image with low resolution for an object detection code. Moreover, i tried using filters such as "GuassianBlur" and other image processing approaches for a better detection however, i want a solution for lowering my image resolution.

im = Image.open("car.png")
im.save("Dpi_test.png", dpi=(10, 10))

This was the code i used for dpi, when i tried to print the dpi using this function, it gave me KeyError:

print(im.info['dpi'])

For more information these are the results i wish for:

Click here for sample image.

Any help in how can i reduce the resolution of an image would be more than great! Thank you

Update!

An edited clarification concerning my question: After the help of one of the commenters he suggested to blur and decimate my image, does anyone has a way to do that? I guess it is something related to down-sampling! Not sure thou!

Update 2 !! Question answered with 2 approaches, big thanks!

2
Low resolution doesnt give you accurate solution but the oppositely inaccurate solutions. Low resolution just increase the speed.Yunus Temurlenk
@PythonCoder100 Welcome to Stack Overflow! Please take the tour and read How to Ask. Please edit the post to include your own effort into solving this problem. The latter preferably in code, this is called a minimal reproducible example.Bilal
@YunusTemurlenk Yes!! How can i lower the resolution thou? any help?Paola Mk
@BelalHomaidan Thank u for the advicePaola Mk
What do you call image resolution, exactly ?Yves Daoust

2 Answers

2
votes

Using PIL, I'm reducing the image to a thumbnail and then transforming it back to the original size:

from PIL import Image
im = Image.open("1jP94.png")
orig_size = im.size
print(orig_size)
display(im)

==> (142, 155)

enter image description here

im.thumbnail([32, 32])
display(im)

enter image description here

im = im.transform(orig_size, Image.EXTENT, (0,0, 32, 32))
display(im)

enter image description here

0
votes

For the ones that want to implement this on OpenCV:

Using the PIL to reduce the image to a thumbnail and then transforming it back to the original size as answered above, this is the same code as above in addition to converting PIL images to OpenCV images:

from PIL import Image
import numpy
import cv2

im = Image.open("image.png")
orig_size = im.size
print(orig_size)
open_cv_image = numpy.array(im)
open_cv_image = open_cv_image[:, :, ::-1].copy()
cv2.imshow("original", open_cv_image)
im.thumbnail([32, 32])
open_cv_image = numpy.array(im)
open_cv_image = open_cv_image[:, :, ::-1].copy()
cv2.imshow("thumbnail", open_cv_image)
im = im.transform(orig_size, Image.EXTENT, (0,0, 32, 32))
open_cv_image = numpy.array(im)
open_cv_image = open_cv_image[:, :, ::-1].copy()
cv2.imshow("result", open_cv_image)
cv2.waitKey(0)