I need to increase dpi my image before read by ocr with opencv. the problem are :
i search in google, and almost every answer suggest using resize
cv2.resize()
image = cv2.imread("source.png")
resized_image = cv2.resize(image, (100, 50)) #I need to change it to 300 DPI
resize
only to change size of image, but after all not increase dpi in image, because i tried use it, and when i check in photoshop, dpi not changed
show please help me for that 2 questions
how to do it with opencv?
honestly I need to change dpi to 300, why i need to know current dpi? because if it already dpi > 300
, so I dont need to convert it.
oya i do it with python
To change an image's DPI in Photoshop, go to Image > Image Size. Uncheck Resample Image, because this setting will upscale your image, which will make it lower quality. Now, next to Resolution, type in your preferred resolution, set as Pixels/Inch.
The first step is to create an object of the DNN superresolution class. This is followed by the reading and setting of the model, and finally, the image is upscaled. We have provided the Python and C++ codes below. You can replace the value of the model_path variable with the path of the model that you want to use.
Let’s begin by taking a look at the OpenCV resize () function syntax. Notice that only two input arguments are required: The source image. The desired size of the resized image, dsize. We will discuss the various input argument options in the sections below.
I searched in Google, and almost every answer suggests using cv2.resize resize only changes the size of image, but after all does not increase the dpi. I tried to use it, and then checked in Photoshop, the dpi was not changed. How to do it with opencv? I need to change dpi to 300, why do I need to know current dpi?
It also helps in zooming in images. Many times we need to resize the image i.e. either shrink it or scale up to meet the size requirements. OpenCV provides us several interpolation methods for resizing an image. cv2.INTER_AREA: This is used when we need to shrink an image. cv2.INTER_CUBIC: This is slow but more efficient.
Vectorize the algorithm/code to the maximum extent possible, because Numpy and OpenCV are optimized for vector operations. Exploit the cache coherence. Never make copies of an array unless it is necessary. Try to use views instead.
The dpi is just a number in the JPEG/TIFF/PNG header. It is entirely irrelevant to the world and his dog until you print the image and then it determines how large the print will be given the image's dimensions in pixels.
During image processing, it is irrelevant. The only thing of any interest is the number of pixels you have. That is the ultimate determinant of image quality, or information content - however you want to describe it.
I don't believe you can set it with OpenCV. You can certainly set it with ImageMagick like this in the Terminal:
mogrify -set density 300 *.png # v6 ImageMagick
magick mogrify -set density 300 *.png # v7 ImageMagick
You can check it with:
identify -format "Density: %x x %y" SomeImage.jpg # v6 ImageMagick
magick identify -format ... as above # v7 ImageMagick
You can do similar things with exiftool
in Terminal - note that exiftool
is MUCH smaller and easier to maintain than ImageMagick because it is "just" a (very capable) single Perl script:
Extract image resolution from EXIF IFD1 information:
exiftool -IFD1:XResolution -IFD1:YResolution image.jpg
Extract all tags with names containing the word "Resolution" from an image|:
exiftool '-*resolution*' image.jpg
Set X/Y Resolution (density) on image.jpg
:
exiftool -xresolution=300 -yresolution=300 image.jpg
Here is a little demonstration of what I mean at the beginning of my answer...
Use ImageMagick to create an image 1024x768 with no dpi information:
convert -size 1024x768 xc:black image.jpg
Now examine it:
identify -verbose image.jpg
Image: image.jpg
Format: JPEG (Joint Photographic Experts Group JFIF format)
Mime type: image/jpeg
Class: PseudoClass
Geometry: 1024x768+0+0
Units: Undefined
Colorspace: Gray
Type: Bilevel
...
...
Now change the dpi and set the dpi units and examine it again:
mogrify -set density 300 -units pixelsperinch image.jpg # Change dpi
identify -verbose image.jpg # Examine
Image: image.jpg
Format: JPEG (Joint Photographic Experts Group JFIF format)
Mime type: image/jpeg
Class: PseudoClass
Geometry: 1024x768+0+0 <--- Number of pixels is unchanged
Resolution: 300x300 <---
Print size: 3.41333x2.56 <--- Print size is now known
Units: PixelsPerInch <---
Colorspace: Gray
Type: Bilevel
...
...
And now you can see that suddenly we know how big a print will come out and that the number of pixels has not changed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With