Python Forum
please help with image resizing and image transparency !
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
please help with image resizing and image transparency !
#1
hello All

I'm using ChatGPT to help me make a program.

๐Ÿ” Tkinter Image Transparency & Resizing Issue (Catch-22 Situation)
We're building a custom UI in Tkinter for a damage simulator project, and we've run into a catch-22 when trying to overlay images with transparent backgrounds โ€” specifically, a frame (frame.png) and a dragon (dragon.png) over a blue background.png.

๐ŸŽฏ Goal:
Display frame.png with a transparent cutout over a combat log box.

Show dragon.png (also transparent) on the right side.

Load a full-screen blue background.png underneath everything.

Ensure all images are resized to fit the GUI layout.

๐Ÿšซ The Core Problem:
Tkinter provides two different ways to load images โ€” and they conflict in what they support:

Method Supports Transparency (Alpha)? Supports .subsample() Resizing?
tk.PhotoImage(...) โŒ Limited / Broken โœ… Yes
ImageTk.PhotoImage(...) โœ… Yes (Full PNG Alpha) โŒ No .subsample() available

So weโ€™re stuck in a loop:
tk.PhotoImage(...).subsample(...) lets us shrink the image, but then loses PNG transparency โ€” it renders transparent areas as black or opaque.

ImageTk.PhotoImage(...) preserves transparency, but does not support .subsample(), and there's no clean way to resize using Tkinter-native tools that respect alpha.

๐Ÿงช Tests We've Done:
Verified that the PNG cutout areas are truly transparent using external editors.

Displayed background.png (solid blue) to check whether images properly reveal what's behind them.

Loaded dragon.png (confirmed transparent) using .subsample() โ€” and it appeared opaque, not transparent.

Tested using .place() and z-ordering, confirming layering works, but transparency does not render with .subsample.

โ“ Why It Matters:
We need to shrink high-res assets to fit a 1600ร—960 GUI layout without breaking transparency, especially for overlays like the metal frame that wraps the combat log window. Currently, no method allows both resizing and transparency in one go using Tkinterโ€™s built-in tools.

๐Ÿ“Œ Summary:
Tkinterโ€™s native PhotoImage supports .subsample() for resizing but kills transparency.
ImageTk.PhotoImage supports PNG alpha but wonโ€™t resize natively.
This creates a catch-22 โ€” we canโ€™t have transparent AND resized image overlays using either method alone.
Reply
#2
just am going to provide 2 Screenshots so you all can geta visual idea of what problems i'm facing.

image


this picture here shows the dragon with a black background, the logo with a white background, and the combat frame (with center cut-out) opaque as well! I confirmed in multiple programs that all three have Alpha channels, RGBA and verified that they indeed have transparent backgrounds.

image
Reply
#3
I don't know if you've tried this:

Do all the resizing and overlaying as an Image in PIL. Use imageTk.PhotoImage to convert the resulting image to a tk compatible image.

Next time post the code you are running.
Reply
#4
(Jul-25-2025, 09:58 PM)deanhystad Wrote: I don't know if you've tried this:

Do all the resizing and overlaying as an Image in PIL. Use imageTk.PhotoImage to convert the resulting image to a tk compatible image.

Next time post the code you are running.


We're trying to load high-res .png images with transparency and shrink them to fit our GUI. This is what we're using now:


frame_image_raw = tk.PhotoImage(file=frame_path).subsample(2, 2)
frame_label = tk.Label(root, image=frame_image_raw, bg="black", bd=0)
frame_label.image = frame_image_raw
frame_label.place(x=600, y=180)

Weโ€™re using .subsample(2, 2) because without it, the full-res image is massively oversized in the GUI โ€” even when the image itself is already 700x760 px. For some reason, either Tkinter, PIL, or Windows display scaling seems to treat the image like it's 2ร— or 3ร— larger than expected unless we manually shrink it using .subsample.

The problem is .subsample() only works with tk.PhotoImage, which breaks transparency โ€” any transparent region in the PNG becomes black or opaque when rendered.

The Catch-22 Weโ€™re Dealing With
tk.PhotoImage: Supports .subsample() โ†’ โœ… resize works, โŒ transparency lost

ImageTk.PhotoImage: Supports full PNG alpha โ†’ โœ… transparency works, โŒ no .subsample() or easy resizing

We've verified our PNGs have proper alpha transparency (confirmed in GIMP and Photoshop), and that the cutout areas are truly empty.

We even tried loading the original-size images directly, but they get blown up and overflow the window โ€” likely due to DPI scaling or how Tkinter interprets native resolution.


What We Need
A reliable way to resize the image to 50% scale

Preserve alpha transparency

Display it in a tk.Label using .place(...) over a background

If we use PILโ€™s .resize(...), it doesn't seem to shrink proportionally when displayed โ€” or the image positioning goes off.

Anyone know the best way to do both without losing transparency or proper GUI alignment?

try:
frame_image_raw = tk.PhotoImage(file=frame_path).subsample(2, 2)
frame_label = tk.Label(root, image=frame_image_raw, bg="black", bd=0)
frame_label.image = frame_image_raw
frame_label.place(x=600, y=180)
except Exception as e:
log_debug(f"[ERROR] Failed to load frame.png: {e}")

try:
dragon_image_raw = tk.PhotoImage(file=dragon_path).subsample(2, 2)
dragon_label = tk.Label(root, image=dragon_image_raw, bg="black", bd=0)
dragon_label.image = dragon_image_raw
dragon_label.place(x=1340, y=100)
except Exception as e:
log_debug(f"[ERROR] Failed to load dragon.png: {e}")
Reply
#5
So, actually, the problem is merging the two images into 1 image?

Can you post the 2 images?
Reply
#6
(Jul-26-2025, 08:12 AM)Pedroski55 Wrote: So, actually, the problem is merging the two images into 1 image?

Can you post the 2 images?

no, that's not what I said at all.

And I did post two SS.

please read what I wrote, I explained the problem in detail.
Reply
#7
Aha, a testy chappy!

The images you posted are screen shots of an entire window. I cut out the riveted frame with a black rectangle, and the black rectangle with the white dragon, saved these images.

Then I made black transparent and saved the images as .png

After that, it is very easy using PIL to resize the dragon and paste it over the frame:

Quote:What We Need
A reliable way to resize the image to 50% scale

from PIL import Image, ImageDraw, ImageFilter

im1 = Image.open('/home/peterr/Downloads/frame.png')
im2 = Image.open('/home/peterr/Downloads/dragon.png')
im1size = im1.size # (521, 565)
im1.mode # 'RGBA'
im2size = im2.size # (123, 187)
im2.mode # 'RGBA'

# resize im2 it is too small
# Specify the target width (lets say 2 x 123 =250)
target_width = 250

# Calculate the aspect ratio
aspect_ratio = im2.width / im2.height

# Calculate the target height to maintain the aspect ratio
target_height = int(target_width / aspect_ratio)

# Resize the image
resized_image = im2.resize((target_width, target_height))

# don't alter the original image
back_im = im1.copy()
back_im.paste(resized_image, (140, 90)) # size and position determined manually by trial and error here
back_im.show()
back_im.save('/home/peterr/Downloads/frame+dragon.png', quality=95)

im3 = Image.open('/home/peterr/Downloads/frame+dragon.png')
im3.mode # 'RGBA'
You can do this with cv2 too.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Image Not Saved Properly jkreski 25 453 Mar-24-2026, 04:01 PM
Last Post: deanhystad
  image processing with opencv-python and Tesseract OCR marchellopl 4 157 Feb-15-2026, 12:27 AM
Last Post: Pedroski55
  why is the image not showing sarbogast 2 681 Nov-12-2025, 07:26 PM
Last Post: deanhystad
  image does not show with pillow Mohamad_afeff_nasser 5 3,905 Apr-13-2025, 07:53 PM
Last Post: snippsat
  Placing image button on top of background image Sintek 1 1,187 Mar-02-2025, 05:36 PM
Last Post: Sintek
  Problem When using canny edge detection,black image returned rickyw2777 1 1,071 Feb-17-2025, 03:22 AM
Last Post: rickyw2777
  Why it gives me a black image and libpng warning: iCCP rickyw2777 1 1,137 Feb-16-2025, 08:26 PM
Last Post: rickyw2777
  QR code creation with image Raja31 1 4,794 Jan-15-2025, 11:17 AM
Last Post: Larz60+
Photo image generation with text style Belialhun 0 1,267 Oct-08-2024, 01:53 PM
Last Post: Belialhun
  Product Image Download Help Required pythonustasi 5 2,157 Jul-21-2024, 08:12 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020