Skip to content

Commit

Permalink
Add resize widget feature based on selected image-gif aspect ratio
Browse files Browse the repository at this point in the history
  • Loading branch information
odest committed Jun 16, 2024
1 parent c447660 commit a72fc48
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions src/menu/backgroundPage.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,26 @@ def showFileDialog(self):
self.parent.backgroundTopGif = self.parent.backgroundCustomGifs[self.hash]["top"]
self.parent.backgroundAnimationCounter = 1

self.__showInfoBar("success", "SUCCESS", "Selected gif update successfully.")
gif = Image.open(gifPath)
width, height = gif.size
aspect_ratio = width / height

if aspect_ratio < 1:
self.parent.windowWidth = 150
self.parent.windowHeight = int(150 / aspect_ratio)
elif aspect_ratio == 1:
self.parent.windowWidth = 266
self.parent.windowHeight = 266
elif aspect_ratio > 1:
self.parent.windowWidth = int(150 * aspect_ratio)
self.parent.windowHeight = 150

self.parent.resize(self.parent.windowWidth, self.parent.windowHeight)
self.parent.updateFontMetrics()
self.parent.update()
self.update()

self.__showInfoBar("success", "SUCCESS", "Selected gif update successfully.")

except Exception as e:
self.__showInfoBar("error", "ERROR", "Please select a valid gif file!")
Expand All @@ -248,6 +267,10 @@ def showFileDialog(self):
os.mkdir(targetFolder)
copiedImagePath = self.__copySelectedFile(imagePath, targetFolder)

image = Image.open(imagePath)
width, height = image.size
aspect_ratio = width / height

if copiedImagePath:
self.parent.backgroundImagePath = "src/assets/images/custom/"
self.parent.backgroundTopImage = None
Expand All @@ -256,8 +279,6 @@ def showFileDialog(self):
self.parent.topLayer.setStyleSheet(f"background-color: transparent; border-image: url('{self.parent.backgroundImagePath}{self.parent.backgroundTopImage}'); border-radius:{self.parent.backgroundBorderRadius}px;")
self.parent.backgroundBlurImage = f"blur{self.parent.backgroundNormalImage}"

image = Image.open(imagePath)

if image.format == "PNG":
convertedImage = image.convert('RGB')
bluredImage = convertedImage.filter(ImageFilter.GaussianBlur(10))
Expand All @@ -274,9 +295,25 @@ def showFileDialog(self):
self.parent.topLayer.setStyleSheet(f"background-color: transparent; border-image: url('{self.parent.backgroundImagePath}{self.parent.backgroundTopImage}'); border-radius:{self.parent.backgroundBorderRadius}px;")
self.parent.backgroundBlurImage = f"blur{self.parent.backgroundNormalImage}"

if aspect_ratio < 1:
self.parent.windowWidth = 150
self.parent.windowHeight = int(150 / aspect_ratio)
elif aspect_ratio == 1:
self.parent.windowWidth = 266
self.parent.windowHeight = 266
elif aspect_ratio > 1:
self.parent.windowWidth = int(150 * aspect_ratio)
self.parent.windowHeight = 150

self.parent.resize(self.parent.windowWidth, self.parent.windowHeight)
self.parent.updateFontMetrics()
self.parent.update()
self.update()

self.__showInfoBar("success", "SUCCESS", "Selected image applied successfully.")

except Exception as e:
print(e)
self.__showInfoBar("error", "ERROR", "Please select a valid image file!")


Expand Down

0 comments on commit a72fc48

Please sign in to comment.