My purpose is to turn an image to greyscale, and set transparency on bright grey values.
I suppose the best way to do that would be to use some graphic libs ane send shell commands, but I don't want to use external libs.
Bernard wrote: ↑Fri Jul 09, 2021 12:01 am
Or you could save the imagedata to a file, then pass that filename to the python script, where python would read the binary data from that named file.
Don't you think reading and writing large files will be longer?
For informations, this is my LC (9.04) code. Sorry, it's not very easy to read, as I tried to optimize it (and it's french commented
)
Code: Select all
local arrParams
on preProcess
if ("button" is in the target) then set hilite of the target to true
set cursor to watch
------- reset PixEnd (delete image pour full reset si l'alphaData est corrompu)
if (there is an image arrParams["destName"]) then delete image arrParams["destName"]
create image arrParams["destName"]
set threeD of image arrParams["destName"] to (the threeD of image arrParams["sourceName"])
set borderWidth of image arrParams["destName"] to (the borderWidth of image arrParams["sourceName"])
set showBorder of image arrParams["destName"] to (the showBorder of image arrParams["sourceName"])
------- visuel
set the rect of image arrParams["destName"] to (the rect of image arrParams["sourceName"])
set the topLeft of image arrParams["destName"] to \
(the left of image arrParams["sourceName"] & "," & the bottom of image arrParams["sourceName"] + 6)
-------
put the milliseconds into fld "Timer" -- /////
end preProcess
on getParams
put "Pix" into arrParams["sourceName"]
put "PixEnd" into arrParams["destName"]
put the hilite of btn "ChkInvert" into arrParams["invert"] -- négatif
get the thumbPos of scrollbar "SliderLighten" -- de 0 (aucun éclaircissement) à 10 (éclaircissement maxi)
put round(it * 200 / 10) into arrParams["brightness"] -- de 0 à 200
get the thumbPos of scrollbar "SliderContrast" -- de 0 (aucune modif) à 10 (contraste maxi)
put it / 10 into arrParams["contrast"] -- de 0 à 1
put 0.30 into arrParams["xRed"] -- augmenter pour éclaircir les rouges (avec total = 1)
put 0.59 into arrParams["xGreen"] -- augmenter pour éclaircir les verts (avec total = 1)
put 0.11 into arrParams["xBlue"] -- augmenter pour éclaircir les bleus (avec total = 1)
put the width of image arrParams["sourceName"] into arrParams["w"]
put the height of image arrParams["sourceName"] into arrParams["h"]
end getParams
on postProcess
------- visual effect
show image "PixEnd"
put (the milliseconds - fld "Timer") && "ms" into fld "Timer"
if ("button" is in the target) then set hilite of the target to false
end postProcess
on mouseUp
getParams
preProcess
turnToGrayTranspa arrParams["sourceName"], arrParams["destName"], arrParams["w"], arrParams["h"], arrParams["xRed"], arrParams["xGreen"], arrParams["xBlue"], arrParams["brightness"], arrParams["contrast"], arrParams["invert"]
postProcess
end mouseUp
on turnToGrayTranspa pixSourceName, pixDestName, imgWidth, imgHeight, xRed, xGreen, xBlue, brightness, fc, invertBWboolean
put "" into log
addToLog log, imgWidth && "x" && imgHeight && "pixels"
addToLog log, "r = " & xRed && " g = " & xGreen && " b = " & xBlue
addToLog log, "brightness = " & brightness
----------------------------------------------------- prépa : négatif
if (invertBWboolean) then
put 255 into grayCorrection
else put 0 into grayCorrection
addToLog log, "invertBWboolean = " & invertBWboolean && " --> grayCorrection = " & grayCorrection
----------------------------------------------------- prépa : contraste
put 0 - (fc^5 + 0.05) into fp5 -- pré-calcul
put sqrt(fc) into racf -- pré-calcul
addToLog log, "fc = " & fc
----------------------------------------------------- prépa : image et transparence
put the imageData of image pixSourceName into tImage
local aImage -- pour alphaData
-----------------------------------------------------
local tPixel, tImgPosition, tGray
# repeat for all the pixels in the image
repeat with tPixel = 0 to ((imgWidth * imgHeight) - 1)
----------------------------------------------------- extractions des composantes RGB
# multiply the image pixel position by 4 (Alpha + Red + Green + Blue)
put tPixel * 4 into tImgPosition
/* plus long
# calculate the gray level
put charToNum(char(tImgPosition + 2) of tImage) into tRed
put charToNum(char(tImgPosition + 3) of tImage) into tGreen
put charToNum(char(tImgPosition + 4) of tImage) into tBlue
----------------------------------------------------- gris
# here you can use any grayscale formula
# set the RGB of the pixel to the same value this gives us the gray level (0-255)
--put (xRed * tRed) + (xGreen * tGreen) + (xBlue * tBlue) into tGray
--------------------- application auto "négatif"
put abs(grayCorrection - ((xRed * tRed) + (xGreen * tGreen) + (xBlue * tBlue))) into tGray
*/
------------ calcul valeurs gris et application auto "négatif"
put abs(grayCorrection - ((xRed * charToNum(char(tImgPosition + 2) of tImage)) + \
(xGreen * charToNum(char(tImgPosition + 3) of tImage)) + \
(xBlue * charToNum(char(tImgPosition + 4) of tImage)))) into tGray
----------------------------------------------------- contraste
if (fc > 0) then
put floor(tGray * (1 - racf)) + racf * (255 / (1 + (0.9921875 * exp(fp5 * (tGray - 128))))) into tGray
-- Rislon : (x * (1 - sqrt(fc))) + sqrt(fc) * (255 / (1 + (0.9921875 * exp(-(fc^5 + 0.05) * (x - 128)))))
/* -- plus précis mais plus long
if (fc >= 0.5) then
put exp(-2 * (tGray - 128) * fc^4) into tmg
put floor(128 + 127.5 * (1 - tmg) / ( 1 + tmg)) into tGray
else
put exp(-2 * (tGray - 128) * 0.5^4) into tmg
get 128 + 127.5 * (1 - tmg) / ( 1 + tmg)
put floor(tGray * (1 - 2 * fc) + 2 * fc * it) into tGray
end if
*/
end if
----------------------------------------------------- éclaircissement général
put min(255, brightness + tGray) into tGray
----------------------------------------------------- application des nouvelles valeurs de gris
put numToChar(tGray) into ntcGray
put ntcGray & ntcGray & ntcGray into char (tImgPosition + 2) to (tImgPosition + 4) of tImage
----------------------------------------------------- transparence toutes valeurs (alphaData)
--------- transparence proportionnelle à la valeur de gris : plus c'est clair -> plus c'est transparent
/*
binaryEncode("C",0) --> transparentPixel
binaryEncode("C",255) --> opaquePixel
*/
put binaryEncode("C", 255 - tGray) after aImage -- 0 = transparent et 255 : opaque
end repeat
-------------------------------------------------------- affectation image dest
set the imageData of image pixDestName to tImage
set the alphaData of img pixDestName to aImage
if (there is a fld "Log") then put log into fld "Log" --/////
end turnToGrayTranspa
on addToLog @log, str
put str & return after log
end addToLog
function func0 x, f -- fcn Rislon pour f >= 0.5 : UNUSED telle quelle
return (128 + 127.5 * (1 - exp(-2 * (x - 128) * f^4)) / ( 1 + exp(-2 * (x - 128) * f^4)))
end func0
function func1 x, f -- fcn Rislon pour f < 0.5 : UNUSED telle quelle
return (x * (1 - 2 * f) + 2 * f * func0(x, 0.5))
end func1