 Alrighty continuing off from where we left off last time. We just turned some images into four different PDFs. We got our beach Balloons mountain stars over here on the left as PDF forms whereas originally they were just straight-up JPEGs Just for example, here's our nice little beach dot PDF We're gonna actually this time destructively crop these PDFs was based off the comment of That our last crop wasn't actually a destructive crop it was actually just hiding and that's typical of Modern edits to PDFs and even some images is that it tries to save all that data Just so you can undo things later down the road But what if you do want to just get rid of it get a smaller file size or something like that? We don't want to just crop it. We actually want to cut it with a Which is basically using our media box rather than our crop box The way I like to look at it is there's a picture frame on the wall If you just take the picture You put in the frame usually you don't see edge-to-edge because that frame hides part of that image That's kind of like cropping it. Whereas if you actually truly destructively crop it You're taking the image out of the out of the frame and then just cutting it cutting the picture down to size That would be a true destructive crop. You won't ever get that back even if you do use some tape and glue But let's get on to doing this media box here We're gonna use our high PDF to Since last time they've updated a significant amount to a 3.0 And we're actually on 3.0 point one here, but in the 3.0 version they removed the PDF file reader and changes just a PDF reader and PDF writer took that File name out of it because it does more than just files now. We're still just doing it with the files up. We're gonna go with this PDF reader Function and pass in our beach PDF in read mode with the r command and just pass it into a reader object From there Rather than doing our dot get num pages, which is depreciated with 3.0 We're gonna do reader dot pages that would give us all our pages and we just want to get the length le n of that object That shows a set sure as Sure enough, we have just a single page because we started with a single image We haven't done anything to actually make it more than a single page with this current file But that's simple enough for this example So we're just gonna get reader dot pages zero. It's first page make that our page object And with this page object we can get the media box the dot media box dot lower left lower right and upper left and upper right to get our formats Just do some fancy printing here to actually have it say lower left is equal to those zero zero comment That's the bottom left pixel Then you get your lower right is 1620 by zero Upper left is zero ten eighty and upper right is our 16 20 by ten eighty as you can see I found some HD images here. That's gives us our ten eighty images But if we just do page dot media box, don't do the lower upper left and right We actually get a rectangle object returned to us That shows us our left bottom left and our upper right all in one little line here If that's all you needed is just to pull that out So we're gonna try to determine a crop area now now We can't just crop this and pretend that we can print it as an eight and a half by eleven because of Good printing resolution is actually 300 pixels per inch And if we take our 1620 divided by 300 and our ten eighty divided by 300 We only really can have a high resolution image of five and a half by three and a half roughly inches and But we're not really printing it right now. We're just we just wanted to make a Page in PDF so that we can send this file back and forth across internet as PDF format. So We're not gonna worry about that But just for information 300 pixels per inch is pretty standard for printing high resolution Stuff We're still gonna just jump to this eight and a half by eleven ratio So we got our paper ratio is a point seven seven our HD ratio actually is a point six six So since our HD ratio HD image ratio is smaller than our paper ratio We're gonna need to do a little conversion find a new width. It's 1080 divided by that patient paper ratio that gets us 1397 so what's what that means is our image currently is 18 I mean 1620 tall I mean wide by 1080 tall we're trying to crop Crop the sides off a little bit so that it actually is fitting an eight and a half by 11 ratio So we can fit it into a nice standard page For our PDF because hey most times PDFs are documents Now that we know our new width is 1398 we're gonna make a writer object with our PDF writer function here writer equals PDF writer and We're gonna just store that away for a little while But take our page that we had before page dot media box dot lower left equals. So we're actually changing it now Now we're gonna take our 1620 minus new width For our lower left and our upper left to also change it to 1620 by minus new width But this time it's 1080 The height and what we're doing here is we didn't want to go from zero on the far left here To 1398 over on this side because this tree is kind of cool. Whereas. This is just standard horizon and water We'll crop off this side. It's a little less exciting here. So rather than going Shrinking it down from the left side. We want to just shrink it down from the right side And it's why we're taking what's that difference that we're actually cutting off because we're moving our zero in Inside slightly so it's we're taking off about 300. So this is about roughly 300 here comma zero 300 comma 1080 Now that we saved that new page with its new Lower left and upper left We're gonna go into our writer dot add page and pass in that page that we just edited That that updates the writer object Then since we don't have any more pages to add we're just gonna go out stream equals open beach underscore cropped pdf That's our new file name don't have it anywhere here right now But we're gonna create it during that open process and then pass in WB so that we can actually write to it We're gonna do writer dot write pass in the file that we're trying to write to which is the out stream we just opened up and Then after that saves we're gonna do out stream dot close so we don't have to keep things lingering in the background here Now that we got that file saved let's beach underscore crop open that up and sure enough There's a lot smaller space over here, but just to be sure we'll make that full screen I mean size that to the screen here open up our old beach make sure that size to the screen and sure enough There you see that same tree on the right here and a bit of a gap over there Still have that tree didn't crop anything off the right side, but on the left side. It's much narrower in between that Umbrella and the edge of the image so Now jump into the other three images. We got a stars a mountains and a balloons These beach ones and back to our image here So starting off with our balloons, what looks good? Oh This is kind of nice over here, but there's a little bit of a bigger gap on this side. So How about let's do the right side this time instead? So just rather than before where we did the subtraction of the original width minus our new width to get how far in We should move from zero We're just going all the way to the other edge on the right side with our lower underscore right and upper Underscore right and doing our new width. That's our 1398 and we just put all that in from before into one little Command I mean command set here run that whole thing we now have our balloons cropped and Sure enough We cropped off that right balloon, but hey, what if we only wanted those two this a little more? Framed nicely and this one. This is a nice wide frame image But if we wanted to fold eight and a half by 11 That's what we needed. So We'll crop off the left of this mountains Because he's kind of jumping off to the right there. That's a little more progressive. We don't need this lake over here So do the same thing with the lower left upper left that we did earlier with a 1620 minus new width Run that command Open that guy up Cropped off half that light, but he's still jumping right in got a nice rule of thirds there Now The stars this guy is pretty dead centered in this with the galaxy right above him We kind of want to keep that so what if we don't do just the left or just the right, but do a little bit of both do some math here 1620 by Minus our new with that's how far we want to totally crop off the image and we just divide that by two So that's how far in we're going on the left just halfway in that we originally did and then our other side We want to go new with that's how far over we were if we only went that far We'd be taking the whole right side off, but we want to do that same thing above the 1620 minus new width divide by two I'll take us only half of that distance in so add that to our new width get a little bit of both sides Our stars There you go nice and centered still original new cropped off we now have our background of our PDF reader So what do we do before we want to make all four of these into just one PDF in Python? we're going to Use a PDF writer make a writer object and to make a little for loop here with our four different file names Cycle cycle through them all with a four in PDF in File name array. We're just going to do writer dot append PDF. This append function is just going to Take the file name that you do That you added and just append it to the end of wherever writer is already at we started off with a blank function So the beach will be the first page followed by balloons mountains and then stars lastly we'll just do writer dot write and Write it to emerge dot PDF. This is a slightly different way to do a right We don't have to necessarily always do an open stream But that was used for the actual pages where you need to turn it into from a page object into a writable object here since we just taking the PDF name PDF file and just pending it we can easily just do a dot write into the PDF Then to close it off. We'll do a writer dot close. Let's run this here Got this not wonderful merge file now. Let's take a look. Well, you know one of four pages here beach balloons Mountains and stars Now what if we wanted to change it around a little bit and didn't want the stars as the last one But one of the second one and we already had it Saved as that merge one we can necessarily in Independently take all these files again. We're gonna take this writer dot PDF writer We're gonna take our merged file say that was original one and take our first merge open that merge dot PDF And then we're gonna take our stars. We just gonna shove that in At some point. So we're gonna open that one in as a separate file as stars equals open Stars crop dot PDF RB. Both of those are readable objects Now we're gonna go reader dot append. We're gonna get a little fancier this time with it first merge That's our merge file for but we only take want to take pages Zero to three. So we're actually just taking one two and three indexed starting at zero though, of course and And Start that with the pen so a new writer function just has First three pages dropped off that last page which happened to be the stars and then we're gonna do another merge writer merge we're gonna take a writer function that has the first three pages again Start with position one Which isn't page one page two because we positioned zero as a first page and then we're gonna in merge in The file object stars. So we already opened up the stars cropped. It's a single page if it was more It would shove all those in starting on page two But we're just doing a single page. So that'll be page two followed by the original page two Just do a simple rate merge new PDF There we go one of four pages we get our beach followed by our stars this time balloons and And mountains Now I know that wasn't the fishing way to do it, but just wanted to show you some of the capabilities Sometimes you might not be able to do the original merge I mean a pen to function and you might need to use a merge. So just wanted to point that out there Hope this helps Please like subscribe and add any comments about what you think you might want to see next Until next time Have a great day