Added a threshold and all mode. Reworked the application into a class and reused relevant code for each dithering method.
This commit is contained in:
parent
c842c03993
commit
ce837f1d7e
|
@ -0,0 +1 @@
|
|||
oneliner
|
17
README.md
17
README.md
|
@ -1,7 +1,7 @@
|
|||
# Lid
|
||||
_lo-fi image dithering_
|
||||
|
||||
Lid performs fairly basic 1 bit image dithering. It converts an input image to grayscale and then dithers that image. It then optimizes the output and saves the file. The result is a reduced filesize, simple image, great for small apge sizes.
|
||||
Lid performs fairly basic 1 bit image dithering. It converts an input image to grayscale and then dithers that image. It then optimizes the output and saves the file. The result is a reduced filesize, simple image, great for small page sizes.
|
||||
|
||||
## Dependencies
|
||||
|
||||
|
@ -14,8 +14,9 @@ lid [option]... [source path] [output name]
|
|||
|
||||
Options:<br>
|
||||
-f = output format, defaults to the recommended setting: png<br>
|
||||
-m = mode, available: o4, o9, e. default: o4.
|
||||
-q = quality, defaults to 90, only affects jpeg/jpg
|
||||
-m = mode, available: o4, o9, e. default: o4.<br>
|
||||
-q = quality (0 - 100), defaults to 90, only affects jpeg/jpg<br>
|
||||
-t = threshold (0 - 255), defaults to image average level. only affects dither mode 't'
|
||||
|
||||
source path = a path to an image file you want to dither<br>
|
||||
output name = the name of the file you would like to output, do not include file extension
|
||||
|
@ -24,14 +25,20 @@ Lid works best outputting png files from any input. You will get the smallest fi
|
|||
|
||||
### Modes
|
||||
|
||||
#### t
|
||||
This is the lowest quality mode. It produces the smallest file, but at the cost of most detail.
|
||||
|
||||
#### o4
|
||||
This is the lowest quality mode. It will produce the smallest file size. It is a four level ordered dither.
|
||||
This mode is a noticable difference from the stark two tone appearance of threashold mode. The appearance 'gray' areas make for a slightly more photographic image.
|
||||
|
||||
#### o9
|
||||
This mode produces medium quality dithering, it is still a very noticeable effect but not as harsh as o4. It is a none level ordered dither.
|
||||
This mode produces medium quality dithering, it is still a very noticeable effect but not as harsh as o4. It is a nine level ordered dither.
|
||||
|
||||
#### e
|
||||
This is the highest quality mode. It uses an error diffusion dither. The file size will be the largest of the bunch, but should still be a good reduction from a regular full color photograph.
|
||||
|
||||
#### a
|
||||
The a, or all, mode is a quick flag to say that you want an image put out in all of the modes. The mode flag value for each will be appended to the filename.
|
||||
|
||||
## License
|
||||
Public domain. Use it, improve it, share it!
|
||||
|
|
215
lid
215
lid
|
@ -8,60 +8,53 @@ from PIL import Image
|
|||
import sys
|
||||
import os, os.path
|
||||
|
||||
# Default dithering: 4 level ordered dither
|
||||
def ordered_dither_4(r):
|
||||
im = Image.open(r["path"]).convert("L")
|
||||
width, height = im.size
|
||||
new = create_image(width, height)
|
||||
newpixels = new.load()
|
||||
class Dither:
|
||||
def __init__(self, data):
|
||||
self.source_image = Image.open(data["path"]).convert("L")
|
||||
self.width, self.height = self.source_image.size
|
||||
self.pixel_count = self.width * self.height
|
||||
self.new = self.create_image()
|
||||
self.new_pixels = self.new.load()
|
||||
self.output_format = data["-f"]
|
||||
self.output_name = data["out"]
|
||||
self.output_quality = data["-q"]
|
||||
self.threshold = data["-t"]
|
||||
|
||||
dots = [
|
||||
[64, 128],
|
||||
[192, 0]
|
||||
]
|
||||
#used for printing progress
|
||||
self.counter = 1
|
||||
self.percent_val = 0
|
||||
self.hash_val = 0
|
||||
|
||||
total_px_count = width * height
|
||||
counter = 1
|
||||
last_percent_val = 0
|
||||
last_hash_val = 0
|
||||
|
||||
def save_file(self):
|
||||
output_filename = "{}.{}".format(self.output_name, self.output_format.lower())
|
||||
self.new.save(output_filename, self.output_format.upper(), optimize=True, quality=self.output_quality)
|
||||
print("\033[20C\nFinished! ~ {}/{}\033[?25h".format(os.getcwd(), output_filename))
|
||||
|
||||
|
||||
# Default dithering
|
||||
def ordered_dither_4(self):
|
||||
dots = [[64, 128],[192, 0]]
|
||||
print("Dither method: ordered (4 levels)\033[?25l")
|
||||
for row in range(0, height):
|
||||
for col in range(0, width):
|
||||
for row in range(0, self.height):
|
||||
for col in range(0, self.width):
|
||||
dotrow = 1 if row % 2 else 0
|
||||
dotcol = 1 if col % 2 else 0
|
||||
impx = get_pixel(im, col, row)
|
||||
newpixels[col, row] = int(impx > dots[dotrow][dotcol])
|
||||
counter += 1
|
||||
new_percent_val = round(counter / total_px_count * 100)
|
||||
new_hash_val = round(counter / total_px_count * 10)
|
||||
if new_percent_val != last_percent_val or new_hash_val != last_hash_val:
|
||||
print("\r{:>3}% |{:<10}|".format(new_percent_val, "#" * new_hash_val), end="")
|
||||
last_percent_val = new_percent_val
|
||||
last_hash_val = new_hash_val
|
||||
px = self.get_pixel(col, row)
|
||||
self.new_pixels[col, row] = int(px > dots[dotrow][dotcol])
|
||||
self.update_progress()
|
||||
self.save_file()
|
||||
|
||||
new.save("{}.{}".format(r["out"], r["-f"]), r["-f"].upper(), optimize=True, quality=r["-q"])
|
||||
print("\033[20C\nFinished! ~ {}/{}.{}\033[?25h".format(os.getcwd(), r["out"], r["-f"].lower()))
|
||||
|
||||
# 9 level ordered dither
|
||||
def ordered_dither_9(r):
|
||||
im = Image.open(r["path"]).convert("L")
|
||||
width, height = im.size
|
||||
new = create_image(width, height)
|
||||
newpixels = new.load()
|
||||
|
||||
def ordered_dither_9(self):
|
||||
dots = [
|
||||
[0, 196, 84],
|
||||
[168, 140, 56],
|
||||
[112, 28, 224]
|
||||
]
|
||||
|
||||
total_px_count = width * height
|
||||
counter = 1
|
||||
last_percent_val = 0
|
||||
last_hash_val = 0
|
||||
print("Dither method: ordered (9 levels)\033[?25l")
|
||||
for row in range(0, height):
|
||||
for col in range(0, width):
|
||||
for row in range(0, self.height):
|
||||
for col in range(0, self.width):
|
||||
if not row % 3:
|
||||
dotrow = 2
|
||||
elif not row % 2:
|
||||
|
@ -75,50 +68,41 @@ def ordered_dither_9(r):
|
|||
dotcol = 1
|
||||
else:
|
||||
dotcol = 0
|
||||
impx = get_pixel(im, col, row)
|
||||
newpixels[col, row] = int(impx > dots[dotrow][dotcol])
|
||||
counter += 1
|
||||
new_percent_val = round(counter / total_px_count * 100)
|
||||
new_hash_val = round(counter / total_px_count * 10)
|
||||
if new_percent_val != last_percent_val or new_hash_val != last_hash_val:
|
||||
print("\r{:>3}% |{:<10}|".format(new_percent_val, "#" * new_hash_val), end="")
|
||||
last_percent_val = new_percent_val
|
||||
last_hash_val = new_hash_val
|
||||
px = self.get_pixel(col, row)
|
||||
self.new_pixels[col, row] = int(px > dots[dotrow][dotcol])
|
||||
self.update_progress()
|
||||
self.save_file()
|
||||
|
||||
new.save("{}.{}".format(r["out"], r["-f"]), r["-f"].upper(), optimize=True, quality=r["-q"])
|
||||
print("\033[20C\nFinished! ~ {}/{}.{}\033[?25h".format(os.getcwd(), r["out"], r["-f"].lower()))
|
||||
|
||||
# Alternate dither mode
|
||||
def error_dither(r):
|
||||
im = Image.open(r["path"]).convert("L")
|
||||
width, height = im.size
|
||||
new = create_image(width, height)
|
||||
newpixels = new.load()
|
||||
i = im.load()
|
||||
def threshold_dither(self):
|
||||
if not self.threshold:
|
||||
px_list = list(self.source_image.getdata())
|
||||
self.threshold = sum(px_list) // len(px_list)
|
||||
print("Dither method: threshold (set to: {})\033[?25l".format(self.threshold))
|
||||
for row in range(0, self.height):
|
||||
for col in range(0, self.width):
|
||||
px = self.get_pixel(col, row)
|
||||
self.new_pixels[col, row] = int(px > self.threshold)
|
||||
self.update_progress()
|
||||
self.save_file()
|
||||
|
||||
total_px_count = width * height
|
||||
counter = 1
|
||||
last_percent_val = 0
|
||||
last_hash_val = 0
|
||||
|
||||
def random_dither(self):
|
||||
pass
|
||||
|
||||
def error_diffusion_dither(self):
|
||||
i = self.source_image.load()
|
||||
print("Dither method: error diffusion\033[?25l")
|
||||
for row in range(0, height):
|
||||
for col in range(0, width):
|
||||
newpixels[col, row] = update_error(im, i, row, col)
|
||||
counter += 1
|
||||
new_percent_val = round(counter / total_px_count * 100)
|
||||
new_hash_val = round(counter / total_px_count * 10)
|
||||
if new_percent_val != last_percent_val or new_hash_val != last_hash_val:
|
||||
print("\r{:>3}% |{:<10}|".format(new_percent_val, "#" * new_hash_val), end="")
|
||||
last_percent_val = new_percent_val
|
||||
last_hash_val = new_hash_val
|
||||
|
||||
new.save("{}.{}".format(r["out"], r["-f"]), r["-f"].upper(), optimize=True, quality=r["-q"])
|
||||
print("\033[20C\nFinished! ~ {}/{}.{}\033[?25h".format(os.getcwd(), r["out"], r["-f"].lower()))
|
||||
for row in range(0, self.height):
|
||||
for col in range(0, self.width):
|
||||
self.new_pixels[col, row] = self.update_error(i, row, col)
|
||||
self.update_progress()
|
||||
self.save_file()
|
||||
|
||||
|
||||
def update_error(im, i, r, c):
|
||||
w, h = im.size
|
||||
current = get_pixel(im, c, r)
|
||||
# Helper for error_diffusion_dither
|
||||
def update_error(self, i, r, c):
|
||||
current = self.get_pixel(c, r)
|
||||
if current > 128:
|
||||
res = 1
|
||||
diff = -(255 - current)
|
||||
|
@ -127,9 +111,9 @@ def update_error(im, i, r, c):
|
|||
diff = abs(0 - current)
|
||||
mov = [[0, 1, 0.4375],[1, 1, 0.0625],[1, 0, 0.3125],[1, -1, 0.1875]]
|
||||
for x in mov:
|
||||
if r + x[0] >= h or c + x[1] >= w or c + x[1] <= 0:
|
||||
if r + x[0] >= self.height or c + x[1] >= self.width or c + x[1] <= 0:
|
||||
continue
|
||||
p = get_pixel(im, c + x[1], r + x[0])
|
||||
p = self.get_pixel(c + x[1], r + x[0])
|
||||
p = round(diff * x[2] + p)
|
||||
if p < 0:
|
||||
p = 0
|
||||
|
@ -139,27 +123,35 @@ def update_error(im, i, r, c):
|
|||
return res
|
||||
|
||||
|
||||
|
||||
def print_spinner(n):
|
||||
print("\r|{:<10}|".format("#" * n), end="")
|
||||
|
||||
# Create a new image with the given size
|
||||
def create_image(i, j):
|
||||
image = Image.new("1", (i, j))
|
||||
# Create a new image with the given size
|
||||
def create_image(self):
|
||||
image = Image.new("1", (self.width, self.height))
|
||||
return image
|
||||
|
||||
|
||||
# Get the pixel from the given image
|
||||
def get_pixel(image, i, j):
|
||||
width, height = image.size
|
||||
if i > width or j > height:
|
||||
# Get the pixel from the given image
|
||||
def get_pixel(self, col, row):
|
||||
if col > self.width or row > self.height:
|
||||
return None
|
||||
|
||||
# Get Pixel
|
||||
pixel = image.getpixel((i, j))
|
||||
pixel = self.source_image.getpixel((col, row))
|
||||
return pixel
|
||||
|
||||
|
||||
def update_progress(self):
|
||||
self.counter += 1
|
||||
new_percent_val = round(self.counter / self.pixel_count * 100)
|
||||
new_hash_val = round(self.counter / self.pixel_count * 10)
|
||||
if new_percent_val != self.percent_val or new_hash_val != self.hash_val:
|
||||
print("\r{:>3}% |{:<10}|".format(new_percent_val, "#" * new_hash_val), end="")
|
||||
self.percent_val = new_percent_val
|
||||
self.hash_val = new_hash_val
|
||||
|
||||
|
||||
#---- END Dither class ----#
|
||||
|
||||
|
||||
def display_help():
|
||||
helptext = """
|
||||
lid - lo-fi image dithering
|
||||
|
@ -171,6 +163,7 @@ def display_help():
|
|||
-f output format. defaults to: png (recommended)
|
||||
-m dither mode. defaults to: o4. other options: e, o9
|
||||
-q image quality. defaults to: 90. only has effect on png/jpg
|
||||
-t threshold. default: image average tone. onls has effect in mode 't'
|
||||
|
||||
arguments:
|
||||
infile path to a valid image file on your system
|
||||
|
@ -198,6 +191,7 @@ def parse_input():
|
|||
"-f": "png", # output format
|
||||
"-q": 25, # optimization quality
|
||||
"-m": "o4", # mode, defaults to ordered
|
||||
"-t": None, # threshold
|
||||
"path": None, # path to file
|
||||
"out": "lid_file" # output filename
|
||||
}
|
||||
|
@ -232,10 +226,20 @@ def parse_input():
|
|||
elif argmap["-f"] == "jpg":
|
||||
argmap["-f"] = "jpeg"
|
||||
|
||||
if not argmap["-m"] in ["o4", "e", "o9"]:
|
||||
if not argmap["-m"] in ["o4", "e", "o9", "t", "a"]:
|
||||
print("lid error: invalid dither mode provided for -m. Options: o4, o9, e. Default: o4")
|
||||
return None
|
||||
|
||||
try:
|
||||
if argmap["-t"]:
|
||||
argmap["-t"] = int(argmap["-t"])
|
||||
if argmap["-t"] < 0 or argmap["-t"] > 255:
|
||||
print("lid error: threshold value must be between 0 and 255")
|
||||
return None
|
||||
except ValueError:
|
||||
print("lid error: invalid threshold value provided for -m. Must be integer from 0 to 255")
|
||||
return None
|
||||
|
||||
try:
|
||||
argmap["-q"] = int(argmap["-q"])
|
||||
if argmap["-q"] < 1 or argmap["-q"] > 100:
|
||||
|
@ -254,12 +258,29 @@ def parse_input():
|
|||
if __name__ == "__main__":
|
||||
request = parse_input()
|
||||
if request:
|
||||
d = Dither(request)
|
||||
if request["-m"] == "o4":
|
||||
ordered_dither_4(request)
|
||||
d.ordered_dither_4()
|
||||
elif request["-m"] == "o9":
|
||||
ordered_dither_9(request)
|
||||
d.ordered_dither_9()
|
||||
elif request["-m"] == "e":
|
||||
error_dither(request)
|
||||
d.error_diffusion_dither()
|
||||
elif request["-m"] == "t":
|
||||
d.threshold_dither()
|
||||
elif request["-m"] == "a":
|
||||
name = request["out"]
|
||||
request["out"] = name + "_o4"
|
||||
d = Dither(request)
|
||||
d.ordered_dither_4()
|
||||
request["out"] = name + "_o9"
|
||||
d2 = Dither(request)
|
||||
d2.ordered_dither_9()
|
||||
request["out"] = name + "_t"
|
||||
d3 = Dither(request)
|
||||
d3.threshold_dither()
|
||||
request["out"] = name + "_e"
|
||||
d4 = Dither(request)
|
||||
d4.error_diffusion_dither()
|
||||
else:
|
||||
print("Unknown mode flag")
|
||||
sys.exit(2)
|
||||
|
|
Loading…
Reference in New Issue