Skip to content

Transparency gets lost with rawsave_buffer #4585

Closed Answered by jcupitt
martindisch asked this question in Q&A
Discussion options

You must be logged in to vote

I messed about a bit and I think this would probably be the best approach:

#!/usr/bin/env python3

import sys
import pyvips

# thumbnail to the largest size in memory, flattening any alpha
image = pyvips.Image.thumbnail(sys.argv[1], 2880) 
if image.hasalpha():
    image = image.flatten(background=255) 
image = image.copy_memory()
image.write_to_file(f"thumb-2880.jpg")

# for subsequent sizes, thumbnail_image to memory
for size in [1440, 1000, 720, 480, 320, 260, 130]:
    image = image.thumbnail_image(size).copy_memory()
    image.write_to_file(f"thumb-{size}.jpg")

I made a large test PNG with alpha:

$ vips copy ~/pics/lion.svg[scale=20] x.png
$ vipsheader x.png
x.png: 10630x10630 uchar, …

Replies: 4 comments 3 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
3 replies
@jcupitt
Comment options

@kleisauke
Comment options

@martindisch
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by martindisch
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
3 participants
Converted from issue

This discussion was converted from issue #4584 on June 25, 2025 21:51.