Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Script to draw line and/or take measurements from pixel coordinates

New Here ,
Dec 01, 2021 Dec 01, 2021

Hello again.

 

I've recently asked how to save a set of coordinates in pixels (X;Y) to a .txt/.csv using a script, and got several successful replies to solve my previous problem. I now have a follow-up.

 

My goal would be to create a script that would allow me to draw lines between two coordinate-specific points, indicated with X;Y coordinates (in pixels) on the image. I have found several scripts online that gave me some hints about the general setup for such a script, but ideally I would like one that could "continue the work" started by the previous script. In other words, I would like to be able to draw lines between the specific points-coordinates that I marked and saved in the .txt/.csv file. Alternatively, a script that does both things at once (export the pixel coordinates to a .txt/.csv, and draw the lines).

 

The lines would, ideally, not always be consecutive. In other words, I wouldn't want a continuous line that goes from point 1 to point 2 to point 3 to point 4 and so on. Rather, a line from point 1 to 2, then 3 to 4, and so on. This probably complicates things a lot with the script, so the most versatile solution, I think, would be a script capable of reading the X;Y coordinates from a .txt/.csv and then draw from there.

 

My current "workaround" to this problem is to manually compile a script where I would draw each individual line, using the coordinates found and saved in the .txt/.csv. In other words, a "basic template" like this:

 

drawLine([startX1,startX2],[endX1,endY1],3);
drawLine([startX1,startX2],[endX2,endY2],3);

[...]

 

This should allow me to draw separate lines just where I need them. However, as it goes without saying, replacing the coordinates manually like that is a bit slow, and would also require to make a separate script for each "drawing" I need to make. If, however, there was a way to automatize that...for example, telling the script that "startX1" values are found in the A2 cell of the .csv file, or that the "startX1;startY1" coordinates are always the ones from the first point marked with the Count Tool...I don't know >.<

 

So...any ideas? My best "solution" is trying to automatize the process with autohotkey (so I'd first save the coordinates from the .csv, then have autohotkey compile a script with copy-pastes, and lastly run the script in photoshop to draw the lines), but it's evidently not optimal, nor particularly elegant.

 

A further evolution would be a script capable of also taking the pixel measurement between the marked points. To be honest this can very easily be done simply with an excel formula and a bit of trigonometry, after getting the X;Y coordinates exported to a .csv -- doing it with a script would just be for "elegance's sake", but I don't really need it. If there is an easy way to do it, then it'd be really nice to have that too; if not, nevermind.

 

Bonus last question: is there maybe a way to rename the "labels" used by the Count Tool? The script I'm using now to save the points coordinates uses the Count Tool to mark the points with numbers (1,2,3,4,5,...). I tried searching online and didn't find anything about a way to custom-rename the "name of the points" (labels) of a count, so I'm guessing there is no way to do it. Anyway, it would have been a nice extra touch to be able to mark the points with custom labels (in a fixed order), for example "bottle tip", "dish center", and so on. Really not that important though 😛

TOPICS
Actions and scripting
1.8K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Community Expert ,
Dec 02, 2021 Dec 02, 2021

It is not a problem to raw  a line from point a to b and then at a later date draw a line  from  point c to d.  The issue is on what layer(s) are the lines  drawn.  If you want them visible it most likely should be on a single layer that is the top layer in the document or documents stack.  You cans record the information in your external  CSV file  you  could also record thee line information in the document meta data.  The thing is  its a Photoshop  document and things can be changed in the document like Canvas can be added and removed.  The layer and its lines will be fine.  The question is how would you maintain the line description in your CSV file  your lines meta data information should you change the document canvas. How is the document being maintained is it even a layered document?  I would think you may  want the lines separate from the image data

JJMack
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 02, 2021 Dec 02, 2021

Hi again, thanks for the reply.

The lines can all be drawn on the same layer (ideally a new layer on top of the basic starting image which I would use to take the measurement points). Yes, I would want them to be visible, so top layer.

I'm not quite sure I understand your question about maintaining the line descriptions. Basically, it might happen that after creating a .csv with the original (X;Y) coordinates of points, I might want to change one of those coordinates (for example I want to move the fourth point, so I would change the X;Y coordinates of the fourth entry in the .csv list). I don't think it's a layered document simply because I don't really know what it is 😛 The coordinates right now are exported to just a normal .csv file which I open with excel.

 

So, are you saying there is a way to "inject" the start and end point of lines into a script, using a .csv document? Directly or indirectly, it wouldn't really matter much as long as it's an automatizable process.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 02, 2021 Dec 02, 2021

Oh, and just to be clear, I wouldn't really ever need to modify the canvas or image size, so the pixels are always gonna have the same coordinates for the same point of the image. What might happen is that I mistakely draw one of the points, so I take the wrong coordinates of one of the points, and I might have to go back to change its coordinates (and only its coordinates; the other points would remain the same).

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 02, 2021 Dec 02, 2021

If you would want to change a line  it would be best to have script clear out the top line layer then draw all the line with the new current  changed coordinates

JJMack
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 02, 2021 Dec 02, 2021

Ok. But what's the script to draw lines using the values from .csv? I only know how to export coordinates from photoshop to a .csv :s

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 02, 2021 Dec 02, 2021

The Script you program.  The script would need to read your CVS and parse it  to add or modify  the top line layer.  Like you Photoshop by recording and playrig actions you also automate Photoshop by writing Photoshop scripts and runnint them.

JJMack
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 02, 2021 Dec 02, 2021

Maybe this wasn't clear, but I know how to use a script. I don't know how to program/write one, and can't spare the time to spend dozens of hours to figure something that more experienced users would be able to do in 5 minutes to help me. I was hoping someone could help me and share the actual lines of code to go from a .cvs (X;Y) set of coordinates to drawing, but I must have used all my luck when I got all those nice answers in the other topic, and ain't got any left to get a response this time.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 02, 2021 Dec 02, 2021

Or perhaps this task is more complex and takes longer than your guesstimate of 5 minutes! :]

 

If you invest the time to find code that comes close, somebody with greater knowledge may take the time to change it, when they don't have the time to do so from scratch.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Dec 03, 2021 Dec 03, 2021

There is some uncertainty in the problem statement. Initially, it was required to get the coordinates under the cursor. We did it. Now it turns out that these coordinates should be used to draw straight lines.

 

The question arises - why not use the pen tool and paths: the tool will allow you to solve both problems at once. On the one hand, we can accurately indicate the position of each point, on the other hand, we get a clear relationship between the points (the order of the points, the presence of a line between the points).

2021-12-03_11-24-48.png

Paths can be automatically drawn using drawing tools, copied between files, and exported to other formats. In particular, the built-in function Export paths for Illustrator allows you to save paths (essentially) in text form.

 

%Adobe_Photoshop_Path_Begin:<< /defaultFill false >>
*u
%AI3_Note:<< /operation /xor /defaultFill false >>
1 XR
518.7600 927.7200 m
428.0400 970.5600 l
348.4345 860.3102 355.3200 857.1600 v
362.2055 854.0098 553.6800 812.1600 y
N
*U
%Adobe_Photoshop_Path_End

 

It is not clear why a script is needed here, and in particular a CSV file?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 03, 2021 Dec 03, 2021
LATEST

It is clear now you can not spare the time to learn how to do what you want to do for it requite to many hours to learn. That is life on this planet  you have a limited amount of time you do not have time to do all you want to do. Have you considered hiring someone to do work you want done.

JJMack
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines