0

I use a programme to store some image on a storage in line (swarm), where the link is directly compute from image data to build a hash.

The issue is that if I upload 2 times the same image, it will create the same hash, that I don't want : I want each hash unique, even if the photo is the same.

So I search a way to modify in in python just a little the image before to upload it.

I have try for example to modify 1 pixel with a random choice like that :

from random import randint

image = Image.open(file)
i = randint(30, image.size[0] - 30)
j = randint(30, image.size[1] - 30)
pixels = image.load()
pixels[i,j]=(pixels[i, j][0], pixels[i, j][1] - 1, pixels[i, j][2])

but for a reason I don't understand it doesn't change anything to the hash

I have also trying to modify so info of the image, without success (with the current UTC time + random code)

So do you have a solution to solve this problem ?

here you have the documentation of swarm : https://docs.ethswarm.org/docs/getting-started/upload-and-download which say "In Swarm, every piece of data has a unique address which is a unique and reproducible cryptographic hash digest. If you upload the same file twice, you will always receive the same hash. This makes working with data in Swarm super secure!"

so I don't understand why modifying 1 pixel is not working.

Also if it can help, here the way I upload a file on swarm

headers = {"content-type": f"image/png", "Swarm-Pin": "true"}
result = requests.post("https://gateway.ethswarm.org/files", data=file, headers=headers)

edit : the full code I have try as requested in comment:

image = Image.open(file)
output = io.BytesIO()
i = randint(0, image.size[0])
j = randint(0, image.size[1])
pixels = image.load()
pixels[i,j]=(pixels[i, j][0], pixels[i, j][1] - 1, pixels[i, j][2])
image.save(output, format=image_format)
result = requests.post("https://gateway.ethswarm.org/files", data=output.getvalue(), headers=headers)
4
  • You don't show what you do with the modified image, so it's hard to say. Commented May 16, 2021 at 2:04
  • It looks like you are passing requests.post your file, not your image. That might be okay, but it also looks like you are never saving your image after you make your changes. I think this might mean you are sending the same data to swarm every time. I don't know very much about swarm, but I would also suggest that you see whether this is the best way of accomplishing what you want to do—it seems unlikely to me that changing random pixels is the best way to do this. Commented May 16, 2021 at 2:05
  • I have added an edit with more code, hope it is more understandable now. I push on swarm after I have done a random modification of the image Commented May 16, 2021 at 2:10
  • That line pixels[i,j]= isn't changing the contents by very much. You don't show what image_format is, but if it's JPEG there's a good chance the output doesn't change at all. Commented May 16, 2021 at 2:17

1 Answer 1

1

Rather than alter the pixel content of your image, which is not very desirable, you might consider using the uuid module to generate a unique UUID, convert it to a string and save it as the comment in your PNG/JPEG file. That ensures that your hash will always be unique.

Note also, that merely reading a JPEG with PIL and saving it, even without changing a pixel, will probably cause changes to file size and pixel values because JPEG is lossy. So maybe also consider using exiftool to set the comment to avoid decoding and re-encoding the pixels.

Sign up to request clarification or add additional context in comments.

1 Comment

after some search based on your comment, I succeed using the exif in the save method of the image, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.