ebook img

Make Art with Python: Programming for Creative People PDF

156 Pages·2019·7.947 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Make Art with Python: Programming for Creative People

Make Art with Python Programming for Creative People Kirk Kaiser Thisbookisforsaleathttp://leanpub.com/makeartwithpython Thisversionwaspublishedon2019-04-20 ISBN978-0-578-46648-4 ThisisaLeanpubbook.LeanpubempowersauthorsandpublisherswiththeLeanPublishing process.LeanPublishingistheactofpublishinganin-progressebookusinglightweighttoolsand manyiterationstogetreaderfeedback,pivotuntilyouhavetherightbookandbuildtractiononce youdo. ©2016-2019KirkKaiser ForLauretta,formakingthishappen. Contents Introduction-LearningtoProgramwithArtandPython . . . . . . . . . . . . . . . . . . . . . i WhyArt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ii WhyPython . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ii TheCreativeProcessofProgramming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iii TheJourneyofBecomingAProgrammer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iii No“RightWay” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iv ChapterOne:ATutorialIntroduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 GettingPython . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 TheTerminal:A ProgramtoControlPrograms. . . . . . . . . . . . . . . . . . . . . . . . . . . 1 WritingOurFirstProgram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 HowtheDevelopmentCycleWorks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 TheEditor:APlaceforWritingPrograms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 ChapterTwo:ComputationalThinking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 TheLoop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 TheVariable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 PythonDataTypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 ControlFlow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 BringingItAllTogether . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 ChapterThree:WritingOurFirstGraphicsProgram . . . . . . . . . . . . . . . . . . . . . . . . 17 DrawingOurFirstPixel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 HowtoReadOurCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 EditingOurCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 TurningPixelsintoaLinewithaLoop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 ChangingOurLine’sDirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 FlippingOurDiagonalLine. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 AFinalChallenge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 ChapterFour:FunctionsAreTheBuildingBlocksofPrograms . . . . . . . . . . . . . . . . . 25 FromPixelstoLines,PuttingtheFuninFunction . . . . . . . . . . . . . . . . . . . . . . . . . 27 TheMechanicsofWritingAFunction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 TestingYourFunctionsasYouGo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 CONTENTS DrawingRandomnesswithOurNewFunction . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 CombiningOurFunctionsforNewEffects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 ChapterFive:ReadingtheUser’sMindWithInput . . . . . . . . . . . . . . . . . . . . . . . . . 34 DoWhattheHumansTellYou. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 GrabbingtheUser’sInput . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 ListsAreLinesofVariables,AllinARow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 CreatingaList,AddingThingstoYourList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 ChapterSix:MorePlayingwithLoops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 DrawingwithOurNewCursor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 DrawingwithOurNewCursor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 LoopingaFade . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 MakingOurFadeintoaWave . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 FadingColorstoMakeRainbows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 MoreExperimenting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 ChapterSeven:InventingIdeaswithClasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 DrawinginNewWays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 MakingYourIdeasPartoftheLanguage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 CreatingOurLineClass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 PlanningYourClassDesign . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 RethinkingHowWeDraw . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 DrawingwiththeMouse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 CleaningUpOurCodewithClass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 ChapterEight:InventingNewWaystoDrawwithShapes . . . . . . . . . . . . . . . . . . . . 66 ExploringPygame’sDrawingMethods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 GivingOurClassNewFeatures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 ColorizingOurLines. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 ChapterNine:PlayingwithFiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 SettingUpOurDirectories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 ReadingOptionsfromtheCommandLine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 UsingIPythontoInspectNewLibraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 SavingOurDrawingswithPickle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 AddingUndotoOurProgram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 UsingTimetoAddDelaytoOurUndo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 ProtectingOurselvesfromErrors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 ChapterTen:PaintingwithImages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 DissectinganImage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 ManipulatingWholeImages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 MakingMirrorImages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 CONTENTS CreatingGeometricImages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 TurningOurImagesintoVideos. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 ChapterEleven:DrawingInfinities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 TheThreeRegularPolygonsthatTesselatethePlane . . . . . . . . . . . . . . . . . . . . . . . 105 DrawingACenteredTriangle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 CheckingtheDistanceofTriangles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 DrawingOurFlippedTrianglesintheRightPlaces . . . . . . . . . . . . . . . . . . . . . . . . 109 MakingOurTesselationsMoreInteresting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 TesselatingHexagons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 ColorizingOurHexagons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 ChapterTwelve:InventingInteractiveTesselations . . . . . . . . . . . . . . . . . . . . . . . . . 121 SurveytheProblemSpace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 DiscovertheRules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 DrawItFirst . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 MakeItInteractive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 MakingaPlanofAttack(ForCode) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 DrawingaSquareFromScratch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 AddingMidpointsToOurSquare’sLines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 SelectingAPoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 FindingtheOppositePointintheSquare . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 BringingItAllTogethertoDraw . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 TesselatingOurNewShape. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 ChapterThirteen:ExportingOurTesselationsforPrint . . . . . . . . . . . . . . . . . . . . . . 135 RenderingVectorGraphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 AdjustingOurTesselation’sThickness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 CreatingGlitchesinOurTessellations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 ColorizingourTesselationswithInkscape . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 ExploringFurther. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 Introduction - Learning to Program with Art and Python If you want to build a ship, don’t drum up the people to gather wood, divide the work, andgiveorders. Instead,teachthemtoyearnforthevastandendlesssea. -AntoineDeSaint-Exupery,TheLittlePrince Faceit,computershavetakenoverhumanity. Everyone’s face is buried in their phone, oblivious to the world. We’ve collectively fallen in love withtheworldsofthemachines. Becauseofthis,theabilitytowritenewsoftwaregivesprogrammersunimaginablepoweroverthe collectivemindsofhumanity.Throughtheirprograms,theymagnifyandmultiplyideas,whichcan quicklyspreadtomillionsofpeopleindays,weeks,months. Buttheprocessoflearningtoprogramcomputersistreacherous.Tothelayperson,itseemsasifonly amathematicallymindedpeoplecaneverhopetolearntoprogram,andtrulycontrolacomputer. And sure, everyone who learns to program must deal with broken code libraries, finding an appropriateprogramminglanguage,decidingwhattomake,andsoon.Therearesomanydecisions tomakealongthejourney,andsomanyfalsestarts,thatmostpeoplegiveupbeforetheyevenreally begin. But programming is especially rewarding for the type of people who are drawn to the humanities. Theybringauniqueperspectiveandexperiencetotheworldofcomputersweinhabit.Theyenrich theworldofcomputationbybringingfunandstoriesandcolortoourvirtualworlds. And honestly, most great programmers are more like painters than technicians, beginning their software with sketches, tiny little programs that just do one thing, that let them see their ideas before beginning. They’ll often make mistakes, write things that don’t run properly the first time. Eventually,throughsheerforceandtrialanderror,thingspushedthroughtoaclearidea. And writing new programs isn’t just for obedient rule followers. Anyone motivated enough can write their own programs, given the proper instruction and patience. Once you understand the fundamental concepts, playing with software becomes an opportunity to play with the flow of thoughtitself. This book gives creative, intelligent people with ideas the mental tools to start programming computers and getting their ideas into the world. If you’ve never thought of yourself as the sort ofpersonwhocouldwriteaprogram,thisisthebookforyou. Introduction-LearningtoProgramwithArtandPython ii Almost everyone explores their ideas and shares their creativity through computers now. Why are sofewpeopleexploringnewideaswithprogramming? Andwhyareevenlesspeoplecreatingprogramsthatarefun? Why Art Inthisbook,we’llfocusonlearningtoprogramthroughartandthePythonprogramminglanguage. To an outsider, art and programming couldn’t be more different. One seems rigid, with precise instructionsthatarereadbyanunyieldingmachine,andtheotherisopentointerpretationbyerror prone,emotionalhumans. But the process of writing software is the same thing as the process of creating art. In each, we experiment, copy, explore, and then share what we’ve made. Art and programming are both mediumsforexploringourideas,andthensharingthoseideasforotherpeopletoplaywith. This book, however, isn’t about art with a capital “A”. We’re not making artwork fit for prestigious museums. Instead, this book is more like art with tiny “a”, as in arts and crafts, or better yet, finger painting. We’re just here to enjoy ourselves, and the act of putting paint on paper. Or in this case, codeonthecomputer. Step by step, we’ll make tiny, understandable changes to the code we’ve written, and reshape and mixideaslikeLegosintonewprograms. We’llwriteprogramsfordrawing,editingphotos,andmakinginfinitelyrepeatingshapes. By manually writing out and running each of these programs, you’ll get a feel for how to start exploringandsolvingproblemsonyourown. We’re just using drawing and art as a medium, because with art we’re allowing ourselves to make mistakesandnotbeperfect. Why Python Tocreateourprograms,we’llusetheopen-sourcePythoninterpreter. Open source is a software movement created by a global community of programmers around the world.Eachcontributestheirtimeandmindtocreatesoftware,andthengivesitawayforfree. Eachpersonmaycontributejustalittlebitofaprogram,butcollectively,theresourceofopensource softwareiswhatpowersmostoftheweb,fromFacebooktoGoogle,toalltheserversinbetween. Python is a programming language, but also a program that reads and runs programs. Python can loadandrunprogramsfromfiles,oryoucanwritecodelinebyline,directlyintoit.BecausePython readsyourprogramlinebyline,it’scalledaninterpreter. Inthisbook,we’llusethemainPythoninterpreter,alsocalledCython. Introduction-LearningtoProgramwithArtandPython iii As an interpreter, Python reads our programs line by line executing each instruction in order from the files we write. Indeed, we can also type out our programs directly into Python, and have our programsrunaswetype.Forabeginner,thismakesstartingwithPythonlessfrustrating,andmakes makingmistakeslesscostly. Python has also been around for a long time, receiving decades of continuous improvements from some of the best programmers in the world. Because of this, it’s also got over a hundred thousand “libraries”,orprogramswhichextendthecapabilitiesofthePythonprogramitself. Libraries can connect our Python programs to the internet, write AI, draw images, control 3D printers, direct robots, and more. Each of them just gives us a new set of tools to incorporate to ourprograms. Finally,Pythonisaveryeasyprogramminglanguagetoread,comparedtomostlanguages.Atfirst, looking at Python code might seem confusing, but there are very simple rules for Python code relative to other languages. It uses a lot of white spaces to designate what parts of the code belong toeachother.Thismakesreadingotherpeople’scodeeasier,andgivesplentyofother,opensource codetolookatifwegetstuck. The Creative Process of Programming As you progress through this book, you may encounter some points where you might not really understandwhat’shappening,orwhythingsmustbeacertainway. Withsomuchtolearnasanewprogrammer,itcanbeveryintimidating.I’vetriedtoanticipateall theplaceswhereyoumaygettrippedup,ornotunderstandfullywhat’shappening. Maintaining the balance between understanding concepts completely and not understanding them atallcandriveyoucrazyasabeginner. Aswegoalong,I’llcalloutthemostimportantpartsyoushouldfullyunderstand.Mosteverything else should be fine to skip over at first. Doing things repeatedly is the only way to really absorb all theideasofprogramming. Keep in mind, whenever we learn something new, our first efforts are often terrible. It can take us hours to type out and run our very first, simple program. The errors we get when we try running thingsmakenosense,andthetiniestthingsseemtomatterinwaystheycouldn’tpossibly. Goeasyonyourselfifyoufeelstuck.Justtrusttheprocess,thatthingswillgetbetter.Slowly,piece afterpiece,webegintofindourway.Soonwecansuccessfullymanipulateourmedium. The Journey of Becoming A Programmer Bytheendofthisbook,youwillbeaprogrammer. Introduction-LearningtoProgramwithArtandPython iv Thismeansyouwillbeabletocomeupwithnewideas,knowwheretobeginafterabitofresearch, andthenmaintaintheresolvetokeeptryinguntilyourideascometolife. Alongtheway,ifyougetlost,itpaystoreadotherpeople’scode.Thisisoneofthemostimportant things you can do when first beginning. Focus on reading other people’s code and understanding thewaysinwhichtheyhaveapproachedtheirownproblems. Initially,youmaywonderwhether thecodeyou’rereadingis goodorbad. Whatifyou’relearning the wrong ideas from “bad code”? Developing a taste for “good code” comes from reading a lot of code, and making the judgement for yourself. Usually it also means writing a lot of bad code yourself. Some people love filing their ideas into classes. Other people like making everything into afunctionthatreturnsavalue. Partofbecomingaprogrammerisdecidingwhichapproachresonateswithyou.Tryafewdifferent approachesout,evenifotherpeoplecallit“bad”.You’vegottostartsomewhere. No “Right Way” Asyouprogress,youwillinevitablyworrythatyou’reprogrammingthe“wrongway”—asifevery programyouwriteiswritteninthemostwrong,convolutedway.You’llwantsomebodytotellyou the right way to write code, and to show you how to organize ideas. But here’s a secret: Nobody is coming to save you. Instead, just remember that if you create solutions to your problems, you are doingagoodjob.Ifyou’velearnedsomethingnew,you’redoingagoodjob. You may wonder about code that you can maintain, or about code that’s fast, but for now, the onlyimportantthingisthatyourcodeisunderstandabletoyourself.Ifyoucanwriteandreadand understandwhateachlineofyourcodeisdoing,thenyouarealreadyagreatprogrammer. Enoughtalk,let’sbegincoding!

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.