diff --git a/README.md b/README.md index e574e71..011168d 100644 --- a/README.md +++ b/README.md @@ -81,9 +81,15 @@ see [Documents](https://github.com/Invary/IvyPhotoshopDiffusion/tree/main/doc) p ## Changelog +- Ver105
+Support restore faces, tiling, ENSD, clip skip
+Set/get photoshop forground/background color
+Bug fix json request/response
+Ver105 work for Automatic1111 2022/11/01 03:00 UTC version
+https://github.com/AUTOMATIC1111/stable-diffusion-webui/tree/5c9b3625fa03f18649e1843b5e9f2df2d4de94f9 + - Ver104
Support latest version of Automatic1111's API
- Ver104 work for Automatic1111 2022/10/31 06:00 UTC version
https://github.com/AUTOMATIC1111/stable-diffusion-webui/tree/17a2076f72562b428052ee3fc8c43d19c03ecd1e @@ -92,7 +98,6 @@ https://github.com/AUTOMATIC1111/stable-diffusion-webui/tree/17a2076f72562b42805 Save last prompt/negative/layername setting for default value
Add setting layer name function
Bug fix image processing exception
- Ver103 work for Automatic1111 2022/10/28 version
https://github.com/AUTOMATIC1111/stable-diffusion-webui/tree/9ceef81f77ecce89f0c8f412c4d849210d852e82 diff --git a/src/IvyPhotoshopDiffusion/Automatic1111.cs b/src/IvyPhotoshopDiffusion/Automatic1111.cs index a3d27a5..b3607c4 100644 --- a/src/IvyPhotoshopDiffusion/Automatic1111.cs +++ b/src/IvyPhotoshopDiffusion/Automatic1111.cs @@ -19,6 +19,18 @@ namespace Invary.IvyPhotoshopDiffusion // https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/API + //progress api + // + //https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/3722 + // + //Call /sdapi/v1/txt2img like this: + //{ + // override_settings: { show_progress_every_n_steps: 5 }, + //} + //And /sdapi/v1/progress will respond with a valid current_image field. + + + internal class Automatic1111 { @@ -120,6 +132,57 @@ public static JsonResponseImg2Img SendImg2Img(JsonRequestImg2Img objJson) + //extra check code + //{ + // JsonRequestExtra objJson = new JsonRequestExtra(); + // objJson.image = Automatic1111.Image2String(new Bitmap(512, 512)); + // Automatic1111.SendExtra(objJson); + //} + + + ////TODO: current version of Automatic1111, extra api cannot work?? + //public static JsonResponseImg2Img SendExtra(JsonRequestExtra objJson) + //{ + // string jsonString = JsonSerializer.Serialize(objJson); + + // var url = $"{XmlSetting.Current.Automatic1111ApiUrl}/sdapi/v1/extra-single-image"; + + // var request = WebRequest.Create(url); + // request.Method = "POST"; + + // string json = jsonString; + // byte[] byteArray = Encoding.UTF8.GetBytes(json); + + // request.ContentType = "application/json"; + // request.ContentLength = byteArray.Length; + + // using (var reqStream = request.GetRequestStream()) + // { + // reqStream.Write(byteArray, 0, byteArray.Length); + + // using (var response = request.GetResponse()) + // { + // Debug.WriteLine(((HttpWebResponse)response).StatusDescription); + + // using (var respStream = response.GetResponseStream()) + // using (var reader = new StreamReader(respStream)) + // { + // string jsonresponse = reader.ReadToEnd(); + + // //TODO: response json is unknown + // Debug.Assert(false); + // var ret = JsonSerializer.Deserialize(jsonresponse); + // ret.Info = JsonSerializer.Deserialize(ret.info); + // return ret; + // } + // } + // } + //} + + + + + public static string Image2String(Image image) { if (image == null) @@ -134,7 +197,6 @@ public static string Image2String(Image image) ms.Read(rawdata, 0, rawdata.Length); return "data:image/png;base64," + Convert.ToBase64String(rawdata); - //return Convert.ToBase64String(rawdata); } } @@ -143,6 +205,7 @@ public static string Image2String(Image image) public static bool SaveBase64EncodingData(string file, string text) { + //TODO: fixed image type text = text.Replace("data:image/png;base64,", ""); var rawdata = Convert.FromBase64String(text); @@ -169,6 +232,7 @@ public class JsonRequestBase public float denoising_strength { get; set; } = 0.75f; public decimal seed { get; set; } = -1; + public decimal subseed { get; set; } = -1; public float subseed_strength { get; set; } = 0.0f; public int seed_resize_from_h { get; set; } = -1; @@ -187,12 +251,27 @@ public class JsonRequestBase public string negative_prompt { get; set; } = ""; - public int eta { get; set; } = 0; + public float eta { get; set; } = 0; + + /// + /// Sigma Churn + /// + public float s_churn { get; set; } = 0; + + /// + /// Sigma max + /// + public float s_tmax { get; set; } = 0; + + /// + /// Sigma min + /// + public float s_tmin { get; set; } = 0; - public int s_churn { get; set; } = 0; - public int s_tmax { get; set; } = 0; - public int s_tmin { get; set; } = 0; - public int s_noise { get; set; } = 1; + /// + /// Sigma noise + /// + public float s_noise { get; set; } = 1; public Override_Settings override_settings { get; set; } @@ -203,6 +282,8 @@ public class JsonRequestBase public class Override_Settings { + public int eta_noise_seed_delta { get; set; } = 0; + public int CLIP_stop_at_last_layers { get; set; } = 1; } @@ -258,6 +339,8 @@ public class JsonRequestImg2Img : JsonRequestBase public class JsonResponseBase { public string[] images { get; set; } + + //automatic1111's bug. response is json text, not object. so need to deserialize this public string info { get; set; } [JsonIgnore] @@ -265,59 +348,11 @@ public class JsonResponseBase } - //public class Parameters - //{ - // public bool enable_hr { get; set; } - // public float denoising_strength { get; set; } - // public int firstphase_width { get; set; } - // public int firstphase_height { get; set; } - // public string prompt { get; set; } - // public object styles { get; set; } - // public int seed { get; set; } - // public int subseed { get; set; } - // public float subseed_strength { get; set; } - // public int seed_resize_from_h { get; set; } - // public int seed_resize_from_w { get; set; } - // public int batch_size { get; set; } - // public int n_iter { get; set; } - // public int steps { get; set; } - // public float cfg_scale { get; set; } - // public int width { get; set; } - // public int height { get; set; } - // public bool restore_faces { get; set; } - // public bool tiling { get; set; } - // public string negative_prompt { get; set; } - // public float eta { get; set; } - // public float s_churn { get; set; } - // public float s_tmax { get; set; } - // public float s_tmin { get; set; } - // public float s_noise { get; set; } - // public object override_settings { get; set; } - // public string sampler_index { get; set; } - //} - - - - - - - - - - - //public class JsonResponseBase - //{ - // public string[] images { get; set; } - // public Info info { get; set; } - //} - - - public class JsonResponseTxt2Img : JsonResponseBase { public ParametersTxt2Img parameters { get; set; } @@ -347,12 +382,12 @@ public class ParametersBase public bool restore_faces { get; set; } public bool tiling { get; set; } public string negative_prompt { get; set; } - public object eta { get; set; } + public float eta { get; set; } public float s_churn { get; set; } - public object s_tmax { get; set; } + public float s_tmax { get; set; } public float s_tmin { get; set; } public float s_noise { get; set; } - public object override_settings { get; set; } + public Override_Settings override_settings { get; set; } public string sampler_index { get; set; } } @@ -366,40 +401,6 @@ public class ParametersTxt2Img : ParametersBase } - //public class Info - //{ - // public string prompt { get; set; } - // public string[] all_prompts { get; set; } - // public string negative_prompt { get; set; } - // public decimal seed { get; set; } - // public decimal[] all_seeds { get; set; } - // public decimal subseed { get; set; } - // public decimal[] all_subseeds { get; set; } - // public float subseed_strength { get; set; } - // public int width { get; set; } - // public int height { get; set; } - // public int sampler_index { get; set; } - // public string sampler { get; set; } - // public float cfg_scale { get; set; } - // public int steps { get; set; } - // public int batch_size { get; set; } - // public bool restore_faces { get; set; } - // public object face_restoration_model { get; set; } - // public string sd_model_hash { get; set; } - // public int seed_resize_from_w { get; set; } - // public int seed_resize_from_h { get; set; } - // public float denoising_strength { get; set; } - // public Extra_Generation_Params extra_generation_params { get; set; } - // public int index_of_first_image { get; set; } - // public string[] infotexts { get; set; } - // public object[] styles { get; set; } - // public string job_timestamp { get; set; } - // public int clip_skip { get; set; } - //} - - - - @@ -476,4 +477,34 @@ public class JsonResponseInfo + + + + + //public class JsonRequestExtra + //{ + // public bool upscale_first { get; set; } = true; + + // public int resize_mode { get; set; } = 0; + // public bool show_extras_results { get; set; } = true; + // public float gfpgan_visibility { get; set; } = 0; + // public float codeformer_visibility { get; set; } = 0; + // public float codeformer_weight { get; set; } = 0; + // public int upscaling_resize { get; set; } = 2; + // public int upscaling_resize_w { get; set; } = 512; + // public int upscaling_resize_h { get; set; } = 512; + // public bool upscaling_crop { get; set; } = true; + // public string upscaler_1 { get; set; } = "None"; + // public string upscaler_2 { get; set; } = "None"; + // public int extras_upscaler_2_visibility { get; set; } = 0; + + // public string image { get; set; } + //} + + + + + + + } diff --git a/src/IvyPhotoshopDiffusion/FormMain.Designer.cs b/src/IvyPhotoshopDiffusion/FormMain.Designer.cs index 00b2206..e01d3c9 100644 --- a/src/IvyPhotoshopDiffusion/FormMain.Designer.cs +++ b/src/IvyPhotoshopDiffusion/FormMain.Designer.cs @@ -79,6 +79,12 @@ private void InitializeComponent() this.buttonLogWrite = new System.Windows.Forms.Button(); this.textBoxLayerName = new System.Windows.Forms.TextBox(); this.label9 = new System.Windows.Forms.Label(); + this.checkBoxRestoreFace = new System.Windows.Forms.CheckBox(); + this.checkBoxTiling = new System.Windows.Forms.CheckBox(); + this.numericUpDownENSD = new System.Windows.Forms.NumericUpDown(); + this.label10 = new System.Windows.Forms.Label(); + this.label11 = new System.Windows.Forms.Label(); + this.numericUpDownClipSkip = new System.Windows.Forms.NumericUpDown(); ((System.ComponentModel.ISupportInitialize)(this.trackBarNoiseScale100)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.trackBarMaskBlur)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxMask)).BeginInit(); @@ -89,6 +95,8 @@ private void InitializeComponent() ((System.ComponentModel.ISupportInitialize)(this.trackBarBatchCount)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxNewVersionExists)).BeginInit(); this.groupBox1.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.numericUpDownENSD)).BeginInit(); + ((System.ComponentModel.ISupportInitialize)(this.numericUpDownClipSkip)).BeginInit(); this.SuspendLayout(); // // buttonGenerate @@ -320,9 +328,7 @@ private void InitializeComponent() this.buttonSetTransparentColor.Name = "buttonSetTransparentColor"; this.buttonSetTransparentColor.Size = new System.Drawing.Size(75, 23); this.buttonSetTransparentColor.TabIndex = 22; - this.toolTip.SetToolTip(this.buttonSetTransparentColor, "Background color in photoshop.\r\nAttension: do not use transparent layer in photos" + - "hop, it cause error.\r\nUse the transparent color setting here for locations you w" + - "ish to treat as transparent."); + this.toolTip.SetToolTip(this.buttonSetTransparentColor, resources.GetString("buttonSetTransparentColor.ToolTip")); this.buttonSetTransparentColor.UseVisualStyleBackColor = true; this.buttonSetTransparentColor.Click += new System.EventHandler(this.buttonSetTransparentColor_Click); // @@ -615,12 +621,92 @@ private void InitializeComponent() this.label9.TabIndex = 49; this.label9.Text = "Layer name"; this.toolTip.SetToolTip(this.label9, resources.GetString("label9.ToolTip")); + // + // checkBoxRestoreFace + // + this.checkBoxRestoreFace.AutoSize = true; + this.checkBoxRestoreFace.Location = new System.Drawing.Point(25, 370); + this.checkBoxRestoreFace.Name = "checkBoxRestoreFace"; + this.checkBoxRestoreFace.Size = new System.Drawing.Size(96, 16); + this.checkBoxRestoreFace.TabIndex = 50; + this.checkBoxRestoreFace.Text = "Restore faces"; + this.checkBoxRestoreFace.UseVisualStyleBackColor = true; + // + // checkBoxTiling + // + this.checkBoxTiling.AutoSize = true; + this.checkBoxTiling.Location = new System.Drawing.Point(24, 392); + this.checkBoxTiling.Name = "checkBoxTiling"; + this.checkBoxTiling.Size = new System.Drawing.Size(52, 16); + this.checkBoxTiling.TabIndex = 51; + this.checkBoxTiling.Text = "Tiling"; + this.checkBoxTiling.UseVisualStyleBackColor = true; + // + // numericUpDownENSD + // + this.numericUpDownENSD.Location = new System.Drawing.Point(65, 441); + this.numericUpDownENSD.Maximum = new decimal(new int[] { + 10000000, + 0, + 0, + 0}); + this.numericUpDownENSD.Name = "numericUpDownENSD"; + this.numericUpDownENSD.Size = new System.Drawing.Size(85, 19); + this.numericUpDownENSD.TabIndex = 52; + // + // label10 + // + this.label10.AutoSize = true; + this.label10.Location = new System.Drawing.Point(24, 445); + this.label10.Name = "label10"; + this.label10.Size = new System.Drawing.Size(35, 12); + this.label10.TabIndex = 53; + this.label10.Text = "ENSD"; + this.toolTip.SetToolTip(this.label10, "Eta noise seed delta\r\n\r\nderault: 1\r\nNovelAI\'s default: 31337"); + // + // label11 + // + this.label11.AutoSize = true; + this.label11.Location = new System.Drawing.Point(24, 420); + this.label11.Name = "label11"; + this.label11.Size = new System.Drawing.Size(25, 12); + this.label11.TabIndex = 55; + this.label11.Text = "Clip"; + this.toolTip.SetToolTip(this.label11, "Clip skip\r\n\r\ndefault: 1\r\nNovelAI\'s default: 2"); + // + // numericUpDownClipSkip + // + this.numericUpDownClipSkip.Location = new System.Drawing.Point(65, 416); + this.numericUpDownClipSkip.Maximum = new decimal(new int[] { + 1000, + 0, + 0, + 0}); + this.numericUpDownClipSkip.Minimum = new decimal(new int[] { + 1, + 0, + 0, + 0}); + this.numericUpDownClipSkip.Name = "numericUpDownClipSkip"; + this.numericUpDownClipSkip.Size = new System.Drawing.Size(85, 19); + this.numericUpDownClipSkip.TabIndex = 54; + this.numericUpDownClipSkip.Value = new decimal(new int[] { + 1, + 0, + 0, + 0}); // // FormMain // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(794, 871); + this.Controls.Add(this.label11); + this.Controls.Add(this.numericUpDownClipSkip); + this.Controls.Add(this.label10); + this.Controls.Add(this.numericUpDownENSD); + this.Controls.Add(this.checkBoxTiling); + this.Controls.Add(this.checkBoxRestoreFace); this.Controls.Add(this.label9); this.Controls.Add(this.textBoxLayerName); this.Controls.Add(this.buttonLogWrite); @@ -676,6 +762,8 @@ private void InitializeComponent() ((System.ComponentModel.ISupportInitialize)(this.trackBarBatchCount)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxNewVersionExists)).EndInit(); this.groupBox1.ResumeLayout(false); + ((System.ComponentModel.ISupportInitialize)(this.numericUpDownENSD)).EndInit(); + ((System.ComponentModel.ISupportInitialize)(this.numericUpDownClipSkip)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); @@ -732,6 +820,12 @@ private void InitializeComponent() private System.Windows.Forms.Button buttonLogWrite; private System.Windows.Forms.TextBox textBoxLayerName; private System.Windows.Forms.Label label9; + private System.Windows.Forms.CheckBox checkBoxRestoreFace; + private System.Windows.Forms.CheckBox checkBoxTiling; + private System.Windows.Forms.NumericUpDown numericUpDownENSD; + private System.Windows.Forms.Label label10; + private System.Windows.Forms.Label label11; + private System.Windows.Forms.NumericUpDown numericUpDownClipSkip; } } diff --git a/src/IvyPhotoshopDiffusion/FormMain.cs b/src/IvyPhotoshopDiffusion/FormMain.cs index f8f05fe..1b37eb6 100644 --- a/src/IvyPhotoshopDiffusion/FormMain.cs +++ b/src/IvyPhotoshopDiffusion/FormMain.cs @@ -26,6 +26,9 @@ namespace Invary.IvyPhotoshopDiffusion //TODO: save last setting //TODO: dupe exec check? + //TODO: 1111 info text to setting + //TODO: NAI prompt conv + //TODO: ctrl+↑↓ at prompt inputing @@ -58,6 +61,10 @@ public FormMain() textBoxNegativePrompt.Text = XmlSetting.Current.LastNegativePrompt; textBoxLayerName.Text = XmlSetting.Current.LastLayerName; + numericUpDownClipSkip.Value = XmlSetting.Current.LastClipSkip; + numericUpDownENSD.Value = XmlSetting.Current.LastENSD; + + labelNoiseScale100.Text = $"{(double)trackBarNoiseScale100.Value / 100:0.##}"; trackBarNoiseScale100.ValueChanged += delegate @@ -178,6 +185,40 @@ public FormMain() + //transparent color button, set right click menu + { + ContextMenu menu = new ContextMenu(); + + menu.MenuItems.Add(new MenuItem("Get from Photoshop foreground color", delegate + { + dynamic appRef = Photoshop.CreateInstance(); + var color = Photoshop.GetForegroundColor(appRef); + buttonSetTransparentColor.BackColor = color; + XmlSetting.Current.TransparentColor = color; + })); + menu.MenuItems.Add(new MenuItem("Get from Photoshop background color", delegate + { + dynamic appRef = Photoshop.CreateInstance(); + var color = Photoshop.GetBackgroundColor(appRef); + buttonSetTransparentColor.BackColor = color; + XmlSetting.Current.TransparentColor = color; + })); + + menu.MenuItems.Add(new MenuItem("-")); + + menu.MenuItems.Add(new MenuItem("Set to Photoshop foreground color", delegate + { + dynamic appRef = Photoshop.CreateInstance(); + Photoshop.SetForegroundColor(appRef, XmlSetting.Current.TransparentColor); + })); + menu.MenuItems.Add(new MenuItem("Set to Photoshop background color", delegate + { + dynamic appRef = Photoshop.CreateInstance(); + Photoshop.SetBackgroundColor(appRef, XmlSetting.Current.TransparentColor); + })); + + buttonSetTransparentColor.ContextMenu = menu; + } @@ -225,6 +266,10 @@ public FormMain() XmlSetting.Current.LastNegativePrompt = textBoxNegativePrompt.Text; XmlSetting.Current.LastLayerName = textBoxLayerName.Text; + XmlSetting.Current.LastClipSkip = (int)numericUpDownClipSkip.Value; + XmlSetting.Current.LastENSD = (int)numericUpDownENSD.Value; + + XmlSetting.Current.Save(); TaskManager.AbortAll(); }; @@ -312,6 +357,12 @@ private void buttonGenerate_Click(object sender, EventArgs e) request.width = int.Parse((string)comboBoxWidth.SelectedItem); request.height = int.Parse((string)comboBoxHeight.SelectedItem); + request.restore_faces = checkBoxRestoreFace.Checked; + request.tiling = checkBoxTiling.Checked; + + request.override_settings = new Override_Settings(); + request.override_settings.CLIP_stop_at_last_layers = (int)numericUpDownClipSkip.Value; + request.override_settings.eta_noise_seed_delta = (int)numericUpDownENSD.Value; if (request.GetType() == typeof(JsonRequestImg2Img)) { diff --git a/src/IvyPhotoshopDiffusion/FormMain.resx b/src/IvyPhotoshopDiffusion/FormMain.resx index 1af4800..e388476 100644 --- a/src/IvyPhotoshopDiffusion/FormMain.resx +++ b/src/IvyPhotoshopDiffusion/FormMain.resx @@ -123,6 +123,13 @@ 17, 17 + + Background color in photoshop. +right click here, and you can get/set color in photoshop + +Attension: do not use transparent layer in photoshop, it cause error. +Use the transparent color setting here for locations you wish to treat as transparent. + diff --git a/src/IvyPhotoshopDiffusion/Photoshop.cs b/src/IvyPhotoshopDiffusion/Photoshop.cs index 7035592..4aab08e 100644 --- a/src/IvyPhotoshopDiffusion/Photoshop.cs +++ b/src/IvyPhotoshopDiffusion/Photoshop.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -56,6 +57,49 @@ public static bool SetSelection(dynamic appRef, double x, double y, double w, do } + public static Color GetForegroundColor(dynamic appRef) + { + //SolidColor + var color = appRef.ForegroundColor; + + //to RGBColor + var rgb = color.RGB; + + return Color.FromArgb((int)rgb.Red, (int)rgb.Green, (int)rgb.Blue); + } + + + public static Color GetBackgroundColor(dynamic appRef) + { + //SolidColor + var color = appRef.BackgroundColor; + + //to RGBColor + var rgb = color.RGB; + + return Color.FromArgb((int)rgb.Red, (int)rgb.Green, (int)rgb.Blue); + } + + public static void SetForegroundColor(dynamic appRef, Color color) + { + var tmp = appRef.ForegroundColor; + tmp.RGB.Red = color.R; + tmp.RGB.Green = color.G; + tmp.RGB.Blue = color.B; + appRef.ForegroundColor = tmp; + } + + public static void SetBackgroundColor(dynamic appRef, Color color) + { + var tmp = appRef.BackgroundColor; + tmp.RGB.Red = color.R; + tmp.RGB.Green = color.G; + tmp.RGB.Blue = color.B; + appRef.BackgroundColor = tmp; + } + + + public static RectangleDouble GetCurrentSelection(dynamic appRef) diff --git a/src/IvyPhotoshopDiffusion/XmlSetting.cs b/src/IvyPhotoshopDiffusion/XmlSetting.cs index 7ee49a9..9fd129b 100644 --- a/src/IvyPhotoshopDiffusion/XmlSetting.cs +++ b/src/IvyPhotoshopDiffusion/XmlSetting.cs @@ -20,7 +20,7 @@ public class XmlSetting [XmlIgnore] - public static int nVersion { get; } = 104; + public static int nVersion { get; } = 105; [XmlIgnore] public static string strVersion { get; } = $"Ver{nVersion}"; @@ -59,6 +59,8 @@ public class XmlSetting public string LastLayerName { set; get; } = "@date, seed=@seed, strength=@strength, cfg=@cfg, steps=@steps, @sampler, @prompt"; + public int LastClipSkip { set; get; } = 1; + public int LastENSD { set; get; } = 0;