crop the image
Unsolved
Qt for Python
-
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.
-
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.
-
@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!")
-
@Mahdi_2020 Please format your code properly...
-
Where in that code is the cropping done ?
-