Hi all,
there: https://forum.xojo.com/27657-draw-line-and-jpg-file
I shared two methods to load and save an image.
I have a simple trouble: my saved image is not transparent: his white is
solid (white), but it is white on screen (I set a light yellow background to my window to be able to see what happens.
Ive done that many times in the past (I am an has been
), but here I am lost.
Help !
I even copy the Offscreen Picture or Canvas1.Backdrop into the Clipboard and at paste time, I can see the solid white (instead of transparent !
BTW: At Open the image time, I tried both:
gPict = New Picture(pic.Width,pic.Height,32)
gPict.Transparent = 1
and
gPict = New Picture(pic.Width,pic.Height)
The white = transparent property is not saved with the image.
You need to set that property after you reopen it.
if you want to save transparency, you should generate a PNG file with alpha channel.
But saving a picture with white spots creates a picture with white spots.
When you open it in Xojo,
after it is put into a picture variable, you then tell Xojo that white is transparent.
dim mypic as picture
mypic = picture.open( myfolderitem)
mypic.transparent = 1
It may be that saving the image changes the colors slightly… make sure that the saved image (and the version you get after reloading) still has white where you think it has white.
Hi Jeff,
You are correct and this is a test I do not do yesterday but minutes ago.
I loaded my just modified and saved image (a Yosemite default folder icon in png, so no white around the folder image) and the result is
my image appears with no white.
Unfortunately, in both Preview and GIMP, the same image have surrounding white :(.
That was what I was testing, yesterday.
I stated that yesterday, I am also unable to place a correct copy of the image in the Clipboard too.
Jeff: while reading your anwser (thanks), a crazy idea comes to mind (I love them).
I added a nother PushButton that load an image and copy its contents into the Clipboard. This works fine with the test file (the Yosemite folder icon), but not in another test file (the kind of files I want to get data without white everywhere).
Heres the code (with redundancies)
[code] //
// Load an image and copy it into a Clipboard instance
Dim picFile As FolderItem
Dim Clip As New Clipboard
picFile = GetOpenFolderItem("")
If picFile <> Nil Then
Dim pic As Picture
pic = Picture.Open(picFile)
pic.Transparent = 1
// Create an offscreen Picture
gPict = New Picture(pic.Width,pic.Height,32)
gPict.Transparent = 1
// Draw the loaded Picture into the Offscreen Picture
gPict = pic
gPict.Transparent = 1
// Assign the image to the Clip instance
Clip.Picture = gPict
Clip.Close
End If[/code]
This is nearly the Open code: I only add the Clipboard part.
I tend to think that .Transparent
does not works anymore
In Preview, I removed a bit of white from my scanned image file (I set the white point in GIMP, and I checked these are really white), saved the file and loaded it with my example project: both copy to Clipboard and save to a new png keeps that simple rect without white.
Of course, opening a file with a part without any color (transparent part) and displaying it with that same project displays what I expect, but this is not the point:
I want to be able to transform an image with white area into an image with transparency where there was white dots in the file before loading it and bee able to save this result.
Since I do not know how to use the new Alpha Channel to make white part transparent, I ask for help here: HELP !
To create a Picture with an alpha channel, use the new constructor that omits the Depth parameter:
dim p as new picture (200,200)
This is all transparent.
Draw the non-transparent stuff to it, and save as PNG.
The transparency will be preserved
The white you see may not be pure (think laundry soap ;). In other words, only &CFFFFFF can be turned transparent. Even the slightest lower value will not turn transparent (for instance &cFFFFFE).
Here is a method that cleans the white above a certain level (&cf5f4f2):
Sub Action()
dim f as FolderItem = SpecialFolder.Desktop.child("blue.jpeg")
dim pic as picture
if f<> Nil then
pic=Picture.Open(f)
for x as integer = 1 to pic.width
for y as integer = 1 to pic.height
dim surf as RGBSurface = Pic.RGBSurface
dim lacouleur as integer = val(str(surf.Pixel(x,y)))
if lacouleur > &hf5f4f2 then
surf.Pixel(x,y) = &cFFFFFF
end if
next
next
pic.Transparent=1
Canvas1.Backdrop=pic
end if
End Sub
I experimented with a picture I got, that to the eye, seems all white background, but in fact around the subject some pixels are slightly grey. To find the value of the lower cutting point &hf5f4f2, I used the pipette in Photoshop. It may work right away for you, but you can also set it darker if that does not do the trick.
@Michel: thank you for the code (idea).
It does the trick on screen, but not in the saved file. To save the file, I added a save part to ypur code (above):
[code] Dim fFI As FolderItem
fFI = GetSaveFolderItem("public.png","Result pic Picture.png")
If fFI <> Nil Then
pic.Save(fFI,Picture.SaveAsPNG)
End If[/code]
The file is created, but all white is still there (unlike on screen). Did I do something wrong ?
I will reformat the whole project and share it a bit later (in one hour or so), then we will be able to really talk about it.
Even my implementation of Jeff advice goes wrong at save to disk ina png goes wrong. *
- if a part of the image is already transparent, this goes like that on disk, but the already white still is white (on disk).
Everything is OK on screen.
Thanks.
OK, I got it !
As astounded as it is, Michael was right: my white was not so white !
The code, once polished, display correctly real white parts as transparent and transparent parts (removed white in GIMP) as transparent !
And once saved, the file keeps the transparent parts.
Now I have to check what I’ve done wrong with Michel code (it stopped working in a change Ive done
)
Thank you to Jeff and Michel !
I found the error
I suspected a trouble when I saw a transparent part
(I buckleted to white that part in GIMP)
Also, I moved:
Dim surf As RGBSurface = Pic.RGBSurface
out (before) the loop to avoid calling it so many times
(slowdown the process: no problemo on small images, but with larger !!!).
After dinner, I tried something else; I modified my target image as follows:
a. Open the file in GIMP,
b. Apply a 1pixel Gaussian Filter,
c. Set the White Point
d. Change the brightness
e. Export the file as png.
Then, and only then run the project and open the brand new file and this was good (either on screen and saved in another png file).
I have to work on it a bit and will try to share that small project by tomorrow