
# https://www.globalsino.com/ICs/
# Align the top-left corner of the image to the center of the slide when there is an image rotation

from pptx import Presentation
from pptx.util import Inches
from PIL import Image

MyPresentation = Presentation()

Slide_Register = MyPresentation.slide_layouts[5]
Slide = MyPresentation.slides.add_slide(Slide_Register)
title  = Slide.shapes.title
title.text = "Experimental Images"

# Convert pixels to inches
def px_to_inches(path):
    im = Image.open(path)
    width = im.width/im.info['dpi'][0] # dpi: image resoution
    height = im.height/im.info['dpi'][1]
    print("height", height)
    return (width, height)

theImage = r"C:\0Python\images\TheJob\JobB\ImagePool (3).jpg"

img = px_to_inches(theImage)
TheWidth = Inches(3) # The new width of th image
print("TheWidth", TheWidth)
print("img[1]", img[1])
print("Inches(img[1])", Inches(img[1]))

print("TheWidth-img[1]", TheWidth-img[1])

print("The width and height of the original image in inches are: ", img[0], img[1])
theMagOfImage = TheWidth/Inches(img[1])
print ("theMagOfImage is ", theMagOfImage)
# left = Inches(3) # This can be used to compare when the rotation is removed
#top = Inches(3)

left = Inches(3) - Inches(img[0]/2) - ((theMagOfImage-1)*Inches(img[0]/2)) + TheWidth/2
print("Inches(img[0]/2)", Inches(img[0]/2))
print("TheWidth/2", TheWidth/2)

top = Inches(3) + Inches(img[1]) - TheWidth/2 - ((theMagOfImage-1)*Inches(img[1]/2))
print("left and top are ", left, top)

pic = Slide.shapes.add_picture(theImage, left, top, width =None, height =TheWidth)
pic.rotation = -90

width, height = pic.image.size  # ---width and height are int pixel-counts
print("width, height are ", width, height)

MyPresentation.save(r"C:\0Python\images\PPTtest_World_JobDescription.pptx")






