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. crop the image
Forum Updated to NodeBB v4.3 + New Features

crop the image

Scheduled Pinned Locked Moved Unsolved Qt for Python
5 Posts 3 Posters 495 Views 1 Watching
  • 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.
  • M Offline
    M Offline
    Mahdi_2020
    wrote on last edited by
    #1

    My crop function works fine when the photo is in normal mode, but when I zoom in or zoom out and want to crop, it does the crop on the original image.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Without your code it's not possible to know what you did wrong beside the fact that your are changing the original image rather than applying your changes to the current image.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        Without your code it's not possible to know what you did wrong beside the fact that your are changing the original image rather than applying your changes to the current image.

        M Offline
        M Offline
        Mahdi_2020
        wrote on last edited by Mahdi_2020
        #3

        @SGaist
        code_text

        @Slot()
            def zoom_in(self):
                self.scale = self.scale + 0.1
                root_object.findChild(QObject, "image").setProperty("scale", self.scale)
                new_height, new_width = int(self.image.shape[0] * self.scale), int(self.image.shape[1] * self.scale)
                self.modified_image = cv2.resize(self.image, (new_width, new_height))
                self.update_qml()
        
        
        @Slot()
            def zoom_out(self):
                if self.scale > 1.0:
                    self.scale -= 0.1
                    root_object.findChild(QObject, "image").setProperty("scale", self.scale)
                    new_height, new_width = int(self.image.shape[0] * self.scale), int(self.image.shape[1] * self.scale)
                    self.modified_image = cv2.resize(self.image, (new_width, new_height))
                    self.update_qml()
        
        @Slot(int, int, int, int)
            def crop(self, x, y, width, height):
                if self.modified_image is not None:
                    img_height, img_width = self.modified_image.shape[:2]
                    qml_image = root_object.findChild(QObject, "image")
                    qml_image_width = qml_image.property("width")
                    qml_image_height = qml_image.property("height")
                    aspect_ratio_image = img_width / img_height
                    aspect_ratio_qml = qml_image_width / qml_image_height
        
                    if aspect_ratio_image > aspect_ratio_qml:
                        scale = qml_image_width / img_width
                        offset_y = (qml_image_height - img_height * scale) / 2
                        real_x = int(x / scale)
                        real_y = int((y - offset_y) / scale)
                    else:
                        scale = qml_image_height / img_height
                        offset_x = (qml_image_width - img_width * scale) / 2
                        real_x = int((x - offset_x) / scale)
                        real_y = int(y / scale)
        
                    real_width = int(width / scale)
                    real_height = int(height / scale)
        
                    if real_x < 0 or real_y < 0 or real_x + real_width > img_width or real_y + real_height > img_height:
                        print("Error: Cropping area is out of bounds!")
        
        jsulmJ 1 Reply Last reply
        0
        • M Mahdi_2020

          @SGaist
          code_text

          @Slot()
              def zoom_in(self):
                  self.scale = self.scale + 0.1
                  root_object.findChild(QObject, "image").setProperty("scale", self.scale)
                  new_height, new_width = int(self.image.shape[0] * self.scale), int(self.image.shape[1] * self.scale)
                  self.modified_image = cv2.resize(self.image, (new_width, new_height))
                  self.update_qml()
          
          
          @Slot()
              def zoom_out(self):
                  if self.scale > 1.0:
                      self.scale -= 0.1
                      root_object.findChild(QObject, "image").setProperty("scale", self.scale)
                      new_height, new_width = int(self.image.shape[0] * self.scale), int(self.image.shape[1] * self.scale)
                      self.modified_image = cv2.resize(self.image, (new_width, new_height))
                      self.update_qml()
          
          @Slot(int, int, int, int)
              def crop(self, x, y, width, height):
                  if self.modified_image is not None:
                      img_height, img_width = self.modified_image.shape[:2]
                      qml_image = root_object.findChild(QObject, "image")
                      qml_image_width = qml_image.property("width")
                      qml_image_height = qml_image.property("height")
                      aspect_ratio_image = img_width / img_height
                      aspect_ratio_qml = qml_image_width / qml_image_height
          
                      if aspect_ratio_image > aspect_ratio_qml:
                          scale = qml_image_width / img_width
                          offset_y = (qml_image_height - img_height * scale) / 2
                          real_x = int(x / scale)
                          real_y = int((y - offset_y) / scale)
                      else:
                          scale = qml_image_height / img_height
                          offset_x = (qml_image_width - img_width * scale) / 2
                          real_x = int((x - offset_x) / scale)
                          real_y = int(y / scale)
          
                      real_width = int(width / scale)
                      real_height = int(height / scale)
          
                      if real_x < 0 or real_y < 0 or real_x + real_width > img_width or real_y + real_height > img_height:
                          print("Error: Cropping area is out of bounds!")
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Mahdi_2020 Please format your code properly...

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Where in that code is the cropping done ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • SGaistS SGaist referenced this topic on

            • Login

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