Lucian Posted September 5, 2016 Share Posted September 5, 2016 Hi, Is it a way to make a rectangular selection and then to save the coordinates to a text file? Quote Link to comment Share on other sites More sharing options...
BoltBait Posted September 5, 2016 Share Posted September 5, 2016 It wouldn't be too hard of an effect to write. What format would you like to save the coordinates in? (IOW, what would you like the text file to look like?) EDIT: It has been a few minutes and you haven't responded to my question. So, I wrote a generic CodeLab script that does what you want: // Name: Output Selection Coordinates // Author: BoltBait // Version: 1.0 #region UICode #endregion string OutputFilename = @"C:\Users\YourName\Desktop\output.txt"; void Render(Surface dst, Surface src, Rectangle rect) { Rectangle selection = EnvironmentParameters.GetSelection(src.Bounds).GetBoundsInt(); int CenterX = ((selection.Right - selection.Left) / 2)+selection.Left; int CenterY = ((selection.Bottom - selection.Top) / 2)+selection.Top; ColorBgra CurrentPixel; for (int y = rect.Top; y < rect.Bottom; y++) { if (IsCancelRequested) return; for (int x = rect.Left; x < rect.Right; x++) { CurrentPixel = src[x,y]; if (x == CenterX && y == CenterY) { if (!File.Exists(OutputFilename)) { using (StreamWriter sw = File.CreateText(OutputFilename)) { sw.WriteLine(selection.Left.ToString() + "," + selection.Top.ToString() + " " + selection.Right.ToString() + "," + selection.Bottom.ToString()); } } else { using (StreamWriter sw = File.AppendText(OutputFilename)) { sw.WriteLine(selection.Left.ToString() + "," + selection.Top.ToString() + " " + selection.Right.ToString() + "," + selection.Bottom.ToString()); } } } dst[x,y] = CurrentPixel; } } } 2 Quote Download: BoltBait's Plugin Pack | CodeLab | and a Computer Dominos Game Link to comment Share on other sites More sharing options...
Lucian Posted September 5, 2016 Author Share Posted September 5, 2016 It seems cool, but I'm pretty new to Paint.net and I don't see how to add the script. Please show me how. Also, it would be great if the text file is named as the image (just another extension, of course). Thank you. Quote Link to comment Share on other sites More sharing options...
Ego Eram Reputo Posted September 5, 2016 Share Posted September 5, 2016 Codelab is a paint.net plugin which creates other plugins. Find it by clicking the link in BoltBaits signature. You will need to compile the script he gave you to make your own plugin. If that seems too much trouble, I'd just write them down from the info in the status bar Quote ebook: Mastering Paint.NET | resources: Plugin Index | Stereogram Tut | proud supporter of Codelab plugins: EER's Plugin Pack | Planetoid | StickMan | WhichSymbol+ | Dr Scott's Markup Renderer | CSV Filetype | dwarf horde plugins: Plugin Browser | ShapeMaker Link to comment Share on other sites More sharing options...
Lucian Posted September 5, 2016 Author Share Posted September 5, 2016 Wow, it works! Thank you very much! I just have this problem with the output file: I need that the coordinates file to be saved near the image file, with the same name. Can you help me a little more? Quote Link to comment Share on other sites More sharing options...
BoltBait Posted September 5, 2016 Share Posted September 5, 2016 OK, here is a more complicated script that should do what you want. // Name: Output Selection Coordinates // Author: BoltBait // Version: 1.1 #region UICode byte Amount1 = 0; // [255] Select Current Image File #endregion string OutputFilename = ""; int PreviousBrowseButton = -1; string imgPath = null; void GetFileName() { OpenFileDialog ofd = new OpenFileDialog(); ofd.Title = "Select Image File"; ofd.Filter = "Image Files(*.PNG;*.BMP;*.JPG;*.GIF)|*.PNG;*.BMP;*.JPG;*.GIF|All files (*.*)|*.*"; ofd.DefaultExt = ".png"; ofd.Multiselect = false; if (ofd.ShowDialog() == DialogResult.OK) { imgPath = ofd.FileName; OutputFilename = Path.ChangeExtension(imgPath, ".txt"); } else { OutputFilename = ""; } } void Render(Surface dst, Surface src, Rectangle rect) { if (PreviousBrowseButton == -1) { PreviousBrowseButton = Amount1; } if (PreviousBrowseButton != Amount1) { PreviousBrowseButton = Amount1; Form IndirectUIForm = Form.ActiveForm; if (IndirectUIForm != null) { IndirectUIForm.Invoke(new Action(delegate () { GetFileName(); })); } } Rectangle selection = EnvironmentParameters.GetSelection(src.Bounds).GetBoundsInt(); int CenterX = ((selection.Right - selection.Left) / 2)+selection.Left; int CenterY = ((selection.Bottom - selection.Top) / 2)+selection.Top; ColorBgra CurrentPixel; for (int y = rect.Top; y < rect.Bottom; y++) { if (IsCancelRequested) return; for (int x = rect.Left; x < rect.Right; x++) { CurrentPixel = src[x,y]; if (x == CenterX && y == CenterY) { if (OutputFilename != "") { if (!File.Exists(OutputFilename)) { using (StreamWriter sw = File.CreateText(OutputFilename)) { sw.WriteLine(selection.Left.ToString() + "," + selection.Top.ToString() + " " + selection.Right.ToString() + "," + selection.Bottom.ToString()); } } else { using (StreamWriter sw = File.AppendText(OutputFilename)) { sw.WriteLine(selection.Left.ToString() + "," + selection.Top.ToString() + " " + selection.Right.ToString() + "," + selection.Bottom.ToString()); } } } } dst[x,y] = CurrentPixel; } } } How to use Download the following zip file (I used CodeLab to compile it for you): WriteSelectionCoordinatesToFile.zip Unzip it to your desktop. Run the enclosed Install_.bat file to install the effect into your paint.net Open an image in paint.net and make a selection. Run the effect. Click the button to select your image file and click OK. (This is necessary because effects do not know the name of the open file.) Click OK on the effect dialog box to finalize your effect and write the coordinates to the file. Hope this helps! 1 Quote Download: BoltBait's Plugin Pack | CodeLab | and a Computer Dominos Game Link to comment Share on other sites More sharing options...
MJW Posted September 5, 2016 Share Posted September 5, 2016 This is very useful code, which may someday save me quite a bit of work. Perhaps it could be also posted in Plugin Developer's Central. Quote Link to comment Share on other sites More sharing options...
BoltBait Posted September 5, 2016 Share Posted September 5, 2016 I don't really feel like writing a tutorial on this one. You better just bookmark it. The script actually has issues. For example, I can't get it to append text to the existing file. My intent was to make a selection, run the effect. Make another selection, run the effect again with the result being 2 lines in the output file. It doesn't work... and I'm too lazy to debug it. Quote Download: BoltBait's Plugin Pack | CodeLab | and a Computer Dominos Game Link to comment Share on other sites More sharing options...
Lucian Posted September 6, 2016 Author Share Posted September 6, 2016 But it's fine, it does append text to the existing file. The only inconvenience is that I need to select the file at least two times: when I open the image and when I save the coordinates. Any chance for something like this? - I run the script - it requests an image file - I select the image file - it opens it - it waits - I make the selection - it saves it to the text file Quote Link to comment Share on other sites More sharing options...
BoltBait Posted September 6, 2016 Share Posted September 6, 2016 Nope. Trust me, this is the most efficient I can get it... given the limitations of the effects system. Quote Download: BoltBait's Plugin Pack | CodeLab | and a Computer Dominos Game Link to comment Share on other sites More sharing options...
Lucian Posted September 6, 2016 Author Share Posted September 6, 2016 I see. But maybe we can suggest an improvement to Paint.net developers. It should be pretty easy to expose the file name. And it would be useful in many situations. Quote Link to comment Share on other sites More sharing options...
BoltBait Posted September 7, 2016 Share Posted September 7, 2016 OK, don't tell Rick that I did this, but here is a plugin that is NOT well behaved that does what you want. I'll leave it to you to build using CodeLab. // Name: Output Selection Coordinates // Author: BoltBait // Version: 2.0 #region UICode #endregion string LatestFileName() { string filename = Form.ActiveForm.Owner.Text; filename = filename.Substring(0, filename.IndexOf(" - ")); if (filename != "Untitled") { for (int i=7; i>=0; i--) { string regpath = @"HKEY_CURRENT_USER\Software\Paint.NET"; string regkey = "File/MostRecent/Path" + i.ToString(); string regval = (string)Registry.GetValue(regpath,regkey,"Untitled"); if (filename == Path.GetFileName(regval)) { return regval; } } } return filename; } void GetFileName() { OpenFileDialog ofd = new OpenFileDialog(); ofd.Title = "Select Image File"; ofd.Filter = "Image Files(*.PNG;*.BMP;*.JPG;*.GIF)|*.PNG;*.BMP;*.JPG;*.GIF|All files (*.*)|*.*"; ofd.DefaultExt = ".png"; ofd.Multiselect = false; if (ofd.ShowDialog() == DialogResult.OK) { imgPath = ofd.FileName; OutputFilename = Path.ChangeExtension(imgPath, ".txt"); } else { OutputFilename = ""; } } string OutputFilename = ""; string imgPath = null; void Render(Surface dst, Surface src, Rectangle rect) { if (OutputFilename == "Cancelled") return; if (OutputFilename == "") { OutputFilename = LatestFileName(); if (OutputFilename == "Untitled" || OutputFilename == "") { Form IndirectUIForm = Form.ActiveForm; if (IndirectUIForm != null) { IndirectUIForm.Invoke(new Action(delegate () { GetFileName(); })); } } else { OutputFilename = Path.ChangeExtension(OutputFilename, ".txt"); } } if (OutputFilename == "") { OutputFilename = "Cancelled"; return; } if (!Path.IsPathRooted(OutputFilename)) { string dir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); OutputFilename = Path.Combine(dir, Path.GetFileName(OutputFilename)); } Rectangle selection = EnvironmentParameters.GetSelection(src.Bounds).GetBoundsInt(); int CenterX = ((selection.Right - selection.Left) / 2)+selection.Left; int CenterY = ((selection.Bottom - selection.Top) / 2)+selection.Top; string OutputData = selection.Left.ToString() + "," + selection.Top.ToString() + " " + selection.Right.ToString() + "," + selection.Bottom.ToString(); ColorBgra CurrentPixel; for (int y = rect.Top; y < rect.Bottom; y++) { if (IsCancelRequested) return; for (int x = rect.Left; x < rect.Right; x++) { CurrentPixel = src[x,y]; if (x == CenterX && y == CenterY) { if (OutputFilename != "") { if (!File.Exists(OutputFilename)) { using (StreamWriter sw = File.CreateText(OutputFilename)) { sw.WriteLine(OutputData); } } else { using (StreamWriter sw = File.AppendText(OutputFilename)) { sw.WriteLine(OutputData); } } Form IndirectUIForm = Form.ActiveForm; if (IndirectUIForm != null) { IndirectUIForm.Invoke(new Action(delegate () { MessageBox.Show("Data saved to file:\r\n" + OutputFilename + "\r\n\r\nData: " + OutputData); })); } } } dst[x,y] = CurrentPixel; } } } And, you fellow Effect authors, avert your eyes! Do not follow down the path of the unrighteous! I'm serious! Rick will probably ban me for posting such unclean code. Press your browser's back button now before you become corrupted! MJW, don't you dare use this! 2 Quote Download: BoltBait's Plugin Pack | CodeLab | and a Computer Dominos Game Link to comment Share on other sites More sharing options...
MJW Posted September 7, 2016 Share Posted September 7, 2016 I'd upvote it, but I don't want to get banned along with you. Quote Link to comment Share on other sites More sharing options...
Lucian Posted September 7, 2016 Author Share Posted September 7, 2016 I understand the trick. You are great! I upvote it, I'm not afraid! I hope everyone will see this as a temporary workaround, until the filename is exposed to plugins. Quote Link to comment Share on other sites More sharing options...
Eli Posted September 7, 2016 Share Posted September 7, 2016 Quote Link to comment Share on other sites More sharing options...
Ego Eram Reputo Posted September 7, 2016 Share Posted September 7, 2016 Yeah Eli. Let that be a lesson to you! Quote ebook: Mastering Paint.NET | resources: Plugin Index | Stereogram Tut | proud supporter of Codelab plugins: EER's Plugin Pack | Planetoid | StickMan | WhichSymbol+ | Dr Scott's Markup Renderer | CSV Filetype | dwarf horde plugins: Plugin Browser | ShapeMaker Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.