'ValueError: Coordinate 'right' is less than 'left'
while cropping an image with pillow/PIL
, i got the following Error:
ValueError: Coordinate 'right' is less than 'left'
I am not able to resolve why cropping left side more than right would cause a problem.
The code below crops the image according to the widget size in tkinter. After several run it works as desired but on the next it raises the same error.
import tkinter as tk
from math import sqrt
from PIL import Image, ImageTk
class Transparent:
# Only works when image is placed
def __init__(self, widget, root, img_path):
root.update()
self.path = img_path
self.widget_cod = (widget.winfo_rootx(), widget.winfo_rooty())
self.widget_size = (widget.winfo_width(), widget.winfo_height())
self.root_size = (root.winfo_width(), root.winfo_height())
self.distace_formulae = lambda x2, x1, y2, y1: math.sqrt(
(x2 - x1) ** 2 + (y2 - y1) ** 2
)
self.cal_dis()
self.show()
def cal_dis(self):
self.left = self.distace_formulae(
x1=0, x2=self.widget_cod[0], y1=self.widget_cod[1], y2=self.widget_cod[1]
)
self.top = self.distace_formulae(
x1=self.widget_cod[0], x2=self.widget_cod[0], y1=0, y2=self.widget_cod[1]
)
self.bottom = (
self.distace_formulae(
x1=self.widget_cod[0],
x2=self.widget_cod[0],
y1=self.widget_cod[1],
y2=self.root_size[1],
)
- self.widget_size[1]
)
self.right = (
self.distace_formulae(
x1=self.widget_cod[0],
x2=self.root_size[0],
y1=self.widget_cod[1],
y2=self.widget_cod[1],
)
- self.widget_size[1]
)
def show(self):
self.img = Image.open(self.path)
print(self.left, self.top, self.right, self.bottom)
self.img = self.img.crop((self.left, self.top, self.right, self.bottom))
self.img.show()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|