Saving multiple images with Python - python

For my cancer research I need to turn cancerscans into black and white images, and save them. I've this code, which turns the first file in the folder into a black and white picture, and copies it 49 times with names like result1, result2 , result3 etc.
wd = os.getcwd()
wd = os.chdir("C:\\Users\\Tije\\Documents\\School\\DeepLearning\\IDC_regular_ps50_idx5\\8863\\test")
for x in range(50):
for file in os.listdir(wd):
image_file = Image.open(file)
image_file= image_file.convert('1')
print(image_file)
image_file.save(f"result{x}.png")
I need the code to blackandwhite'n every picture in the folder and not just the first one of course. I can't seem to understand why it does this.
Any help?

You're looping over the entire directory 50 times and thus result{x} gets overwritten 50 times.
If you want to index for each result, just use enumerate as follows:
for index, file in enumerate(os.listdir(wd)):
image_file = Image.open(file)
image_file= image_file.convert('1')
print(image_file)
image_file.save(f"result{index}.png")

Related

Saving images with different name in folder

I tried save images in folder like this, it saves different images but every next image have all names of previously images.
db = h5py.File('results/Results.h5', 'r')
dsets = sorted(db['data'].keys())
for k in dsets:
db = get_data()
imnames = sorted(db['data'].keys())
slika = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.imwrite(f'spremljene_slike/ime_{imnames}.png', slika)
So i tried like this and it saves different names but only last generated picture is imwrited in folder, so different names - the same picture
NUM_IMG = -1
N = len(imnames)
global NUM_IMG
if NUM_IMG < 0:
NUM_IMG = N
start_idx,end_idx = 0,N #min(NUM_IMG, N)
**In different function:**
for u in range(start_idx,end_idx):
imname = imnames[u]
cv2.imwrite(f'spremljene_slike/ime_{imname}.png', imname)
Can someone help, I can't figure out.
I have script which generate images with rendered text and save it in .h5 file, and then from there I want to save this pictures with corresponding names in different folder.
Don't see how this works at all. On line 1 you define db=h5py.File(), then on line 4, you redefine it as db=get_data(). What is get_data()?
It's hard to write code without the schema. Answer below is my best-guess assuming your images are datasets in db['data'] and you want to use the dataset names (aka keys) as the image names.
with h5py.File('results/Results.h5', 'r') as db:
dsets = sorted(db['data'].keys())
for imname in dsets:
img_arr = db['data'][imname][()]
slika = cv2.cvtColor(img_arr, cv2.COLOR_BGR2RGB)
cv2.imwrite(f'spremljene_slike/ime_{imname}.png', slika)
That should be all you need to do. You will get 1 .png for each dataset named ime_{imname}.png (where imname is the matching dataset name).
Also, you can eliminate all of the intermediate variables (dsets, img_arr and slika). Compress the code above into a few lines:
with h5py.File('results/Results.h5', 'r') as db:
for imname in sorted(db['data'].keys()):
cv2.imwrite(f'spremljene_slike/ime_{imname}.png', \
cv2.cvtColor(db['data'][imname][()], cv2.COLOR_BGR2RGB))

Load images from data stream and not from a file

I have a program (in Pyglet) in which I would like to keep the icons of them in the form of bytes, then convert them into temporary images and pass them to the program to be able to use them.
This is a dictionary where the data of the two icons are present:
ico = {
"ico_16": {
"dimension": (16, 16),
"byte": b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x10\x00\x00\x00\x10\x08\x06\x00\x00\x00\x1f\xf3\xffa\x00\x00\x00\x04gAMA\x00\x00\xb1\x8f\x0b\xfca\x05\x00\x00\x00 cHRM\x00\x00z&\x00\x00\x80\x84\x00\x00\xfa\x00\x00\x00\x80\xe8\x00\x00u0\x00\x00\xea`\x00\x00:\x98\x00\x00\x17p\x9c\xbaQ<\x00\x00\x00\x06bKGD\x00\xff\x00\xff\x00\xff\xa0\xbd\xa7\x93\x00\x00\x00\x07tIME\x07\xe2\x08\x05\x12-\x0f?\xc1\x9b\xba\x00\x00\x03\x0fIDAT8\xcbU\x92Kh\\U\x00\x86\xbfs\xee\x99{g\xe6>f:\xe9<,3i51\xa6\x92B\xb5iHQ\n%.\n\xf6A)E(\xb5YtS\x8a R\\\xe8F\r\xe2^\x10WZ\x91\xae\x8a\x8b`\t\xbaP\xac\x0b\xad\x8a\xf4\x85\n\xadv\xa16\x19\'N\x9b\xcc+3\xb9\xcf\xe3b\xda\x85\xff\xf2\x87\xff\xdb\xfc\x9f`Q\xc31\x01\x07\xce\xc0\xee\xb9\x14?^~\x81^{\x9e`\xf3y\x92\xa4\x0c\x80\x94\xab\x98\xe9\xefpr\x9f2{\x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92F\x03\xc41H\xb3)q\xb6^\x17I#\xab\xd1d\xe7\xd6\x01A\xbbM{u\x9d\xa9r\xc8z+\x81\x10d\x1c"7W\xf8\xe7\x8f_\x18\x89\xd6(Y#\x12\x83W\xba\xae\xe4\xcc\xc1\xcf\xad\x82\xbd\xff\xeeW\x9f9!\x12_\x18$R\x91\xcdo!\xb6\xca\xcc\xceN\x12\xf7\xd7\x88\xda\xf7\xa0\xdb\xe2\xb7\xef\rF\'\xc6P[\n=1}\xf0\xb2P_\xeaQyaa!\xf8\xf5\x87y,\x17\x9c\x1cX\x19H\xa5\xc1r1\xbd\x02\x89? Z\xaf\xc3\xa0\x0bA\x88\x88\x03\xd4\xd8\xae\x8b\xf1\xfc\x1bo\xa9\xa8B\x03\xa7\xf06J\x14\xe8\xd4\x0f\xe3w\x86*\xa7,P&\xc1\xfd\xbf \x89\xfe\xa7\xb2v\xf2K\xe1\xc8\xb6w(Q\x17\\X\x85W\xf7\xc0s\xc7*\xd4\xef\xbe\xc9F\xf7e4y\x0csx\x15#\x1cA\x1c\x80\xa0\x85\xed^\xa4:\xf1\x1e_\xbc\xdf\xe0\x93\x7f\x91\xe4K\xd0]\x86\xcd\x8d\x06s\xa7\xceS\xaa\x9e\xc4v/!\xf5\n\xb1\x1f\x11\xfb\x11R\xaf`\xbb\x97(UO2w\xea<\x9b\xfd\x06ZC\xae\xc8\x7f\xde\x160\x90\xb5\xbaI\xa8\x00\x00\x00%tEXtdate:create\x002018-08-05T18:45:15+00:00\xbf\xac\x1c\xb8\x00\x00\x00%tEXtdate:modify\x002018-08-05T18:45:15+00:00\xce\xf1\xa4\x04\x00\x00\x00\x00IEND\xaeB`\x82'
},
"ico_32": {
"dimension": (32, 32),
"byte": b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00 \x00\x00\x00 \x08\x06\x00\x00\x00szz\xf4\x00\x00\x00\x04gAMA\x00\x00\xb1\x8f\x0b\xfca\x05\x00\x00\x00 cHRM\x00\x00z&\x00\x00\x80\x84\x00\x00\xfa\x00\x00\x00\x80\xe8\x00\x00u0\x00\x00\xea`\x00\x00:\x98\x00\x00\x17p\x9c\xbaQ<\x00\x00\x00\x06bKGD\x00\xff\x00\xff\x00\xff\xa0\xbd\xa7\x93\x00\x00\x00\x07tIME\x07\xe2\x08\x05\x12+\x1a\x04F\xd8\xd7\x00\x00\x08\xc3IDATX\xc3\x9d\x97il\\\xd5\x15\xc7\x7f\xf7\xad\xb3\xd8c{<\xb6gL\xec\xd8\xb1]\xc7\xc4\x91\x1bB\x0c\x844II\xa9h\x8b(\x82\xb2\xb4H]>\xb0tQEAU#\xb4\xaa\xdb\xaa-\x15-\x82~#\x84(m?P\xf5C\x05\xdd\xa8\x8ah\xd9B\x81`\'\r\x11 \x12;\xc4\x89q\xf06\xf6,\x9ey\xb3\xbc\xad\x1f\xee\x9b\xd8NhUq\xa5\xa3;\xbao\xee9\xffs\xee\xb9\xe7\xfc\xaf\xe0\xc3\xc6]O#n\x01BQ\x98\x9d\x84\x07\x1fQ\xf8\xf1\x0f\xfa\xc9/\xee\xa1\xb4\xfcI*\xd6f\xecj\x12\xcf\x8d\x02\xa0\xa8Etc\x163\xf2\x16\xe1\xfa\x17\x895?\xcf\xf7\x7fx\x82\xfb\xee\xf6HvC\xb9\x08\r-\xf0\xf8\x1d\x17\x98\x12\x00<\xed\xaf\xac\xfc\xf9a8\xfd6\xc4S\xf0\x8b\x9f\x08\xbe\xb3w\x98\xcc\xecW\xc9/^C1\xdfA\xb5\xac\xe2\xb9\xe0\xafl\x07_\xfeTT0B.\xd1\xd8\xfb\xc4\x9a\x9f\xa5)\xf9[\x1e\xfe\xf9(\xdf}\xc0gi\x06\xba\x06\xe1\xf3\xf7\xac\xd8\xbaA \xd6\x18\x0f\x033s\xf0\xfb\x11H\xacK1s\xean\x16\xcf~\x8d\xfcR\x0b\x8e\x03\x8a\x06\xaa.gEY\x0b\xc0\xf3\xc0s\xc0\xb5\xe5\xaci\x10\x8b\xa7i\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\x85\xe0\x9c[\x86\x01\xe1VR\xedI,_\xe7\xd0\xac\xc2\x86T\\\x1e\x89\xae\xae\xee$\xe7\x86\xe2\xc2\x1b\x93\xd0\x9b\x8aR\x97J\x81\xd9,E\xab\x0f\x00hr\xd6\x0c\xf0=\xb02\xfd\nU\xab-\xe8h\x81\xd2#\xab\x06\x84\x14\x84\xaePq\xc0\xb2\x01U\x80)\xe4\xb7\xd5\xc6]\xc0\x93K\xf94Le\x14\xb6\xf4\x07\xf9b\xb4\x80\x16\tt+R\x7f\xcdV\xd5jS\xf0\x9c0\x8a\x90\xb5]\x08\xa9\xa5\x16^mU\x98k\xc5\xb3\xb6\xb6zx\x81\xf8#\xd9\xe7\xc8\x84M_\xd2\xa4\xae-\x01F\x93\x8c\x98X\x05\xbaf\xcbs\xc2\xe7\xabZ\x15Oy\xae5\x00k\x80}\x18\x00?\x98\xed\n\x99\x99,S\x8b6[\xfa"\x10\xd2\x11\xea\x9a\xe6\x7f\x9e\x19E+\xe1\x05\xdd\xcc\xf7\xd7~=\x7f\xc3j\x00\xab=\xaaE\xc0\xf3\xc1\xceA)\xcd\x91\x139\xfaZ}\xa2\t\xf0\x95\xf3ty\xbe\xb4\xa5h%\x05#2/\x17\x9ds\x9du\x8d\xfc?\xa3\x16\x01\xdf\x01\'\x0b\xf6\x12\x99\x0f\xe6\x99J\x97\xd9\xd2\x05\x9e\xbaJ\x97\x8flN\x00FdN\xa1.~\x02\xa1\x80c\x83\xef\xe1{\xdeJbUV\x94\xfb5#\x8e\xfc&\x08\xd6\xd6\x80v\xc0\xc9\xcb(\x14\x178\xfcn\x9a\x9e\xb8O\xac>\xb0_\x03\xe1\xba\xf2\xeeF\x9bO($\xd6\x8fbD\\\xec\x12\xf8\x1ev\xa1\x80U\xb2\x19h\x87h#\x0c\xb6\xc2|\xceg~\xd1\xa3\xb3\x01\xe2M\xd0\x11\x97y\x95-\x04\xb4\xc2\xa9\x01u\xc0Y\x96 \xcai\xb2g\xa685[\xe0\xf2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xc4\xde3\xad\xf4\x83\x00\x00\x00%tEXtdate:create\x002018-08-05T18:43:26+00:00\r\xd5q\x81\x00\x00\x00%tEXtdate:modify\x002018-08-05T18:43:26+00:00|\x88\xc9=\x00\x00\x00\x00IEND\xaeB`\x82'
}
}
While this is the part of the script in which it is converted into temporary images and passed to Pyglet:
temp_ico = []
for ic, ic_key in ico.items():
with BytesIO() as ic_key["byte"]:
from PIL import Image
with Image.new("RGB", ic_key["dimension"]) as ico_img:
ico_img.save(ic_key["byte"], format="PNG")
ico_img = pyglet.image.load(ico_img)
temp_ico.append(ico_img)
self.set_icon(temp_ico[0], temp_ico[1])
The problem I'm having is that Pyglet, with: pyglet.image.load(), expects the file path and not the file itself.
Since the file is not physical, I do not know how to get the path of it to get it used by Pyglet. Do you have any suggestions or advice on how to solve the problem?
Edit:
I tried with both answers obtained from "Torxed":
temp_ico = []
self.temp_sp = []
for ic, ic_key in ico.items():
ico_img = pyglet.image.ImageData(ic_key["dimension"][0], ic_key["dimension"][1], 'RGB', ic_key["byte"])
temp_ico.append(ico_img)
temp = pyglet.sprite.Sprite(ico_img)
temp.x = randint(0, 100)
temp.y = randint(0, 100)
self.temp_sp.append(temp)
self.set_icon(temp_ico[0], temp_ico[1])
And:
temp_ico = []
self.temp_sp = []
for ic, ic_key in ico.items():
ico_img = pyglet.image.SolidColorImagePattern((255, 255, 255, 255)).create_image(ic_key["dimension"][0], ic_key["dimension"][1])
ico_img.set_data('RGB', ico_img.width * len('RGB'), ic_key["byte"])
temp_ico.append(ico_img)
temp = pyglet.sprite.Sprite(ico_img)
temp.x = randint(0, 100)
temp.y = randint(0, 100)
self.temp_sp.append(temp)
self.set_icon(temp_ico[0], temp_ico[1])
But in both methods I get a mix of confusing pixels and not the original image.
I have also modified the method in which I obtained the printing of the image bytes. Before I used this method:
from io import BytesIO
from PIL import Image
with BytesIO() as output:
from PIL import Image
with Image.open("ico_32.png") as img:
img.convert('RGB').save(output, "png")
data = output.getvalue()
print(data)
Which I had obtained from here Answer by: perillaseed
But then I changed to:
s = open("ico_32.png", "rb")
data = s.read()
print(data)
s.close()
f = open('new_image.png', 'wb')
f.write(data)
f.close()
If I go to check the new saved image, it is exactly the same as the original, so I copy the byte string manually, insert it in the dictionary and it does not work.
Here are the visual results:
Also, I wanted to warn that I put the two icons as sprite and designed to see them better, since they were too small on the actual icon of the program.
To respond to snakecharmerb, I tried when you answered the suggestion you gave me, but as he also answered Torxed, I get the error he reported himself.
Edit2:
I tried to change the icons (since the two I used were only for testing and not the final ones of the program) with two others, but I get the same result. Above are the original ones, under the new ones.
Bytes string (new_ico_16):
b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x10\x00\x00\x00\x10\x08\x03\x00\x00\x00(-\x0fS\x00\x00\x00\x03sBIT\x08\x08\x08\xdb\xe1O\xe0\x00\x00\x00\tpHYs\x00\x00\x01\xc1\x00\x00\x01\xc1\x01E\x85\\9\x00\x00\x00\x19tEXtSoftware\x00www.inkscape.org\x9b\xee<\x1a\x00\x00\x00\xd2PLTE\xff\xff\xff\x00\x00\x00O33gNNm/1l/1t./\x87`cy*-\x86_b\x8f*+\x8efiz,/\x90hl\x94!$\xa1\'(\xa3{|\x8eln\xb4$&\xb9 !\xa1\x88\x8b\x8b"#\xae\x8f\x91\xbc\xa8\xaa\xbe\xb0\xb2\xe4\x19\x1a\xec\x1d\x1d\x90\x8d\x90\x85\x88\x8c\x89\x8b\x8e\x8b\x92\x96\x90\x95\x98\x96\x9e\xa2\x97\x9f\xa3\xa2or\xa4`b\xa9\x19\x1a\xaa\x1b\x1d\xaa\xb5\xba\xaa\xb6\xbb\xab\xb2\xb7\xab\xb3\xb8\xab\xb4\xb8\xab\xba\xc0\xb7\x11\x12\xb9\xc4\xc9\xbe\xc0\xc2\xbf\xbb\xbc\xc4\x14\x15\xd0\xde\xe3\xd6\t\t\xd8\n\n\xd8\xe5\xea\xda\n\n\xda\x0b\x0b\xda\xe6\xeb\xdb\xe7\xec\xdc\xe8\xec\xe0\r\r\xe1\r\r\xe2\xec\xef\xe2\xec\xf0\xe4\xe5\xe6\xe7\x10\x10\xe8\xf0\xf3\xec\xea\xeb\xf4\x16\x16\xf5\xf8\xfa\xff\x1a\x1a\xff\xff\xff\xeb\x12-N\x00\x00\x00\x1ctRNS\x00\x01-4\x81\x82\x91\x9d\x9e\x9e\xb0\xb2\xb8\xc1\xd0\xd1\xd8\xd9\xe5\xe9\xf1\xf2\xf2\xfb\xfc\xfd\xfd\xfe(\xffw\x99\x00\x00\x00\xb0IDAT\x18\x19=\xc1\x89\x1a\x81#\x14\x06\xd0\x7f\n\x955Id\xdf\xf7\xc24\xd6\x90\xf5\xfd_\xc9\x1d\xfa:\x07\x92\x92\xc9\x96J\xd9\x8c\x82?\x96\xb6\xde?V\x9a\x81\xb0|\xe3\x19\x85\xfb}\x18=\x1by\x06 \xe5\x9ey\xec\xec\xa6\x00\xa5\x12\xf0DPQ\xa0\xef\xb8\xb4\xd9pi\xa7\xa3\xc8\xa5\xc1l6\xe4R\x11\xd5\x15\x19\xcf\xbb\x9d\xf5hE\xaahzd\xbah\xb7\xfc\x89G\x9a\xb0\x85\xd4\xf7\xfd\x9e\x90l\x14\x8e\x82l\x97\xcb\xad \xc7\x02\xd4\xfaA$\x0eu\x15\xd0n\x17\x11\xbb\xdc4\x00\xac\xfcx\xdd\xaf\xa7\xd3\xf5\xfez\x94\x19\x083j\x9f\x9f\x9a\xc1\xf0\xa7\xe6L\xc71s*\xc8\x17B\xf5#3;\xd8r\xdc\x00\x00\x00\x00IEND\xaeB`\x82'
Bytes string (new_ico_32):
b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00 \x00\x00\x00 \x08\x03\x00\x00\x00D\xa4\x8a\xc6\x00\x00\x00\x03sBIT\x08\x08\x08\xdb\xe1O\xe0\x00\x00\x00\tpHYs\x00\x00\x03\x82\x00\x00\x03\x82\x01\x83\x06\xfdz\x00\x00\x00\x19tEXtSoftware\x00www.inkscape.org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f\x8e\x89\x01<\xe9q;;\x87\x85\x16h`\xc2\xef\x1f\xec\xdb\xe0\xe0w\xb4\xfdbB\x03\x94.\xb3\x08\xcf\x87\x05t\xbb\xf1\x9f\xf9/\x8bX.\x85\xd4i&\x8c\x9amN\xa7\xcd<\xca\x84\xe9T\xd0\xf43\xc13\xd6\xdb\xd5\xd5;\xe6aB\xbf\x06\xe4\x8a\x00\x8b\x9a\x9f\x99\x99gQ\x81\n\x19\xc0\xb0\xb8\xc9\x12\xd8\\4\x00\x00\xa9\xba\xf3\xb1\xb8|wU\x04\xc2tm\xa1=\x16\xc7^\xa8M\x07\xcf$\xd3*\xde\x04\xd8\x1b\x81\x1b\\5I\xf0\x82\x94\x1c#^l3\x85\xed\x0b\xc4\xe3\x12\x02\xafT\xc5\xeb\x88\x18\xba>;\xdc\xf7\xf9\xf6\x0f\xcf\xaeC\x88\xb8^\xac\x02\x81\x98\xda\x9f0\xc6S\xbb\x89\x80\x82\xa4\xab\x9eC\x85\xb9j\x9d\x04\xb1\x88\xa1\xb2u\xed\x01\xc3\x1e\xd6Z+\r\x04\xde\x935)9E\xe5\xe5E9)\x1a\x19\x84\xff\xe6\x8b7\xda7\x9aJ\x93\x00\x00\x00\x00IEND\xaeB`\x82'
I also tested this way (by modifying the script in the first edit), but with no different results from before:
s = open(ic + ".png", "rb")
data = s.read()
ico_img = pyglet.image.SolidColorImagePattern((255, 255, 255, 255)).create_image(ic_key["dimension"][0], ic_key["dimension"][1])
ico_img.set_data('RGBA', ico_img.width * len('RGBA'), data)
s.close()
What #snakecharmerb said is completely true, you could load it via a BytesIO stream. And it's usually the way most people go. The problem with it is that you need the complete data in order for it to load, otherwise you'll get:
Traceback (most recent call last):
File "test.py", line 19, in <module>
kitten = pyglet.image.load('kitten.png', file=kitten_stream)
File "C:\Program Files\Python37\lib\site-packages\pyglet\image\__init__.py", line 205, in load
raise first_exception
File "C:\Program Files\Python37\lib\site-packages\pyglet\image\__init__.py", line 195, in load
image = decoder.decode(file, filename)
File "C:\Program Files\Python37\lib\site-packages\pyglet\image\codecs\gdiplus.py", line 283, in decode
bitmap = self._load_bitmap(file, filename)
File "C:\Program Files\Python37\lib\site-packages\pyglet\image\codecs\gdiplus.py", line 226, in _load_bitmap
'GDI+ cannot load %r' % (filename or file))
pyglet.image.codecs.ImageDecodeException: GDI+ cannot load 'kitten.png'
To get around this, you can create your own valid image structure and in-line replace the data.
And I like quirky solution, so I'll drop this little nugget for anyone who needs to manipulate data/pixels in a way you're planning on doing.
ico_img = pyglet.image.SolidColorImagePattern((255,255,255,255)).create_image(16, 16) # Width, Height
ico_img.set_data('RGB', image_data.width*len('RGB'), ico['ico_16']['byte'])
What this does, is basically just crate (pretty quickly) a solid image blob with the right format. Then it in-line replaces the data with whatever you give it. The only important thing here is that your width and height matches the data-length/format chosen. I assumed the data you've got going is RGB and it appears to be working.
Now, I recon this is the fastest way to do it.
But there's a better and almost as fast way of doing it, and it's more readable:
ico_img = pyglet.image.ImageData(16, 16, 'RGB', ico['ico_16']['byte'])
This accepts any data stream that's valid RGB data (0-255).
Lastly, don't forget to place your image in a sprite object to make it faster and easier to work with:
ico_img_sprite = pyglet.sprite.Sprite(ico_img)
It'll make your life easier in the long run! :)
Edit: there are three differences in the first post and the methods to solve this.
Firstly, the original image data appears to be broken, so it's hard/impossible to load.
Secondly, seeing as the new raw data contains meta-data such as headers, creation timestamp etc. It will only be loadable via the io.BytesIO method, because it needs to go through any available image decoder.
Thirdly, you need to extract the image data (pixel-data) from the image in order to use any methods posted in my answer. Since it will bypass any image encoders and/or compressions. Making it a little bit faster, but at the expense of image data size.
Here is snakes answer working with the new data:
raw_image = b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x10\x00\x00\x00\x10\x08\x03\x00\x00\x00(-\x0fS\x00\x00\x00\x03sBIT\x08\x08\x08\xdb\xe1O\xe0\x00\x00\x00\tpHYs\x00\x00\x01\xc1\x00\x00\x01\xc1\x01E\x85\\9\x00\x00\x00\x19tEXtSoftware\x00www.inkscape.org\x9b\xee<\x1a\x00\x00\x00\xd2PLTE\xff\xff\xff\x00\x00\x00O33gNNm/1l/1t./\x87`cy*-\x86_b\x8f*+\x8efiz,/\x90hl\x94!$\xa1\'(\xa3{|\x8eln\xb4$&\xb9 !\xa1\x88\x8b\x8b"#\xae\x8f\x91\xbc\xa8\xaa\xbe\xb0\xb2\xe4\x19\x1a\xec\x1d\x1d\x90\x8d\x90\x85\x88\x8c\x89\x8b\x8e\x8b\x92\x96\x90\x95\x98\x96\x9e\xa2\x97\x9f\xa3\xa2or\xa4`b\xa9\x19\x1a\xaa\x1b\x1d\xaa\xb5\xba\xaa\xb6\xbb\xab\xb2\xb7\xab\xb3\xb8\xab\xb4\xb8\xab\xba\xc0\xb7\x11\x12\xb9\xc4\xc9\xbe\xc0\xc2\xbf\xbb\xbc\xc4\x14\x15\xd0\xde\xe3\xd6\t\t\xd8\n\n\xd8\xe5\xea\xda\n\n\xda\x0b\x0b\xda\xe6\xeb\xdb\xe7\xec\xdc\xe8\xec\xe0\r\r\xe1\r\r\xe2\xec\xef\xe2\xec\xf0\xe4\xe5\xe6\xe7\x10\x10\xe8\xf0\xf3\xec\xea\xeb\xf4\x16\x16\xf5\xf8\xfa\xff\x1a\x1a\xff\xff\xff\xeb\x12-N\x00\x00\x00\x1ctRNS\x00\x01-4\x81\x82\x91\x9d\x9e\x9e\xb0\xb2\xb8\xc1\xd0\xd1\xd8\xd9\xe5\xe9\xf1\xf2\xf2\xfb\xfc\xfd\xfd\xfe(\xffw\x99\x00\x00\x00\xb0IDAT\x18\x19=\xc1\x89\x1a\x81#\x14\x06\xd0\x7f\n\x955Id\xdf\xf7\xc24\xd6\x90\xf5\xfd_\xc9\x1d\xfa:\x07\x92\x92\xc9\x96J\xd9\x8c\x82?\x96\xb6\xde?V\x9a\x81\xb0|\xe3\x19\x85\xfb}\x18=\x1by\x06 \xe5\x9ey\xec\xec\xa6\x00\xa5\x12\xf0DPQ\xa0\xef\xb8\xb4\xd9pi\xa7\xa3\xc8\xa5\xc1l6\xe4R\x11\xd5\x15\x19\xcf\xbb\x9d\xf5hE\xaahzd\xbah\xb7\xfc\x89G\x9a\xb0\x85\xd4\xf7\xfd\x9e\x90l\x14\x8e\x82l\x97\xcb\xad \xc7\x02\xd4\xfaA$\x0eu\x15\xd0n\x17\x11\xbb\xdc4\x00\xac\xfcx\xdd\xaf\xa7\xd3\xf5\xfez\x94\x19\x083j\x9f\x9f\x9a\xc1\xf0\xa7\xe6L\xc71s*\xc8\x17B\xf5#3;\xd8r\xdc\x00\x00\x00\x00IEND\xaeB`\x82'
image_buffer = io.BytesIO(raw_image)
image = pyglet.image.load('temp.png', file=image_buffer)
image_sprite = pyglet.sprite.Sprite(pyglet_image)
If you want to store the pixel data alone, you could extract it like this:
print(image.image_data.get_data('RGBA', 16*4))
Where 'RGBA' is the format you want on the data, and 16*4 is the number of pixels per row times the number of colors per pixel (RBGA is 4).
That will give you the option to do:
raw_data = b'\x00\xae\xff\x00\x00\x00\x00\x00\x00#b\x00\x00q\xa5\x05\x009XI\x00-I\xa8\x00\'A\xe4\x00%?\xfb\x00%?\xfb\x00\'A\xe4\x00-I\xa8\x009XI\x00q\xa5\x05\x00Be\x00\x00\xff\xff\x00\x00\xae\xff\x00\x00\x9e\xe9\x00\x00\r\x1c\x00\x00Hm\x18\x00.I\x92\x00#<\xef\x00"<\xff\x00%B\xff\x00(F\xff\x00(F\xff\x00%B\xff\x00"<\xff\x00#<\xef\x00.I\x92\x00Jo\x18\x00\x00\x00\x00\x00\x9f\xea\x00\x00\xcf\xff\x06\x00a\x93 \x00-I\xb2\x00";\xff\x00)G\xff\x002T\xff\x007[\xff\x00;a\xff\x00;c\xff\x009_\xff\x003V\xff\x00)H\xff\x00"<\xff\x00.L\xb3\x00i\xa0#\x00\xcb\xff\x07\x04\xb7\xff\x1e\x06U\x81\xa0\x00)H\xff\x000U\xff\x00<d\xff\x00Cm\xff\x00Hu\xff\x00O\x82\xff\x06]\x94\xff\x04V\x8b\xff\x00Eq\xff\x00<d\xff\x006_\xff\x013Y\xff\x05V\x86\xa2\x02\xb5\xff!\x02v\xb4f2\x8d\xbf\xf3\x10[\x95\xff\x0b[\x97\xff\x04\\\x9a\xff\x02^\x9e\xff\x0cn\xaf\xff\tu\xc6\xffA\xad\xee\xff5\x9f\xdc\xff\x01Z\x92\xff\x00M\x82\xff!u\xb0\xff?\x9b\xd3\xff(\x88\xbf\xf3\x01u\xb5g\x00#l\xb5+{\xab\xff4\xa0\xea\xff:\xaa\xf6\xff4\xa5\xf0\xff,\xa0\xee\xff;\xb0\xfa\xff4\xaf\xfd\xff8\xb3\xff\xff3\xa9\xf4\xff\x05i\xaf\xff\x00d\xad\xffL\xb0\xec\xff[\xc2\xff\xff$w\xad\xff\x00Am\xb5\x004X\xe7*}\xb1\xffD\xb8\xff\xff\x11\x96\xff\xff:\xb4\xff\xff;\xb7\xff\xff\x0f\x99\xff\xffE\xbf\xff\xff8\xb8\xff\xff1\xac\xf9\xff\x03q\xca\xff\x19\x89\xda\xff9\xb4\xfe\xffE\xbb\xff\xff$y\xb0\xff\x003W\xe7\x001R\xfc+\x80\xb6\xffD\xb9\xff\xff\x10\x97\xff\xff9\xb4\xff\xff;\xb7\xff\xff\x0e\x99\xff\xffE\xbe\xff\xff8\xba\xff\xff1\xaf\xfe\xff\x07\x80\xea\xff1\xa5\xf5\xff\x1d\x9e\xff\xffF\xbb\xff\xff$|\xb3\xff\x001Q\xfc\x001R\xfc+\x80\xb6\xffD\xb8\xff\xff\x10\x93\xff\xff:\xb2\xff\xff;\xb5\xff\xff\x0e\x95\xff\xffE\xbe\xff\xff8\xbc\xff\xff0\xb2\xff\xff\x1f\x9d\xfd\xff&\x9b\xf2\xff\x11\x8b\xf6\xffG\xba\xff\xff${\xb2\xff\x001Q\xfc\x004W\xe7*}\xb2\xffD\xb7\xff\xff\x0f\x88\xf5\xff8\xa8\xf4\xff?\xaf\xf6\xff\x0c\x89\xf7\xffE\xbc\xff\xff8\xbd\xff\xff4\xb9\xff\xff8\xb0\xf9\xff\tx\xcf\xff\x12\x80\xdc\xffG\xb7\xfe\xff$x\xae\xff\x003V\xe7\x00>h\xb3+z\xab\xffE\xb5\xf7\xff\x0fv\xc8\xff\x07g\xaa\xff\tl\xad\xff\nw\xcd\xffG\xbb\xfd\xff7\xb9\xff\xff\\\xc7\xff\xff3\x9b\xdc\xff\x00\\\xa4\xff\x13v\xbd\xffH\xb6\xf6\xff%u\xa6\xff\x00=f\xb2\x00X\x8f^"o\x9c\xf2F\xa5\xd8\xff\tY\x92\xff\x00Iw\xff\x00N|\xff\x04b\x9e\xffB\xab\xe6\xff+\xa0\xee\xff=\xa4\xe6\xff\x0ej\xa6\xff\x00L\x7f\xff\x0b[\x93\xffI\xa7\xd9\xff\x1ek\x97\xf2\x00W\x8c]\x00\xa1\xf0\x17\x02Gq\x9d\x056Y\xff\x002X\xff\x00:_\xff\x00Ah\xff\x00Jw\xff\x04Z\x90\xff\x02Z\x97\xff\x00U\x8e\xff\x00Gt\xff\x00<c\xff\x003Y\xff\x057Z\xff\x02Gp\x9d\x00\xa1\xf0\x16\x00\xfa\xff\x03\x00b\x95\x1f\x00-I\xb3\x00"<\xff\x00)G\xff\x002T\xff\x008]\xff\x00;b\xff\x00<d\xff\x009_\xff\x003V\xff\x00)H\xff\x00"<\xff\x00-I\xb3\x00b\x95\x1f\x00\xff\xff\x03\x00\x8b\xce\x00\x00\x15\'\x00\x00In\x18\x00.I\x92\x00#<\xef\x00"<\xff\x00%B\xff\x00(F\xff\x00(F\xff\x00%B\xff\x00"<\xff\x00#<\xef\x00.I\x92\x00In\x18\x00\x16(\x00\x00\x8a\xcc\x00\x00\xaf\xff\x00\x00\x00\x00\x00\x00Ac\x00\x00q\xa6\x05\x009XI\x00-I\xa8\x00\'A\xe4\x00%?\xfb\x00%?\xfb\x00\'A\xe4\x00-I\xa8\x009XI\x00q\xa6\x05\x00Ad\x00\x00\x00\x00\x00\x00\xaf\xff\x00'
image_sprite = pyglet.sprite.Sprite(pyglet.image.ImageData(16, 16, 'RGBA', raw_data), x=10, y=10)
And in both cases, you'll get a result that looks like this:
According to the docs, you like load an image from a file-like object (like a BytesIO) by specifying the file keyword parameter:
kitten_stream = io.BytesIO(some_bytes)
kitten = pyglet.image.load('kitten.png', file=kitten_stream)
(You may need to rewind the file pointer to the beginning of the file by calling kitten_stream.seek(0) before loading).

Modifying a large number of DICOM (.dcm) files.

bit of a simple question perhaps but I am not making progress and would appreciate help.
I have a list of size 422. Within index 0 there are 135 file paths to .dcm images. For example '~/images/0001.dcm','~/images/0135.dcm' Within index 1, there are 112 image paths, index 2 has 110, etc.
All images are of size 512 x 512. I am looking to re-size them to be 64 x 64.
This is my first time working with both image and .dcm data so I'm very unsure about how to resize. I am also unsure how to access and modify the files within the 'inner' list, if you will.
Is something like this way off the mark?
IMG_PX_SIZE = 64
result = []
for i in test_list:
result_inner_list = []
for image in i:
# resize all images at index position i and store in list
new_img = cv2.resize(np.array(image.pixel_array (IMG_PX_SIZE,IMG_PX_SIZE))
result_inner_list.append(new_img)
# Once all images at index point i are modified, append them these to a master list.
result.append(result_inner_list)
You seem to be struggling with two issues:
accessing the file paths
resize
For you to win, better separate these two tasks, sample code below
IMG_PX_SIZE = 64
def resize(image):
# your resize code here similar to:
# return v2.resize(np.array(image.pixel_array(IMG_PX_SIZE,IMG_PX_SIZE))
pass
def read(path):
# your file read operation here
pass
big_list = [['~/images/0001.dcm','~/images/0135.dcm'],
['~/images/0002.dcm','~/images/0136.dcm']]
resized_images = [[resize(read(path)) for path in paths] for paths in big_list]

Why os.remove() or shutil.move() can only move part of files

I want to randomly choose 10 images from the training dataset to be the test data. If I only copy the selected data to the destination path, it works. But if I want to remove the source data, it can only remove some of them. I tried both os.remove() and shutil.move() function, but the issue remains. The below is my script:
for label in labels:
training_data_path_ch1 = os.path.join(training_data_folder, label, 'ch1')
test_data_path_ch1 = os.path.join(test_data_folder, label, 'ch1')
training_data_path_ch5 = os.path.join(training_data_folder, label, 'ch5')
test_data_path_ch5 = os.path.join(test_data_folder, label, 'ch5')
ch1_imgs = listdir(training_data_path_ch1)
# Randomly select 10 images
ch1_mask = np.random.choice(len(ch1_imgs), 10)
ch1_selected_imgs = [ch1_imgs[i] for i in ch1_mask]
for selected_img in ch1_selected_imgs:
ch1_img_path = os.path.join(training_data_path_ch1, selected_img)
shutil.copy2(ch1_img_path, test_data_path_ch1)
os.remove(ch1_img_path)
print('Successfully move ' + label + ' ch1 images')
And I add an image to show the running status.
You can see, the program indeed can copy the images and remove some of the images, but why it cannot remove all images?
Any ideas? I appreciate any helps!
In:
ch1_mask = np.random.choice(len(ch1_imgs), 10)
You're potentially getting the same index returned more than once which means you're then trying to copy a file you've already copied and deleted (so you can't copy it again as it's removed), instead pass replace=False, eg:
ch1_mask = np.random.choice(len(ch1_imgs), 10, replace=False)

How to loop through one element of a zip() function twice - Python

So here's my dilema... I'm writing a script that reads all .png files from a folder and then converts them to a number of different dimensions which I have specified in a list. Everything works as it should except it quits after handling one image.
Here is my code:
sizeFormats = ["1024x1024", "114x114", "40x40", "58x58", "60x60", "640x1136", "640x960"]
def resizeImages():
widthList = []
heightList = []
resizedHeight = 0
resizedWidth = 0
#targetPath is the path to the folder that contains the images
folderToResizeContents = os.listdir(targetPath)
#This splits the dimensions into 2 separate lists for height and width (ex: 640x960 adds
#640 to widthList and 960 to heightList
for index in sizeFormats:
widthList.append(index.split("x")[0])
heightList.append(index.split("x")[1])
#for every image in the folder, apply the dimensions from the populated lists and save
for image,w,h in zip(folderToResizeContents,widthList,heightList):
resizedWidth = int(w)
resizedHeight = int(h)
sourceFilePath = os.path.join(targetPath,image)
imageFileToConvert = Image.open(sourceFilePath)
outputFile = imageFileToConvert.resize((resizedWidth,resizedHeight), Image.ANTIALIAS)
outputFile.save(sourceFilePath)
The following will be returned if the target folder contains 2 images called image1.png,image2.png (for sake of visualization I'll add the dimensions that get applied to the image after an underscore):
image1_1024x1024.png,
..............,
image1_640x690.png (Returns all 7 different dimensions for image1 fine)
it stops there when I need it to apply the same transformations to image_2. I know this is because the length of widthList and heightList are only 7 elements long and so exits the loop before image2 gets its turn. Is there any way I can go about looping through widthList and heightList for every image in the targetPath?
Why not keep it simple:
for image in folderToResizeContents:
for fmt in sizeFormats:
(w,h) = fmt.split('x')
N.B. You are overwriting the files produced as you are not changing the name of the outpath.
Nest your for loops and you can apply all 7 dimensions to each image
for image in folderToResizeContents:
for w,h in zip(widthList,heightList):
the first for loop will ensure it happens for each image, whereas the second for loop will ensure that the image is resized to each size
You need to re-iterate through the sizeFormats for every file. Zip doesn't do this unless you get even trickier with cyclic iterators for height and width.
Sometimes tools such as zip make for longer more complicated code when a couple of nested for loops work fine. I think its more straight forward than splitting into multiple lists and then zipping them back together again.
sizeFormats = ["1024x1024", "114x114", "40x40", "58x58", "60x60", "640x1136", "640x960"]
sizeTuples = [(int(w), int(h)) for w,h in map(lambda wh: wh.split('x'), sizeFormats)]
def resizeImages():
#for every image in the folder, apply the dimensions from the populated lists and save
for image in os.listdir(targetPath):
for resizedWidth, resizedHeight in sizeTuples:
sourceFilePath = os.path.join(targetPath,image)
imageFileToConvert = Image.open(sourceFilePath)
outputFile = imageFileToConvert.resize((resizedWidth,resizedHeight), Image.ANTIALIAS)
outputFile.save(sourceFilePath)

Categories