-
Notifications
You must be signed in to change notification settings - Fork 257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG] Image background not transparent: becomes gray #244
Comments
We have seen a similar problem in the past where such gray shadows are created. In that case, when we analyzed the image data with go and I could see that there are gray RGBA pixels with low alpha values A around 10-15. The range of A is 0-255 where 0 is transparent and 255 fully opaque. So, it makes sense that we see those gray areas. What does not make sense however, is that when I view this image in Chrome or Preview, I don't notice those gray areas. So I am not sure if some image viewers filter out some alpha values. To solve this problem in the past we added functionality in unipdf to transform the alpha values. The mapping function takes in an alpha value and returns an alpha value. The following code worked well to make the image in the PDF without this gray shadow and and should be harmless to other signatures that do not have this issue: This is implemented on img.AlphaMap(func(alpha byte) byte {
if alpha > 50 {
return alpha
} else {
return 0 // Transparent
}
}) However, for the present example this function is not accessible since its a Also with respect to related issue #180. Note that I have not verified yet that this is the same problem, but looks similar and good to get this on record anyway. |
After further analysis, it was found that the image was not clean, containing alpha values on the border. The following processing yields a clean image: // Open image file.
imgFile, err := os.Open(imagePath)
if err != nil {
log.Fatal(err)
}
defer imgFile.Close()
// Read image file.
img, err := model.ImageHandling.Read(imgFile)
if err != nil {
log.Fatal(err)
}
// Remove image background noise.
img.AlphaMap(func(alpha byte) byte {
// Increase/Decrease alpha threshold depending on the input image
// noise profile.
if alpha <= 245 {
return 0
}
return alpha
})
// Add creator image.
cImg, err := c.NewImage(img)
if err != nil {
log.Fatal(err)
} creates a clean image in the PDF. Note that this only solves this specific problem. For more advanced image processing, it would make sense to use a dedicated image processing library prior to embedding the image into unipdf. |
Description
A clear and concise description of what the bug is.
An issue report was received via email:
Possibly related to #180
Expected Behavior
Image is expected to have transparent background when opened in a PDF viewer.
Actual Behavior
Attachments
A full example was included with the report.
The text was updated successfully, but these errors were encountered: