r/CodingHelp • • Jul 01 '21

[Request Coders] Question from a noob at coding: How difficult would it be to use image processing of pictures, to create a replica image using dice. Like in this artwork by Steven Paul Judd.

Post image
56 Upvotes

7 comments sorted by

9

u/[deleted] Jul 01 '21 edited Jul 01 '21

There's a website called Rasterbator that's been around for almost 20 years where you can upload any picture and it will turn it into a giant halftoned poster that is split up across standard-sized sheets of paper you can print out at home and tape together. All you have to do is take the same idea and size it down to dice. The source and binaries are available here.

1

u/jezzikah01 Jul 01 '21

Oooh, i downloaded a version of this when I was in uni and kept the installer for when I would need it. Never did use it and it likely won't work anymore.
Didn't know they put up a website. So happy to have found this comment!

2

u/Nightcorex_ Jul 01 '21

Not very hard. You'd need to take a photo of the piece, cut it with f.e. GIMP, so that the picture only contains the painting and then render it to the resolution you want the final piece to be in (f.e. if you want to have a 200:500p endproduct, then you need to make sure all pictures you hand the program are in this resolution.).

Now you call your program that does this, with the image file as a parameter.

Then there will be functions to parse such an image pixel by pixel where, depending on the color, you can then replace them with a fitting dice

3

u/[deleted] Jul 01 '21

You don’t even have to use a second program, just convert the image in Gimp to a colour index with six shades of grey. You might have to grayscale and tune the curves before you do the indexing before you apply the index for it to look good though. Very basic image manipulation, however.

1

u/devnull10 Jul 01 '21

Very easy however I suspect (though couldn't say for definite on my phone) that the artist uses two dice types.

  • white with black dots

  • black white dots

Thus you get 12 "colours" rather than 6. Then it's just a case of doing what the others have suggested already - scale down to your desired resolution, convert each pixel to a "dice" based on it's colour, and render those.

1

u/spaghettu Professional Coder Jul 01 '21

I think making a program that does this would be a good beginner-intermediate level project, maybe after a good set of tutorials

1

u/devnull10 Jul 01 '21

I posted the code for it below.

1

u/devnull10 Jul 03 '21

Sure, it's easy enough. Here is a pic of Boris! :) Click Me

As for the code, copy this into a file and run it. Using the default parameters it does a 100x100 sizing, but it can be interesting to use say 500x500, though note that the pictures will be bigger. You can also change the dice size if you wanted a huge picture (maybe you want to print it to A0 etc).

import argparse
from PIL import Image, ImageDraw

output_file="output.png"

def get_dice(val,size):
	dice={1:"----x----",2:"x-------x",3:"x---x---x",4:"x-x---x-x",5:"x-x-x-x-x",6:"x-xx-xx-x"}

	val_normalized=max(round(12*(val/255)),1)	
	dot_size=size/8
	col="black" if val_normalized<=6 else "white"
	dot_col="white" if col=="black" else "white"
	val_normalized=val_normalized-6 if val_normalized>6 else val_normalized

	img=Image.new("RGB",(size,size),color=col)
	dots=ImageDraw.Draw(img)
	dots.rectangle((0,0,size,size),outline=dot_col)
	x=y=0

	for r in range(3):
		for c in range(3):
			if dice[val_normalized][r*3+c]=="x":
				# x
				if c==0:
					x=size/5
				elif c==1:
					x=size/2
				elif c==2:
					x=size-size/5
				# y
				if r==0:
					y=size/5
				elif r==1:
					y=size/2
				elif r==2:
					y=size-size/5

				dots.ellipse((x-dot_size/2,y-dot_size/2,x+dot_size,y+dot_size),fill=dot_col)

	return img

if __name__=="__main__":
	parser = argparse.ArgumentParser(description = "Convert a picture into Dice")
	parser.add_argument("source", help="Source image file")
	parser.add_argument("-d", default="50", help="Dice size", metavar="DiceSize")
	parser.add_argument("-x", default="100", help="Maximum Width in Dice", metavar="MaxWidth")
	parser.add_argument("-y", default="100", help="Maximum Height in Dice", metavar="MaxHeight")
	args = vars(parser.parse_args())
	source_file=args["source"]
	dice_size=int(args["d"])
	max_width=int(args["x"])
	max_height=int(args["y"])

	img=Image.open(source_file).convert("L")
	img.thumbnail((max_width,max_height),Image.ANTIALIAS)

	img_new=Image.new("RGB",(img.width*dice_size,img.height*dice_size))

	for y in range(img.height):
		for x in range(img.width):
			img_new.paste(get_dice(img.getpixel((x,y)),dice_size),(x*dice_size, y*dice_size))


	img_new.save("output.png")

Maybe you could change it to make the dice more realistic (rounded edges etc)...