I have the free version without solutions of Cho Chikun's life and death problems, and I want to convert them to sgf. They are in pdf format, and I'm wondering if someone has written software that takes a pdf in and spits an sgf file out.
If not, would it be possible to write such a utilty?
Re: Is there a .pdf to sgf utility?
Posted: Sat Sep 10, 2011 4:09 am
by danielm
A PDF will just have the problems as images, or (not likely) vector graphics. Analysing those to create SGF files may be theoretically possible, but practically not. Unless I am missing something and there is something special about this PDF.
The other way around it would be much easier.
Re: Is there a .pdf to sgf utility?
Posted: Sat Sep 10, 2011 5:47 am
by tchan001
The pdfs are made without answers because the problems are originally from a commercial software which used to be available from Kiseido. There was a freeware program which converted the problems from the software into sgf format.
This specific pdf is even simpler: copy the whole content from your pdf and paste it in a text editor, and you'll see. From that, it's not that hard to convert it to sgf.
Re: Is there a .pdf to sgf utility?
Posted: Mon Sep 12, 2011 2:15 pm
by kivi
Since long time I have these pdf's on my android phone and I do occasionally solve a few problems. But it would be more practical if they were sgf files, so that I can keep track of it easier, especially as there are now a few nice apps.
With lorill's finding, it is indeed not too difficult. Actually, if we emailed tasuki, he would probably send the original sgfs. Anyways, here is a simple python script for parsing text files. Feel free to correct or edit for purposes you see fit.
Usage = """ Usage: python tasuki2sgf.py textfile1 [textfile2 ...] E.g.: python tasuki2sgf.py cho2.txt cho3.txt gokyo.txt It will create seperate sgf files for each problem.
Explanation on <textfile>s: Download tasuki's tsumego pdf's. Open using a pdf reader, select all (e.g., Crtl-A), copy (Ctrl-C) and then paste to a new text file. This script is meant to parse that text file. """
import os import sys
def coord(i): return chr(ord('a') + i)
white_char = "!" black_char = "@"
def dia_to_sgf(problem, prob_id, foldername, comment): """ Takes a single problem as a list of lines in tasuki format. Writes (overwrites if exists) a sgf file with the name problem_<prob_id>.sgf """ black_indices = [] white_indices = [] for x, row_str in enumerate(problem): black_indices += [(x, y) for (y, ch) in enumerate(row_str) if ch == black_char] white_indices += [(x, y) for (y, ch) in enumerate(row_str) if ch == white_char]
f = open(foldername + "/problem_" + str(prob_id) + ".sgf", "w") f.write("(;GM[1]FF[4]SZ[19]") if len(comment) > 0: f.write("C[" + comment + "]") f.write("\nAB") for (x, y) in black_indices: f.write("[" + coord(y) + coord(x) + "]") f.write("\nAW") for (x, y) in white_indices: f.write("[" + coord(y) + coord(x) + "]") f.write("\n)") f.close()
# Get past the preamble for line in fhandle: if line[:6] == "tasuki": line = fhandle.next() # pass the date under "tasuki" print line.strip() break
problem = [] for line in fhandle: if len(line.strip()) == 0 or (line[0] >= "1" and line[0] <= "9"): # page numbers continue split_line = line.split() if len(split_line) >= 2 and split_line[0] == "problem": dia_to_sgf(problem, split_line[1].strip(","), foldername, " ".join(split_line[2:])) #print ".", problem = [] else: problem.append(line)
fhandle.close() print "done.\n"
def main(file_list): if len(file_list) < 1: print Usage else: for filename in file_list: file_to_collection(filename)
if __name__ == '__main__': main(sys.argv[1:])
Re: Is there a .pdf to sgf utility?
Posted: Mon Jun 09, 2014 4:01 pm
by Jaafar
Thanks a lot for your script kivi, I had to make some small changes for python 3.4.1 on windows (print() and next())
Here is the script that worked for me, it's almost the same
Usage = """ Usage: python tasuki2sgf.py textfile1 [textfile2 ...] E.g.: python tasuki2sgf.py cho2.txt cho3.txt gokyo.txt It will create seperate sgf files for each problem.
Explanation on <textfile>s: Download tasuki's tsumego pdf's. Open using a pdf reader, select all (e.g., Crtl-A), copy (Ctrl-C) and then paste to a new text file. This script is meant to parse that text file. """
import os import sys
def coord(i): return chr(ord('a') + i)
white_char = "!" black_char = "@"
def dia_to_sgf(problem, prob_id, foldername, comment): """ Takes a single problem as a list of lines in tasuki format. Writes (overwrites if exists) a sgf file with the name problem_<prob_id>.sgf """ black_indices = [] white_indices = [] for x, row_str in enumerate(problem): black_indices += [(x, y) for (y, ch) in enumerate(row_str) if ch == black_char] white_indices += [(x, y) for (y, ch) in enumerate(row_str) if ch == white_char]
f = open(foldername + "/problem_" + str(prob_id) + ".sgf", "w") f.write("(;GM[1]FF[4]SZ[19]") if len(comment) > 0: f.write("C[" + comment + "]") f.write("\nAB") for (x, y) in black_indices: f.write("[" + coord(y) + coord(x) + "]") f.write("\nAW") for (x, y) in white_indices: f.write("[" + coord(y) + coord(x) + "]") f.write("\n)") f.close()
# Get past the preamble for line in fhandle: if line[:6] == "tasuki": #line = fhandle.next() # pass the date under "tasuki" print (line.strip()) break
problem = [] for line in fhandle: if len(line.strip()) == 0 or (line[0] >= "1" and line[0] <= "9"): # page numbers continue split_line = line.split() if len(split_line) >= 2 and split_line[0] == "problem": dia_to_sgf(problem, split_line[1].strip(","), foldername, " ".join(split_line[2:])) #print ".", problem = [] else: problem.append(line)
fhandle.close() print ("done.\n")
def main(file_list): if len(file_list) < 1: print (Usage) else: for filename in file_list: file_to_collection(filename)
if __name__ == '__main__': main(sys.argv[1:])
Re: Is there a .pdf to sgf utility?
Posted: Mon Jun 09, 2014 4:38 pm
by RBerenguel
A few months ago I found someone had posted them already converted:
This is what I use now to generate the images for Anki, instead of recompiling Tasuki's TeX sources, which is more labor-intensive (not accounting for the effort taken in tweaking sgftopng.)