Created
April 24, 2013 05:01
-
-
Save tk0miya/5449742 to your computer and use it in GitHub Desktop.
xaxtsuxo.py generates wonderful powerpoint file. it depends PIL and python-pptx.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Image | |
| from urllib import urlopen | |
| from cStringIO import StringIO | |
| from pptx import Presentation | |
| from pptx.util import Inches, Px | |
| from pptx.oxml import _SubElement, qn | |
| def set_textbox_bgcolor(textbox, color): | |
| spPr = textbox._element.spPr | |
| noFill = list(spPr.iterchildren())[-1] | |
| spPr.remove(noFill) | |
| _SubElement(spPr, 'a:solidFill') | |
| _SubElement(spPr[qn('a:solidFill')], 'a:srgbClr') | |
| color = "%02X%02X%02X" % color | |
| spPr[qn('a:solidFill')][qn('a:srgbClr')].set('val', color) | |
| def put_pixel(slide, x, y, color): | |
| N = 4 | |
| textbox = slide.shapes.add_textbox(Inches(0.5) + Px(x * N), Inches(0.5) + Px(y * N), Px(N), Px(N)) | |
| textbox.text = "" | |
| set_textbox_bgcolor(textbox, color) | |
| def load_image(url): | |
| return Image.open(StringIO(urlopen(url).read())) | |
| def image_to_pixels(image): | |
| _, height = image.size | |
| for i, color in enumerate(image.getdata()): | |
| x = i % height | |
| y = i / height | |
| yield (x, y), color | |
| def main(): | |
| url = 'https://si0.twimg.com/profile_images/2259615760/__________2012-05-29_14.45.07_bigger.png' | |
| image = load_image(url) | |
| prs = Presentation() | |
| default_layout = prs.slidelayouts[1] | |
| slide = prs.slides.add_slide(default_layout) | |
| for (x, y), color in image_to_pixels(image): | |
| put_pixel(slide, x, y, color) | |
| prs.save('xaxtsuxo.pptx') | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment