Method of generating a copy detection pattern,CDP, or a portion of a CDP, for printing on a substrate, comprising: a) generating a test digital file of an image comprising an at least partially random two dimensional distribution of dark and light pixels, b) applying a first print simulation filter to said digital file configured to generate a first print image file simulating an original print of said image on a substrate, c) applying a second print simulation filter to said first print image file to generate a second print image file simulating a printed copy of said original print, d) computing a copy detection performance for said digital file indicative of a measure of the differences between the printed copy and the original print, e) creating a modified digital file by modifying said test digital file by changing one or more pixels from dark to light or light to dark, f) applying steps b) to d) on the modified digital file, g) comparing the copy detection performance of the test digital file with the copy detection performance of the modified digital file, h) if the copy detection performance of the modified digital file is indicative of a greater difference between the printed copy and the original print than the copy detection performance of the test digital file, replacing the test digital file with the modified digital file, which now becomes the test digital file, i) if the copy detection performance of the modified digital file is indicative of a smaller difference between the printed copy and the original print than the copy detection performance of the digital file, repeating steps e) to h), j) repeating steps b) to i) until a termination condition is met, k) after step j), forming said CDP or said portion of CDP, from the test digital file. a) generating a test digital file of an image comprising an at least partially random two dimensional distribution of dark and light pixels, b) applying a first print simulation filter to said digital file configured to generate a first print image file simulating an original print of said image on a substrate, c) applying a second print simulation filter to said first print image file to generate a second print image file simulating a printed copy of said original print, d) computing a copy detection performance for said digital file indicative of a measure of the differences between the printed copy and the original print, e) creating a modified digital file by modifying said test digital file by changing one or more pixels from dark to light or light to dark, f) applying steps b) to d) on the modified digital file, g) comparing the copy detection performance of the test digital file with the copy detection performance of the modified digital file, h) if the copy detection performance of the modified digital file is indicative of a greater difference between the printed copy and the original print than the copy detection performance of the test digital file, replacing the test digital file with the modified digital file, which now becomes the test digital file, i) if the copy detection performance of the modified digital file is indicative of a smaller difference between the printed copy and the original print than the copy detection performance of the digital file, repeating steps e) to h), j) repeating steps b) to i) until a termination condition is met, k) after step j), forming said CDP or said portion of CDP, from the test digital file.