Skip to content

Instantly share code, notes, and snippets.

@gunthercox
Last active August 29, 2015 13:57
Show Gist options
  • Save gunthercox/9874531 to your computer and use it in GitHub Desktop.
Save gunthercox/9874531 to your computer and use it in GitHub Desktop.
Generates a graph based on the file size of image formats for different dimensions of images.
from PIL import Image
import turtle
import os
def make_image(imagename, imagetype, imagesize):
"""
Creates a new square RGB image.
"""
size = (imagesize, imagesize)
img = Image.new("RGB", size, "white")
img.save(imagename, imagetype)
print("Created", imagename)
def make_log(imageType):
"""
Creates a csv log file with
the image dimensions followed by the
image file size.
"""
image_directory = "tests/"
image_max_size = 2001
image_size_interval = 200
# TODO: Create test directory if it does not already exist
# Create a log of the file sizes
log = open(image_directory + imageType + "_log.txt", "w")
for size in range(0, image_max_size, image_size_interval):
if size == 0:
size = 1
name = image_directory + "test_" + str(size) + "." + imageType
make_image(name, imageType, size)
# Save the file size of each image in a file
filesize = str(os.path.getsize(name))
log.write(str(size) + "," + filesize + ",\n")
log.close()
# Make a graph with a different color line for each image type.
# Image dimensions will be the X axis and file size will be the y axis.
def graph(t, color, datax, datay):
"""
Graphs a graph of data
Takes a turtle, a color, and a list of data
"""
t.down()
for i in range(0, len(datax)):
# values to scale graph
x = datax[i]/10
y = datay[i]/200
# Plot the points
t.color(color)
t.goto(x,y)
t.down()
t.dot()
# reset the position
t.up()
def graphImages():
colors = ["red", "black", "green", "blue"]
types = ["jpeg", "bmp", "png", "gif"]
t = turtle.Turtle()
t.hideturtle()
t.speed(0)
turtle.screensize(canvwidth=1500, canvheight=1500, bg=None)
for i in range(0, len(types)):
make_log(types[i])
csvfile = open("tests/"+types[i]+"_log.txt", "r")
data = csvfile.readlines()
# Make a list of the file content
dataSplit = [line.split(',') for line in data[1:]]
dimensions = []
filesize = []
for line in dataSplit:
dimensions.append(int(line[0]))
filesize.append(int(line[1]))
graph(t, colors[i], dimensions, filesize)
t.goto(0, 0)
# Pause so user can view the graph.
input('Press enter to continue.')
# TESTS
graphImages()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment