Seite 1 von 1

Pillow Linien sind unterschiedlich dick

Verfasst: Sonntag 27. Juni 2021, 12:46
von cube2000
Moin
Wieso wenn man mit Pillow eine Linie zeichnet die Linien unterschiedlich dick?
Hier ein Beispiel:
https://ibb.co/j4QQrH6
Weiss jemand wie ich es hin bekomme, dass alle Linien ungefähr gleich aussehen?
Mit Freundlichen Grüssen

Mein Code:

from PIL import Image, ImageDraw

import math


class DashedImageDraw(ImageDraw.ImageDraw):

def thick_line(self, xy, direction, fill=None, width=0):

if xy[0] != xy[1]:
self.line(xy, fill=fill, width=width)
else:
x1, y1 = xy[0]
dx1, dy1 = direction[0]
dx2, dy2 = direction[1]
if dy2 - dy1 < 0:
x1 -= 1
if dx2 - dx1 < 0:
y1 -= 1
if dy2 - dy1 != 0:
if dx2 - dx1 != 0:
k = - (dx2 - dx1) / (dy2 - dy1)
a = 1 / math.sqrt(1 + k ** 2)
b = (width * a - 1) / 2
else:
k = 0
b = (width - 1) / 2
x3 = x1 - math.floor(b)
y3 = y1 - int(k * b)
x4 = x1 + math.ceil(b)
y4 = y1 + int(k * b)
else:
x3 = x1
y3 = y1 - math.floor((width - 1) / 2)
x4 = x1
y4 = y1 + math.ceil((width - 1) / 2)
self.line([(x3, y3), (x4, y4)], fill=fill, width=1)
return

def dashed_line(self, xy, dash=(4, 4), fill=None, width=0):
# xy – Sequence of 2-tuples like [(x, y), (x, y), ...]
for i in range(len(xy) - 1):
x1, y1 = xy
x2, y2 = xy[i + 1]
x_length = x2 - x1
y_length = y2 - y1
length = math.sqrt(x_length ** 2 + y_length ** 2)
dash_enabled = True
postion = 0
while postion <= length:
for dash_step in dash:
if postion > length:
break
if dash_enabled:
start = postion / length
end = min((postion + dash_step - 1) / length, 1)
self.thick_line([(round(x1 + start * x_length),
round(y1 + start * y_length)),
(round(x1 + end * x_length),
round(y1 + end * y_length))],
xy, fill, width)
dash_enabled = not dash_enabled
postion += dash_step
return



image = Image.new('RGB', (800, 260), (36, 36, 36))
d = DashedImageDraw(image)
l_raw = [190, 117, 80, 184, 196, 134, 102, 82, 122, 157, 80, 124, 151, 186]
largest_n = max(l_raw)
length_beam = []
for number in l_raw:
length_beam.append(number/largest_n*170)
dashed_line_x = 0
repeat = 0
for length in length_beam:

if not dashed_line_x == 0:
if not dashed_line_x >=750:
d.ellipse((dashed_line_x-3,220-length-3,dashed_line_x+3,220-length+3), fill=(232, 218, 79), outline='white')

d.dashed_line(xy=[(dashed_line_x,220),(dashed_line_x,220-length)], fill=(232, 218, 79))
dashed_line_x+= 61.538

dashed_line_x = 0

for elm in range(0,len(length_beam)-1):
y1 = length_beam[elm]
y2 = length_beam[elm +1]
d.line((dashed_line_x, 220-y1, dashed_line_x + 61.538, 220-y2), fill=(232, 218, 79), width=2)
dashed_line_x += 61.538

image.save("image.png", "PNG")

Re: Pillow Linien sind unterschiedlich dick

Verfasst: Sonntag 27. Juni 2021, 12:49
von __deets__
Bitte Code in code-Tags stecken. Das ist der </>-Knopf im vollstaendigen Editor.

Und was dein Problem angeht: frueher zumindest war es besser, AggDraw zu benutzen, weil PIL (der Vorgaenger von Pillow) nicht so geil war beim zeichnen.

Re: Pillow Linien sind unterschiedlich dick

Verfasst: Sonntag 27. Juni 2021, 13:29
von __blackjack__
Wenn das immer noch um Diagramme geht, würde ich auch eher bei matplotlib & Co bleiben statt jetzt wirklich *alles* von 0 an selbst zu implementieren.

Re: Pillow Linien sind unterschiedlich dick

Verfasst: Sonntag 27. Juni 2021, 13:58
von __deets__
@__blackjack__ ich finde gerade matplotlib schwer zu stylen. Sehr sogar. Darum würde ich da durchaus so vorgehen, wenn ich so viel Kontrolle über das Ergebnis brauche.

Re: Pillow Linien sind unterschiedlich dick

Verfasst: Sonntag 27. Juni 2021, 16:53
von __blackjack__
@__deets__: Aber selbst wenn man mit matplotlib alles von 0 selbst macht, ist die Qualität deutlich besser, und man kann neben PNG auch PDF oder SVG produzieren.