Skip to content
Snippets Groups Projects
Commit 79b2e948 authored by Tamazouzt AIT ELDJOUDI's avatar Tamazouzt AIT ELDJOUDI
Browse files

Adding the class My_image and fixing the size of the eggs

parent 426d9c34
Branches
No related tags found
No related merge requests found
File added
File added
File added
File added
......@@ -2,7 +2,7 @@ from ast import Raise
from logging import exception
from PIL import Image
import logic
import parser
import parsing
colors = ['black','blue','green','orange','pink','purple','red','yellow']
......@@ -15,17 +15,30 @@ def associateVariableWithColor(variable):
if str(variable) not in variables_colors_couple:
variables_colors_couple[str(variable)] = colors[-1]
colors.pop()
#------------------------------------------------------------------------------------------------------------------------------------------
class My_image:
def __init__(self,image,b):
self.image=image
self.is_egg=b
def resize(self,width,height):
if not(self.is_egg):
self.image.resize(width, height)
else:
self.image.resize(min(self.image.width,width), min(self.image.height,height))
#---------------------------------------------------------------------------------------------------------------------------------
def createVarImage(variable):
assert (logic.isVariable(variable))
associateVariableWithColor(variable)
egg_img = Image.open("figures/egg_"+variables_colors_couple[str(variable)]+".png")
image=My_image(egg_img,True)
return egg_img
def createAlligator(terme):
assert (logic.isVariable(terme))
associateVariableWithColor(terme)
alligator_img = Image.open("figures/alligator_"+variables_colors_couple[str(terme)]+".png")
image=My_image(alligator_img,False)
return alligator_img
def createAbsImage(terme):
......@@ -34,21 +47,26 @@ def createAbsImage(terme):
output = logic.getOutputFromAbs(terme)
im1 =createAlligator(input)
im2 = createImage(output)
abstraction = Image.new('RGB', (max(im1.width, im2.width), im1.height + im2.height), (255, 255, 255))
abstraction.paste(im1, ((max(im1.width, im2.width)-im1.width) // 2, 0))
abstraction.paste(im2, ( 0 , im1.height))
abstraction = Image.new('RGB', (max(im1.width, im2.image.width), im1.height + im2.image.height), (255, 255, 255))
abstraction.paste(im1, ((max(im1.width, im2.image.width)-im1.width) // 2, 0))
abstraction.paste(im2.image, ( 0 , im1.height))
return abstraction
def get_concat_h_multi_resize(im_list, resample=Image.BICUBIC):
def get_concat_h_multi_resize(im_list, resample=Image.Resampling.BICUBIC):
max_height=max(im.height for im in im_list)
min_height = min(im.height for im in im_list)
im_list_resize = [im.resize((int(im.width * min_height / im.height), min_height), resample=resample)
for im in im_list]
total_width = sum(im.width for im in im_list_resize)
dst = Image.new('RGB', (total_width, min_height))
min_width=min(im.width for im in im_list)
space_width=int(min_width*0.25)
total_width = sum(im.width for im in im_list_resize) + (len(im_list_resize) -1) * space_width
dst = Image.new('RGB', (total_width, min_height),(255,255,255))
pos_x = 0
for im in im_list_resize:
dst.paste(im, (pos_x, 0))
pos_x += im.width
pos_x += im.width + space_width
return dst
def createAppImage(terme):
......@@ -57,17 +75,16 @@ def createAppImage(terme):
right = logic.getSecondTerm(terme)
im1 = createImage(left)
im2 = createImage(right)
space = Image.open("figures/space.png")
application = get_concat_h_multi_resize([im1,space,im2])
application = get_concat_h_multi_resize([im1.image,im2.image])
return application
def createImage(terme):
if logic.isVariable(terme):
return createVarImage(terme)
return My_image(createVarImage(terme),True)
if logic.isAbstraction(terme):
return createAbsImage(terme)
return My_image(createAbsImage(terme),False)
if logic.isApplication(terme):
return createAppImage(terme)
return My_image(createAppImage(terme),False)
else:
raise Exception("Unsupported term type")
......
......@@ -195,7 +195,7 @@ import image_maker
image_counter = 0
def captureImage(terme):
global image_counter
image_maker.saveImage(image_maker.createImage(terme),str(image_counter))
image_maker.saveImage(image_maker.createImage(terme).image,str(image_counter))
image_counter += 1
def beta_reduction_totale(terme):
......
import logic
import parser
import parsing
import image_maker
def run():
# x = image_maker.createImage(parser.parseTerm(input("Enter a term: ")))
# x = image_maker.createImage(parsing.parseTerm(input("Enter a term: ")))
# image_maker.saveImage(x, "terme")
# x.show()
logic.beta_reduction_totale(parser.parseTerm(input("Enter a term: ")))
logic.beta_reduction_totale(parsing.parseTerm(input("Enter a term: ")))
run()
\ No newline at end of file
File moved
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment