Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How can I resize the Tiff Image without compromising its quality?
Forum Updated to NodeBB v4.3 + New Features

How can I resize the Tiff Image without compromising its quality?

Scheduled Pinned Locked Moved Unsolved Qt for Python
5 Posts 2 Posters 702 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nanthini
    wrote on last edited by
    #1

    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?

    qwasder85Q 1 Reply Last reply
    0
    • N Nanthini

      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?

      qwasder85Q Offline
      qwasder85Q Offline
      qwasder85
      wrote on last edited by qwasder85
      #2

      @Nanthini If you're showing your TIF in a QPixmap, you can use the QPixmap::scaled()-function and pass Qt::SmoothTransformation for the transform mode. This does soften the look of it a bit, though.

      N 1 Reply Last reply
      0
      • qwasder85Q qwasder85

        @Nanthini If you're showing your TIF in a QPixmap, you can use the QPixmap::scaled()-function and pass Qt::SmoothTransformation for the transform mode. This does soften the look of it a bit, though.

        N Offline
        N Offline
        Nanthini
        wrote on last edited by
        #3

        @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)
        
        qwasder85Q 1 Reply Last reply
        0
        • N Nanthini

          @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)
          
          qwasder85Q Offline
          qwasder85Q Offline
          qwasder85
          wrote on last edited by
          #4

          @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.

          N 1 Reply Last reply
          1
          • qwasder85Q qwasder85

            @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.

            N Offline
            N Offline
            Nanthini
            wrote on last edited by
            #5

            @qwasder85 I'm grateful for the solution you shared; it worked perfectly. Thank you!

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved