File size: 906 Bytes
371818e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
"""Native RGBA prompting and lossless alpha reporting. Built with Qwen."""

def transparent_prompt(prompt):
    return (prompt.rstrip() + '\nIsolated subject on a transparent background. '
            'Preserve natural fine edges and any translucent details in the alpha channel. '
            'No opaque backdrop or checkerboard pattern.')


def alpha_statistics(image):
    result = {'mode': image.mode, 'has_alpha': 'A' in image.getbands()}
    if result['has_alpha']:
        alpha = image.getchannel('A')
        histogram = alpha.histogram()
        total = image.width * image.height
        result.update(alpha_min=alpha.getextrema()[0], alpha_max=alpha.getextrema()[1],
                      transparent_fraction=sum(histogram[:16])/total,
                      translucent_fraction=sum(histogram[16:240])/total,
                      opaque_fraction=sum(histogram[240:])/total)
    return result