Scan2CAD Python API¶
Quickstart¶
Extract raster images from PDF files:
in_dir = "path/to/input_directory"
pdfs = s2c.list(in_dir, "pdf")
for p in pdfs:
pp = p.open()
pp.save(s2c.FileType.RASTER, s2c.join(in_dir, p.get_name()))
Vectorize images in a directory:
in_dir = "path/to/input_directory"
preset = s2c.Preset()
imgs = s2c.list(in_dir, "png")
for i in imgs:
r = i.open()
v = r.vectorize(preset)
v.save(r.path)
Example scripts¶
List of classes¶
Classes used for accessing files¶
-
class
s2c.
FilePath
(path)¶ Holds file’s path. Use this to load files with
open()
method.Currently we support three types of files:
Raster()
,Vector()
,PDF()
.-
get_path
()¶ Retruns path provided during initialization (can be relative or absolute)
-
get_absolute_path
()¶ Returns absolute path
-
get_full_name
()¶ Returns name with extension
-
get_name
()¶ Returns name without extension
-
get_type
()¶ Returns file extension
-
-
class
s2c.
File
(path)¶ Base class for all file types that are supported
-
class
s2c.
Raster
(path: s2c.FilePath)¶ Class representing raster files
-
threshold
(value=127)¶ Applies threshold raster effect on this image
-
segment
(colors=6, definition=3)¶ Applies segment raster effect on this image
-
rotate90
(clockwise=True)¶ Rotates raster image by 90 degrees in requested direction
-
remove_speckles_and_holes
(speckle_size, hole_size)¶ Applies raster effect: remove speckles and holes
-
smooth
()¶ Applies raster effect: smooth
-
auto_deskew
()¶ Applies raster effect: auto deskew
-
thicken
()¶ Applies raster effect: thicken
-
thin
(level)¶ Applies raster effect: thin
-
save
(path=None)¶ Saves this raster image under the given path
-
vectorize
(preset) → s2c.Vector¶ Vectorizes this image and returns
Vector()
object
-
-
class
s2c.
Vector
(path)¶ -
scale
(length_in_pixels, length_in_unit, unit: s2c.Unit)¶ Sets vector scale in requested unit
-
save
(path=None, type=1)¶ Saves this vector image under the given path
-
exportToPdf
(path)¶ Saves this vector image to PDF file
-
rasterize
(path)¶ Saves this vector to as raster file
-
-
class
s2c.
PDF
(path)¶ Class representing PDF files
Loads PDF file using provided FilePath instance
-
save
(type, path)¶ Saves PDF file in one of supported formats, see
FileType()
class
-
Utilities¶
-
class
s2c.
list
(path, extension='')¶ Lists files in the directory.
- Parameters
extension – can be a string or a list of strings.
-
class
s2c.
join
(path, *paths)¶ Joins paths together using platform-specific path separators
-
class
s2c.
PDFsInDirToDxfs
(in_dir, out_dir)¶ Extracts vector data in DXF format from all PDF files in the directory
-
class
s2c.
FileType
¶ Enumeration with all types supported by FilePath and File (Raster, Vector, PDF)
-
RASTER
= 0¶
-
VECTOR
= 1¶
-
PDF
= 2¶
-
-
class
s2c.
Preset
¶ Structure used to define settings used in vectorization
- In order to change advanced settings, modify these members:
self.technical
,self.ocr
,self.outline
,self.solid
.
-
class
Predefined
¶ -
Architectural
= 1¶
-
Electrical
= 2¶
-
Mechanical
= 3¶
-
CNCProfile
= 4¶
-
SitePlan
= 5¶
-
ContourMap
= 6¶
-
Sketch
= 7¶
-
Outline
= 8¶
-
-
class
Languages
¶ -
English
= 1¶
-
German
= 2¶
-
French
= 3¶
-
Italian
= 4¶
-
Japanese
= 5¶
-
JapaneseEnglish
= 6¶
-
Polish
= 7¶
-
Spanish
= 8¶
-
-
class
Technical
¶ Structure holding additional settings for Technical vectorization
-
polyline_identification
= False¶
-
line_width
= 14¶
-
line_deviation_factor
= 2.2¶
-
arc_identification
= True¶
-
arc_min_radius
= 6¶
-
bezier_identification
= False¶
-
circle_identification
= True¶
-
circle_radius
= 6¶
-
arrow_identification
= True¶
-
arrow_max_head_length
= 64¶
-
dash_line_identification
= True¶
-
dash_line_max_length
= 45¶
-
dash_line_max_gap
= 45¶
-
hatch_identification
= True¶
-
hatch_angle
= 45¶
-
hatch_angle_tolerance
= 12¶
-
orthogonal_snap
= True¶
-
orthogonal_snap_tolerance
= 8¶
-
snap_to_30
= False¶
-
snap_to_45
= False¶
-
snap_to_60
= False¶
-
snap_to_120
= False¶
-
snap_to_135
= False¶
-
snap_to_150
= False¶
-
snap_tolerance
= 8¶
-
corner_snap
= 8¶
-
corner_snap_tolerance
= 6¶
-
pass_thro_snap
= 3¶
-
gap_jump_distance
= 4¶
-
loose_ends_length
= 7¶
-
connect_thro_vectors
= True¶
-
-
class
OCR
¶ Structure holding additional settings for OCR
-
char_size_max
= 75¶
-
char_size_min
= 9¶
-
recognition
= 60¶
-
exclude_intersecting
= True¶
-
vertical
= False¶
-
angular
= False¶
-
document_is_only_text
= False¶
-
-
class
Outline
¶ Structure holding additional settings for Outline vectorization
-
definition
= 20¶
-
bias_orthogonal
= False¶
-
bias
= 2¶
-
type
= 4¶
-
-
class
Solid
¶ Structure holding additional settings for Solid vectorization
-
definition
= 5¶
-
identify_circles
= True¶
-
bias_orthogonal
= False¶
-
bias
= 2¶
-
type
= 2¶
-
-
method
= 1¶ Determines vectorization mode.
-
option
= 1¶ Determines option type (relevant only for Technical vectorization).
-
language
= 1¶ Determines OCR language (relevant only when OCR is enabled).
-
color
= False¶ If enabled output vector will contain color. Ingored for
Method.Solid()
.
-
load
(pre: s2c.Preset.Predefined)¶ Preloads chosen preset