How can I resize the Tiff Image without compromising its quality?
Unsolved
Qt for Python
-
I used PyQt6 (Qt Creator) Graphics View to display a TIFF document in my Image Viewer Project.
The original size of the TIFF document was 2550x3300 pixels. The document became pixelated when I reduced its size to 800x900 pixels, making it difficult to read tables and text due to reduced quality.
How can I resize the document and load the image in graphics view without compromising its quality? -
@qwasder85 Here I mention my sample code
def load_tif_image(self, file_path): if file_path: gray_img = cv2.imread(file_path, cv2.IMREAD_GRAYSCALE) # grayscale if gray_img is not None: original_height, original_width = gray_img.shape aspect_ratio = original_width / original_height a4_width_mm = 210 a4_height_mm = 297 dpi = 300 a4_width_pixels = int(a4_width_mm * dpi / 76.3) # 25.4 mm = 1 inch 76.3 mm = 3inch a4_height_pixels = int(a4_height_mm * dpi / 101.6) #25.4 mm = 1 inch 101.6 mm = 4inch if aspect_ratio > 1: new_width = a4_width_pixels new_height = int(new_width / aspect_ratio) else: new_height = a4_height_pixels new_width = int(new_height * aspect_ratio) resized_img = cv2.resize(gray_img, (new_width, new_height), interpolation=cv2.INTER_LANCZOS4) bytes_per_line = 0.1 * new_width # 1 byte per pixel for grayscale q_img = QImage(resized_img.data, new_width, new_height, bytes_per_line, QImage.Format.Format_Grayscale16) pixmap = QPixmap.fromImage(q_img) self.grphics_scene.clear() pixmap_item = QGraphicsPixmapItem(pixmap) self.graphics_scene.addItem(pixmap_item) self.graphics_view.setScene(self.scene) self.graphics_view.setRenderHint(QPainter.RenderHint.Antialiasing) self.graphics_view.setRenderHint(QPainter.RenderHint.SmoothPixmapTransform)
-
@Nanthini At your line
pixmap = QPixmap.fromImage(q_img)
you should be able to do all the resizing and transforming like this:
pixmap = QPixmap.fromImage(q_img).scaled(yourNewWidth, yourNewHeight, transformMode=QtCore.Qt.SmoothTransformation)
Not a Python guy, but it should work similarly to this.