Rotate and scale a complete .SVG document using Python - python-2.7

I have a SVG drawing (from a Building Map) and I want to rotate the complete document 90 degrees clockwise. Now, the drawing orientation is portrait, the idea is to have a landscape orientation.
Besides of this, I would like to scale the complete document (so including all elements).
For now, I could not find the possibilties for doing this on the web. So that is why I am asking overhere. My questions are:
Is it possible?
If yes, in what way can this be done? And who wants to help with this issue.

I managed to rotate an SVG figure with svgutils module. It can be installed with pip.
>>> import svgutils
>>> svg = svgutils.transform.fromfile('camera.svg')
>>> originalSVG = svgutils.compose.SVG('camera.svg')
>>> originalSVG.rotate(90)
>>> originalSVG.move(svg.height, 10)
<svgutils.compose.SVG object at 0x7f11dc78fb10>
>>> figure = svgutils.compose.Figure(svg.height, svg.width, originalSVG)
>>> figure.save('svgNew.svg')
Note that width and height attributes must be specified in original svg file in svg tag
Reference I used
Actually, this method didn't do anything with elements except wrapping them all with g tag with transform attribute. But it seems that with this module you can access each and every element in SVG tree and do whatever you want with them.
Scaling an SVG is also easy:
>>> originalSVG.scale(2)
<svgutils.compose.SVG object at 0x7f11dc78fb10>
>>> figure = svgutils.compose.Figure(float(svg.height) * 2, float(svg.width) * 2, originalSVG)
>>> figure.save('svgNew.svg')

Related

PIL - how to insert an index, or subscript, into text?

Like this:
Calculating coordinates looks not so good, maybe there is a better way?
This code works fine (), but it's complicated always calculate where to place index for each letter.
image = Image.new('I', (300, 100), "white").convert('RGBA')
font = ImageFont.truetype(font=r"C:\Windows\Fonts\Arial.ttf", size=39)
draw = ImageDraw.Draw(image, 'RGBA')
draw.text((10, 10), "P", fill="black", font=font, align="center")
font = ImageFont.truetype(font=r"C:\Windows\Fonts\Arial.ttf", size=20)
draw.text((25, 35), "2", fill="black", font=font, align="center")
image.save(output_folder + 'test.png')
One possibility for you might be to use ImageMagick which understands Pango Markup Language - which looks kind of like HTML.
So, at the command-line you could run this:
convert -background white pango:'<span size="49152">Formula: <b>2P<sub><small><small>2</small></small></sub>O<sub><small><small>5</small></small></sub></b></span>' formula.png
which produces this PNG file:
Change to -background none to write on a piece of transparent canvas if you want to preserve whatever is underneath the text in your original image.
You can also put all the markup in a separate text file, called say "pango.txt" like this:
<span size="49152">Formula: <b>2P<sub><small><small>2</small></small></sub>O<sub><small><small>5</small></small></sub></b></span>
and pass that into ImageMagick like this:
convert pango:#pango.txt result.png
You could shell out and do this using:
subprocess.call()
Then you can easily load the resultant image and composite/paste it in where you want it - that would take about 3 lines of Python that you could put in a function.
Here is a further example of an image generated with Pango by Anthony Thyssen so you can see some of the possibilities:
There is loads of further information on Pango by Anthony here.
Note that there are also Python bindings for ImageMagick but I am not very familiar with them, but that may be cleaner than shelling out.
Keywords: Pango, PIL, Pillow, Python, markup, subscript, superscript, formula, chemical formulae, ImageMagick, image, image processing, SGML, HTML.
You can also do this sort of thing using Mathtext in Matplotlib:
#!/usr/bin/env python3
import matplotlib.pyplot as plt
plt.axes([0.025, 0.025, 0.95, 0.95])
# Some formula with superscripts, subscripts, square roots, fractions and integrals
eq = r"$ 2P_2 O_5 + H^{2j}$"
size = 50
x,y = 0.5, 0.5
alpha = 1
params = {'mathtext.default': 'regular' }
plt.rcParams.update(params)
plt.text(x, y, eq, ha='center', va='center', color="#11557c", alpha=alpha,
transform=plt.gca().transAxes, fontsize=size, clip_on=True)
# Suppress ticks
plt.xticks(())
plt.yticks(())
# Save on transparent background
plt.savefig('result.png', transparent=True)
You can also save the output in a memory buffer (without going to disk) and then use that in your PIL-based image processing.
Note that I have explicitly named and assigned all the parameters (x, y, size and alpha) so you can play with them and that makes the code look longer and more complicated than it actually is.
Keywords: Python, PIL, Pillow, maths, mathematical symbols, formula with superscripts, subscripts, square roots, fractions and integrals.

Find the width of an ink stroke in an image using OpenCV & C++

I have the following sample of handwriting taken with three different writing instruments:
Looking at the writing, I can tell that there is a distinct difference between the first two and the last one. My goal is to determine an approximation of the stroke thickness for each letter, allowing me to group them based on being thin or thick.
So far, I have tried looking into stroke width transform, but I have struggled to translate it to my example.
I am able to preprocess the image such that I am just left with just the contours of the test in question. For example, here is thick from the last line:
I suggest detecting contours with cv::findContours as you are doing and then compare bounding rectangle area and contour area. The thicker writing the greater coefficent (contourArea/boundingRectArea) will be.
This approach will help you. This will calcuate the stroke width.
from skimage.feature import peak_local_max
from skimage import img_as_float
def adaptive_thresholding(image):
output_image = cv2.adaptiveThreshold(image,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,21,2)
return output_image
def stroke_width(image):
dist = cv2.distanceTransform(cv2.subtract(255,image), cv2.DIST_L2, 5)
im = img_as_float(dist)
coordinates = peak_local_max(im, min_distance=15)
pixel_strength = []
for element in coordinates:
x = element[0]
y = element[1]
pixel_strength.append(np.asarray(dist)[x,y])
mean_pixel_strength = np.asarray(pixel_strength).mean()
return mean_pixel_strength
image = cv2.imread('Small3.JPG', 0)
process_image = adaptive_thresholding(image)
stroke_width(process_image)
A python implementation for this might go something like this, using Stroke Width Transform implementation of SWTloc.
Full Disclosure: I am the author of this library.
EDIT : Post v2.0.0
Transforming The Image
import swtloc as swt
imgpath = 'images/path_to_image.jpeg'
swtl = swt.SWTLocalizer(image_paths=imgpath)
swtImgObj = swtl.swtimages[0]
# Perform SWT Transformation with numba engine
swt_mat = swtImgObj.transformImage(auto_canny_sigma=1.0, gaussian_blurr=False,
minimum_stroke_width=3, maximum_stroke_width=50,
maximum_angle_deviation=np.pi/3)
Localize Letters
localized_letters = swtImgObj.localizeLetters()
Plot Histogram of Each Letters Strokes Widths
import seaborn as sns
import matplotlib.pyplot as plt
all_sws = []
for letter_label, letter in localized_letters.items():
all_sws.append(letter.stroke_widths_mean)
sns.displot(all_sws, bins=31)
From the distribution plot, it can be inferred that there might be three fontsize of the text available in the image - [3, 15, 27]

how to center allign a text in a custom x y coordinate using reportlab

I am trying to develop one python app. the app should print text in an a4 page and on that a4 sheet already there are 4 rectangle boxes, i have to put text in center allign format in each box. the image is attached as reference.sample image
i have written something like this using reportlab.
self.canvas = canvas.Canvas(name, pagesize=landscape(A4))
self.canvas.drawCentredString(x1,y1,"c1")
but i am not achieving my goal.
rlhelper has some useful functions which may be of assistance here.
If you can identify the mid-point x-coordinate of each box, the following function can work:
import rlhelper
simpleCentredstring('canvas', 'x', 'y', 'string', 'fontcolor', 'font (+weight)','fontsize')

networkx graph display using matplotlib- missing labels

I am writing a program which generates satisfiable models (connected graphs) for a specific input string. The details here are not important but the main problem is that each node has a label and such label can be lengthy one. So, what happens is that it does not fit into the figure which results in displaying all the nodes but some labels are partly displayed... Also, the figure that is displayed does not provide an option to zoom out so it is impossible to capture entire graph with full labels on one figure.
Can someone help me out and perhaps suggest a solution?
for i in range(0,len(Graphs)):
graph = Graphs[i]
custom_labels={}
node_colours=['y']
for node in graph.nodes():
custom_labels[node] = graph.node[node]
node_colours.append('c')
#nx.circular_layout(Graphs[i])
nx.draw(Graphs[i], nx.circular_layout(Graphs[i]), node_size=1500, with_labels=True, labels = custom_labels, node_color=node_colours)
#show with custom labels
fig_name = "graph" + str(i) + ".png"
#plt.savefig(fig_name)
plt.show()
Update picture added:
You could scale the figure
import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
G.add_edge('a'*50,'b'*50)
nx.draw(G,with_labels=True)
plt.savefig('before.png')
l,r = plt.xlim()
print(l,r)
plt.xlim(l-2,r+2)
plt.savefig('after.png')
before
after
You could reduce the font size, using the font_size parameter:
nx.draw(Graphs[i], nx.circular_layout(Graphs[i]), ... , font_size=6)

GeoDjango: How to create a circle based on point and radius

I have the following (simplified) Model:
class Zone(gismodels.Model):
name = gismodels.CharField()
poly = gismodels.PolygonField()
I want to create and save a polygon that represents a circle, based upon a given point and radius.
The only way I can figure out how to achieve this, is to call the postgis ST_Buffer function using raw SQL. I'm really hoping that there is another way.
Is it possible to access the GEOS buffer methods?
Yes, it is possible to use the geos buffer method:
>>> from django.contrib.gis import geos
>>> center = geos.Point(5, 5)
>>> radius = 2
>>> circle = center.buffer(radius)
>>> circle
<Polygon object at 0x1029d8370>
The radius here is in the same units as the coordinates of the points. This will work for some coordinate systems like UTM, but not as well for others.
Also, while this is appropriate for constructing a circular geometry, the PostGIS documentation notes that for doing radius searches ST_DWithin is more efficient.
I spent a ridiculous amount of time trying to get this working. Since this is the number one google search result, here's what worked for me:
radius_km = radius*1.609 # convert miles to km
point = target.geolocation # a django PointField using SRID 4326
# re-project point to a flat coordinate system
# so we can use meters instead of degrees below,
# AND get an actual circle instead of oval
point.transform(6347)
poly = point.buffer(radius_km*1000) # get a circular polygon from radius
poly.transform(4326)# re-project the resulting polygon back
Bonus: If you're doing this so you can get a circle on a google static map, grab polyline:
import polyline
import ast
geo = ast.literal_eval(poly.geojson) # turn the text into a dict
points = geo['coordinates'][0]
pl = polyline.encode(points, geojson=True, precision=5) # make a polyline out of the polygon for google
map_url += '&path=color:0x00000000%7Cfillcolor:0x0000AA33%7Cweight:1%7Cenc:' + pl