{"action":"created","issue":{"url":"https://api.github.com/repos/scanny/python-pptx/issues/116","repository_url":"https://api.github.com/repos/scanny/python-pptx","labels_url":"https://api.github.com/repos/scanny/python-pptx/issues/116/labels{/name}","comments_url":"https://api.github.com/repos/scanny/python-pptx/issues/116/comments","events_url":"https://api.github.com/repos/scanny/python-pptx/issues/116/events","html_url":"https://github.com/scanny/python-pptx/issues/116","id":41607226,"node_id":"MDU6SXNzdWU0MTYwNzIyNg==","number":116,"title":"feature: Picture.replace_image()","user":{"login":"kodonnell","id":8204904,"node_id":"MDQ6VXNlcjgyMDQ5MDQ=","avatar_url":"https://avatars2.githubusercontent.com/u/8204904?v=4","gravatar_id":"","url":"https://api.github.com/users/kodonnell","html_url":"https://github.com/kodonnell","followers_url":"https://api.github.com/users/kodonnell/followers","following_url":"https://api.github.com/users/kodonnell/following{/other_user}","gists_url":"https://api.github.com/users/kodonnell/gists{/gist_id}","starred_url":"https://api.github.com/users/kodonnell/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kodonnell/subscriptions","organizations_url":"https://api.github.com/users/kodonnell/orgs","repos_url":"https://api.github.com/users/kodonnell/repos","events_url":"https://api.github.com/users/kodonnell/events{/privacy}","received_events_url":"https://api.github.com/users/kodonnell/received_events","type":"User","site_admin":false},"labels":[{"id":19922764,"node_id":"MDU6TGFiZWwxOTkyMjc2NA==","url":"https://api.github.com/repos/scanny/python-pptx/labels/image","name":"image","color":"fef2c0","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":{"url":"https://api.github.com/repos/scanny/python-pptx/milestones/5","html_url":"https://github.com/scanny/python-pptx/milestone/5","labels_url":"https://api.github.com/repos/scanny/python-pptx/milestones/5/labels","id":614255,"node_id":"MDk6TWlsZXN0b25lNjE0MjU1","number":5,"title":"later","description":"","creator":{"login":"scanny","id":2062718,"node_id":"MDQ6VXNlcjIwNjI3MTg=","avatar_url":"https://avatars3.githubusercontent.com/u/2062718?v=4","gravatar_id":"","url":"https://api.github.com/users/scanny","html_url":"https://github.com/scanny","followers_url":"https://api.github.com/users/scanny/followers","following_url":"https://api.github.com/users/scanny/following{/other_user}","gists_url":"https://api.github.com/users/scanny/gists{/gist_id}","starred_url":"https://api.github.com/users/scanny/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/scanny/subscriptions","organizations_url":"https://api.github.com/users/scanny/orgs","repos_url":"https://api.github.com/users/scanny/repos","events_url":"https://api.github.com/users/scanny/events{/privacy}","received_events_url":"https://api.github.com/users/scanny/received_events","type":"User","site_admin":false},"open_issues":18,"closed_issues":7,"state":"open","created_at":"2014-03-30T04:51:58Z","updated_at":"2018-07-25T15:49:06Z","due_on":null,"closed_at":null},"comments":4,"created_at":"2014-09-01T05:13:48Z","updated_at":"2018-09-04T11:38:13Z","closed_at":null,"author_association":"NONE","body":"As discussed on the mailing group (https://groups.google.com/forum/?hl=en#!topic/python-pptx/YJBzJznTTHw), there are some potential uses for replacing an image with a new one. [Think \"refreshing\" a PPT with updated images.] \n\nSome working code (largely suggested by Steve) is as follows:\n\n``` python\nfrom pptx import Presentation\nfrom pptx.util import Px\nfrom PIL import Image\n\n# inputs\nPPT_TEMPLATE = 'test.pptx'\nREPLACEMENT_IMG = 'pic.png'\n\n# load preso and shape\npresso = Presentation(PPT_TEMPLATE)\nslide = presso.slides[0]\nimg = slide.shapes[0]\n\n# get current image info\nimgPic = img._pic\nimgRID = imgPic.xpath('./p:blipFill/a:blip/@r:embed', namespaces=imgPic.nsmap)[0]\nimgPart = slide.related_parts[imgRID]\n\n# get info about replacement image\nwith open(REPLACEMENT_IMG, 'rb') as f:\n    rImgBlob = f.read()\nrImgWidth, rImgHeight = Image.open(REPLACEMENT_IMG).size\nrImgWidth, rImgHeight = Px(rImgWidth), Px(rImgHeight) # change from Px\n\n# replace\nimgPart._blob = rImgBlob\n\n# now alter the size and position to suit that of the replacement img\n# rescale sizes so image isn't stretched\nwidthScale = float(rImgWidth) / img.width\nheightScale = float(rImgHeight) / img.height\nmaxScale = max(widthScale, heightScale)\nscaledImgWidth, scaledImgHeight = int(rImgWidth / maxScale), int(rImgHeight / maxScale)\n# center the image if it's different size to the original\nscaledImgLeft = int(img.left + (img.width - scaledImgWidth)/2)\nscaledImgTop = int(img.top + (img.height - scaledImgHeight)/2)\n# now update\nimg.left, img.top, img.width, img.height = scaledImgLeft, scaledImgTop, scaledImgWidth, scaledImgHeight\n\n```\n"},"comment":{"url":"https://api.github.com/repos/scanny/python-pptx/issues/comments/418335159","html_url":"https://github.com/scanny/python-pptx/issues/116#issuecomment-418335159","issue_url":"https://api.github.com/repos/scanny/python-pptx/issues/116","id":418335159,"node_id":"MDEyOklzc3VlQ29tbWVudDQxODMzNTE1OQ==","user":{"login":"timini","id":247302,"node_id":"MDQ6VXNlcjI0NzMwMg==","avatar_url":"https://avatars2.githubusercontent.com/u/247302?v=4","gravatar_id":"","url":"https://api.github.com/users/timini","html_url":"https://github.com/timini","followers_url":"https://api.github.com/users/timini/followers","following_url":"https://api.github.com/users/timini/following{/other_user}","gists_url":"https://api.github.com/users/timini/gists{/gist_id}","starred_url":"https://api.github.com/users/timini/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/timini/subscriptions","organizations_url":"https://api.github.com/users/timini/orgs","repos_url":"https://api.github.com/users/timini/repos","events_url":"https://api.github.com/users/timini/events{/privacy}","received_events_url":"https://api.github.com/users/timini/received_events","type":"User","site_admin":false},"created_at":"2018-09-04T11:38:13Z","updated_at":"2018-09-04T11:38:13Z","author_association":"NONE","body":"+1 would like this feature"}}
{"actor":{"display_login":"timini"}}