Copy/Paste first wird in tag into tag?

Started by TushyBhutt, February 15, 2023, 01:15:41 PM

Previous topic - Next topic

TushyBhutt

Hello, it's me again.  I am 97.6% of the way through completing my Stable Diffusion to Lightroom code string but I am tripping up on one last item.

Let's say I have this text:

Colour: Red, Green, Blue, Gold

and I want to turn it into:

Colour|Red##Colour|Green##Colour|Blue##Colour|Gold
How can I do this with Regex for:

1. An arbitrary number of lines like:

Colour: Red, Green, Blue, Gold
Shapes: Square, Round, Triangle
etc...

becomes

Colour|Red##Colour|Green##Colour|Blue##Colour|Gold
Shapes|Square##,Shapes|Round##Shapes|Triangle
etc...

2. For an arbitrary number of words between commas
3. An arbitrary number of comma separated values?

If I needed a PERL code called out for this I supposed it can be done?

According to ChatGPT (I know, I  know), this could work:

$text =~ s/,/^([^:]+):/ge;

but when I try it on regex101, it doesn't have the /e flag (I guess it's deprecated?)

And this doesn't work either

(^[^:]+):\s*\K(, )

Is this possible within ExifTool?

Phil Harvey

I can do this by looping:

> cat a.txt
Colour: Red, Green, Blue, Gold
Shapes: Square, Round, Triangle
> perl -e 'open FILE,"a.txt";foreach (<FILE>) { while (s/^(.*?): (.*), (.*)/$1: $2##$1|$3/) { };s/: /|/;print $_ }'
Colour|Red##Colour|Green##Colour|Blue##Colour|Gold
Shapes|Square##Shapes|Round##Shapes|Triangle

But I don't know if this could be done with a single regex.

/e would be not allowed on regex101 because it is a security hazard.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

TushyBhutt

#2
Quote from: Phil Harvey on February 15, 2023, 01:54:17 PMI can do this by looping:

> cat a.txt
Colour: Red, Green, Blue, Gold
Shapes: Square, Round, Triangle
> perl -e 'open FILE,"a.txt";foreach (<FILE>) { while (s/^(.*?): (.*), (.*)/$1: $2##$1|$3/) { };s/: /|/;print $_ }'
Colour|Red##Colour|Green##Colour|Blue##Colour|Gold
Shapes|Square##Shapes|Round##Shapes|Triangle

But I don't know if this could be done with a single regex.

/e would be not allowed on regex101 because it is a security hazard.

- Phil

Thanks!  Understood about Regex, and when I tried /gr or /ge in Exiftool I got an error anyway.  With your code example, I take it that is something Exiftool can call via script to PERL with the -p flag? I've got my commands to get SD --> LR all set up and if I can figure this out I supposed I can just chain it to the end of the search/replace steps so, something like this:

"-XMP-lr:HierarchicalSubject<${PNG:parameters; s/\A/Prompt:/; s/\|/\¦/g ; s/(\G(?!\A)|(Steps))((?:(?!(\R)).)*?)(\: )/$3$2|/g; s/^([^:]+):\s*/\1\|/gm; s/(, *|\R)/##/g;}" -p "${DoSomeStuffHere}"

I figure with ExifToolGUI I may not need 'open FILE,"a.txt";foreach (<FILE>) ?

TushyBhutt

Hi,  can I pass the value of XMP:LR-HierarchicalSubject from PNG:Parameters to the PERL tool this way?

-e "-XMP-lr:HierarchicalSubject<${PNG:parameters;  { while (s/^(.*?): (.*), (.*)/$1: $2##$1|$3/) { };s/: /|/;print $_ };}"

When I did that, there were no errors but the output was not as expected.  It worked on one line, but not the remaining.

Input:

crisp [airbrush:painting:0.7] of small (cat|dog) wearing sweater, funny.Negative prompt: logo, text,watermark,(NegLowRes-1000:1.2).Steps: 25, Sampler: Euler a, CFG scale: 7, Seed: 755474724, Size: 512x768, Model hash: ad2a33c361, Model: StabilityAI_Stable21-768-ema-pruned, Denoising strength: 0.6, Hires resize: 1024x1536, Hires steps: 25, Hires upscaler: Latent (bicubic antialiased), Eta: 0.85, Score: 6.93.Template: crisp [airbrush:painting:0.7] of small (cat|dog) wearing sweater, funny.Negative Template: logo, text,watermark,(NegLowRes-1000:1.2)

Output:

crisp [airbrush:painting:0.7] of small (cat|dog) wearing sweater, funny.Negative prompt|logo, text,watermark,(NegLowRes-1000:1.2).Steps: 25, Sampler: Euler a, CFG scale: 7, Seed: 755474724, Size: 512x768, Model hash: ad2a33c361, Model: StabilityAI_Stable21-768-ema-pruned, Denoising strength: 0.6, Hires resize: 1024x1536, Hires steps: 25, Hires upscaler: Latent (bicubic antialiased), Eta: 0.85, Score: 6.93.Template: crisp [airbrush:painting:0.7] of small (cat|dog) wearing sweater, funny.Negative Template: logo, text,watermark,(NegLowRes-1000:1.2)
Expected Output:

crisp [airbrush:painting:0.7] of small (cat|dog) wearing sweater, funny.Negative prompt|logo##Negative prompt|text##Negative prompt|watermark##Negative prompt|(NegLowRes-1000:1.2).Steps: 25, Sampler: Euler a, CFG scale: 7, Seed: 755474724, Size: 512x768, Model hash: ad2a33c361, Model: StabilityAI_Stable21-768-ema-pruned, Denoising strength: 0.6, Hires resize: 1024x1536, Hires steps: 25, Hires upscaler: Latent (bicubic antialiased), Eta: 0.85, Score: 6.93.Template|crisp [airbrush:painting:0.7] of small (cat|dog) wearing sweater##Template|funny.Negative Template|logo##Negative Template|text##Negative Template|watermark##Negative Template|(NegLowRes-1000:1.2)

The PNG data does have line breaks in it, so not sure why the PERL code didn't see them.  For easier readability, I was thinking the output would be like:


crisp [airbrush:painting:0.7] of small (cat|dog) wearing sweater, funny
Negative prompt|logo##Negative prompt|text##Negative prompt|watermark##Negative prompt|(NegLowRes-1000:1.2)
Steps: 25, Sampler: Euler a, CFG scale: 7, Seed: 755474724, Size: 512x768, Model hash: ad2a33c361, Model: StabilityAI_Stable21-768-ema-pruned, Denoising strength: 0.6, Hires resize: 1024x1536, Hires steps: 25, Hires upscaler: Latent (bicubic antialiased), Eta: 0.85, Score: 6.93
Template|crisp [airbrush:painting:0.7] of small (cat|dog) wearing sweater##Template|funny
Negative Template|logo##Negative Template|text##Negative Template|watermark##Negative Template|(NegLowRes-1000:1.2)