Note that there are some explanatory texts on larger screens.

plurals
  1. POcentring an image ontop of another one in python(PIL), then hiding one inside the other
    primarykey
    data
    text
    <p>The task is to make two functions one that embeds a grayscale image inside a RGB one. The second then shows the hidden "watermark". I've completed the task( there is 15 tests the code must pass I passed them all :D), but I want to tidy up my code if possible. I have been told that I could have used the crop function to centre the water mark on top of the photo(I couldn't work out how to do this) instead I used math logic. </p> <pre><code>from PIL import Image #function for embedding the watermark def add_watermark(clean_file, watermark_file): #if the files are not compatible #or doesn't exist end the function try: clean_photo = Image.open(clean_file) watermark_photo = Image.open(watermark_file) except: return False #the images pixels tuple values are attained clean_photo_size = clean_photo.size watermark_photo_size = watermark_photo.size # .load was used to return a pixel access object # that can be used to read and modify pixels #for both images photo_array = clean_photo.load() watermark_array = watermark_photo.load() #centring the watermarked image on the photo start_x_coord=int((clean_photo_size[0]-watermark_photo_size[0])/2) start_y_coord=int((clean_photo_size[1]-watermark_photo_size[1])/2) #for the pixels that share the same position as the pixels in the #watermark, manipulate their RGB tuple value to include the watermarks #greyscale value as part of the original RGB tuple for line in range(watermark_photo_size[0]): for rank in range(watermark_photo_size[1]): photo_array [(start_x_coord+line),\ (start_y_coord+rank)]\ =(int(((photo_array [(start_x_coord+line),\ (start_y_coord+rank)][0]/10)*10)\ +((watermark_array[line,rank]/100)%10)),\ int(((photo_array [(start_x_coord+line),\ (start_y_coord+rank)]\ [1]/10)*10)+((watermark_array[line,rank]/10)%10)),\ int(((photo_array [(start_x_coord+line),\ (start_y_coord+rank)][2]/10)*10)\ +(watermark_array[line,rank]%10))) #create a new file name with _X at the end new_File_Name = clean_file[:-4]+"_X"+clean_file[-4:] #create a new file clean_photo.save(new_File_Name) #return true for the test return True </code></pre> <p>I tried to make this post spelling and grammar correct, but I have been awake for 20 hours now so I apologize if I missed anything.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload